map forms to labels

This commit is contained in:
Matthew Spence 2024-09-20 13:53:43 -05:00
parent cb7005611a
commit 5dbc356d71
No known key found for this signature in database

View file

@ -149,39 +149,35 @@ class DomainFormBaseView(DomainBaseView, FormMixin):
return current_domain_info return current_domain_info
def send_update_notification(self, form, is_formset: bool=False): def send_update_notification(self, form):
"""Send a notification to all domain managers that an update has occured """Send a notification to all domain managers that an update has occured
for a single domain. Uses update_to_approved_domain.txt template. for a single domain. Uses update_to_approved_domain.txt template.
Checks for changes, and does nothing if the form has not changed. Checks for changes, and does nothing if the form has not changed.
Formsets have to be handled in a special way, so use is_formset to indicate
whether the value passed into form is actually a formset.
""" """
# send notification email for changes to any of these forms # send notification email for changes to any of these forms
notify_on_change = ( form_label_dict = {
DomainSecurityEmailForm, DomainSecurityEmailForm: "Security Email",
DomainDnssecForm, DomainDnssecForm: "DNSSec",
DomainDsdataFormset, DomainDsdataFormset: "DS Data",
) DomainOrgNameAddressForm: "Org Name/Address",
SeniorOfficialContactForm: "Senior Official",
}
# forms of these types should not send notifications if they're part of a portfolio/Organization # forms of these types should not send notifications if they're part of a portfolio/Organization
notify_unless_in_portfolio = ( check_for_portfolio = {
DomainOrgNameAddressForm, DomainOrgNameAddressForm,
SeniorOfficialContactForm SeniorOfficialContactForm,
) }
if isinstance(form, notify_on_change): if form.__class__ in form_label_dict:
# always notify for these forms
should_notify=True should_notify=True
elif isinstance(form, notify_unless_in_portfolio): if form.__class__ in check_for_portfolio:
# for these forms, only notify if the domain is not in a portfolio # check for portfolio
info: DomainInformation = self.get_domain_info_from_domain() info = self.get_domain_info_from_domain()
if not info or info.portfolio: if not info or info.portfolio:
should_notify = False should_notify = False
else:
should_notify=True
else: else:
# don't notify for any other types of forms # don't notify for any other types of forms
should_notify=False should_notify=False
@ -189,22 +185,13 @@ class DomainFormBaseView(DomainBaseView, FormMixin):
if should_notify and form.has_changed: if should_notify and form.has_changed:
logger.info("Sending email to domain managers") logger.info("Sending email to domain managers")
changes = self._get_changes_from_formset(form) if is_formset else form.changed_data
context={ context={
"domain": self.object.name, "domain": self.object.name,
"user": self.request.user, "user": self.request.user,
"date": date.today(), "date": date.today(),
"changes": str(changes).strip("'") # django templates auto-escape quotes "changes": form_label_dict[form.__class__]
} }
self.email_domain_managers(self.object, "emails/update_to_approved_domain.txt", "emails/update_to_approved_domain_subject.txt", context) self.email_domain_managers(self.object, "emails/update_to_approved_domain.txt", "emails/update_to_approved_domain_subject.txt", context)
def _get_changes_from_formset(self, formset):
changes = set()
for form in formset:
changes.update(form.changed_data)
return list(changes)
def email_domain_managers(self, domain_name, template: str, subject_template: str, context: any = {}): def email_domain_managers(self, domain_name, template: str, subject_template: str, context: any = {}):
"""Send a single email built from a template to all managers for a given domain. """Send a single email built from a template to all managers for a given domain.
@ -561,7 +548,7 @@ class DomainNameserversView(DomainFormBaseView):
messages.error(self.request, NameserverError(code=nsErrorCodes.BAD_DATA)) messages.error(self.request, NameserverError(code=nsErrorCodes.BAD_DATA))
logger.error(f"Registry error: {Err}") logger.error(f"Registry error: {Err}")
else: else:
self.send_update_notification(formset, is_formset=True) self.send_update_notification(formset)
messages.success( messages.success(
self.request, self.request,
"The name servers for this domain have been updated. " "The name servers for this domain have been updated. "
@ -739,7 +726,7 @@ class DomainDsDataView(DomainFormBaseView):
logger.error(f"Registry error: {err}") logger.error(f"Registry error: {err}")
return self.form_invalid(formset) return self.form_invalid(formset)
else: else:
self.send_update_notification(formset, is_formset=True) self.send_update_notification(formset)
messages.success(self.request, "The DS data records for this domain have been updated.") messages.success(self.request, "The DS data records for this domain have been updated.")
# superclass has the redirect # superclass has the redirect