From 425cfbcb4eae8b5b1e2a4ac2df5691a399ce9886 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Mon, 8 Jul 2024 14:13:29 -0600 Subject: [PATCH] Logic refactor --- src/registrar/admin.py | 57 +++++++++++-------- src/registrar/assets/js/get-gov-admin.js | 6 ++ src/registrar/models/domain_request.py | 21 +------ .../admin/includes/detail_table_fieldset.html | 9 +++ 4 files changed, 48 insertions(+), 45 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index e50b262fb..1561b7e1f 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1735,15 +1735,16 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin): # Get the original domain request from the database. original_obj = models.DomainRequest.objects.get(pk=obj.pk) - if obj.action_needed_reason: - text = self._get_action_needed_reason_default_email_text(obj, obj.action_needed_reason) + if obj.action_needed_reason and obj.action_needed_reason != obj.ActionNeededReasons.OTHER: + text = self._get_action_needed_reason_default_email(obj, obj.action_needed_reason) body_text = text.get("email_body_text") - if body_text: - body_text.strip().lstrip("\n") - is_default_email = body_text == obj.action_needed_reason_email - reason_changed = obj.action_needed_reason != original_obj.action_needed_reason - if is_default_email and reason_changed: - obj.action_needed_reason_email = body_text + reason_changed = obj.action_needed_reason != original_obj.action_needed_reason + is_default_email = body_text == obj.action_needed_reason_email + if body_text and is_default_email and reason_changed: + obj.action_needed_reason_email = body_text + should_save = True + elif not obj.action_needed_reason and obj.action_needed_reason_email: + obj.action_needed_reason_email = None should_save = True if obj.status == original_obj.status: @@ -1967,12 +1968,12 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin): if domain_request.action_needed_reason == enum_value and domain_request.action_needed_reason_email: custom_text = domain_request.action_needed_reason_email - emails[enum_value] = self._get_action_needed_reason_default_email_text( + emails[enum_value] = self._get_action_needed_reason_default_email( domain_request, enum_value, custom_text ) return json.dumps(emails) - def _get_action_needed_reason_default_email_text(self, domain_request, action_needed_reason: str, custom_text=None): + def _get_action_needed_reason_default_email(self, domain_request, action_needed_reason: str, custom_text=None): """Returns the default email associated with the given action needed reason""" if action_needed_reason is None or action_needed_reason == domain_request.ActionNeededReasons.OTHER: return { @@ -1980,28 +1981,34 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin): "email_body_text": None, } - # Get the email body - if not custom_text: - template_path = f"emails/action_needed_reasons/{action_needed_reason}.txt" - else: - template_path = "emails/action_needed_reasons/custom_email.txt" - - template = get_template(template_path) - - # Get the email subject - template_subject_path = f"emails/action_needed_reasons/{action_needed_reason}_subject.txt" - subject_template = get_template(template_subject_path) - if flag_is_active(None, "profile_feature"): # type: ignore recipient = domain_request.creator else: recipient = domain_request.submitter - # Return the content of the rendered views + # Return the context of the rendered views context = {"domain_request": domain_request, "recipient": recipient} + + # Get the email subject + template_subject_path = f"emails/action_needed_reasons/{action_needed_reason}_subject.txt" + subject_text = get_template(template_subject_path).render(context=context) + + # Get the email body + if not custom_text: + template_path = f"emails/action_needed_reasons/{action_needed_reason}.txt" + else: + template_path = "emails/action_needed_reasons/custom_email.txt" + context["custom_email_content"] = custom_text + + email_body_text = get_template(template_path).render(context=context) + + email_body_text_cleaned = None + if email_body_text: + email_body_text_cleaned = email_body_text.strip().lstrip("\n") + return { - "subject_text": subject_template.render(context=context), - "email_body_text": template.render(context=context) if not custom_text else custom_text, + "subject_text": subject_text, + "email_body_text": email_body_text_cleaned, } def process_log_entry(self, log_entry): diff --git a/src/registrar/assets/js/get-gov-admin.js b/src/registrar/assets/js/get-gov-admin.js index 2a01eb304..3cf20311d 100644 --- a/src/registrar/assets/js/get-gov-admin.js +++ b/src/registrar/assets/js/get-gov-admin.js @@ -531,6 +531,12 @@ function initializeWidgetOnList(list, parentId) { if(actionNeededReasonDropdown && actionNeededEmail) { // Add a change listener to the action needed reason dropdown handleChangeActionNeededEmail(actionNeededReasonDropdown, actionNeededEmail); + + document.addEventListener('DOMContentLoaded', function() { + if (!actionNeededReasonDropdown.value || actionNeededReasonDropdown.value == "other") { + showNoEmailMessage(actionNeededEmail); + } + }); } function handleChangeActionNeededEmail(actionNeededReasonDropdown, actionNeededEmail) { diff --git a/src/registrar/models/domain_request.py b/src/registrar/models/domain_request.py index 5740d9504..25138c54a 100644 --- a/src/registrar/models/domain_request.py +++ b/src/registrar/models/domain_request.py @@ -874,14 +874,10 @@ class DomainRequest(TimeStampedModel): def _send_action_needed_reason_email(self, send_email=True, email_content=None): """Sends out an automatic email for each valid action needed reason provided""" - # Store the filenames of the template and template subject email_template_name: str = "" email_template_subject_name: str = "" - # Check if the current email that we sent out is the same as our defaults. - # If these differ, then that means that we're sending custom content. - default_email = self.get_default_action_needed_reason_email(self.action_needed_reason) - if self.action_needed_reason_email and self.action_needed_reason_email != default_email: + if email_content is not None: email_template_name = "custom_email.txt" # Check for the "type" of action needed reason. @@ -916,21 +912,6 @@ class DomainRequest(TimeStampedModel): wrap_email=True, ) - def get_default_action_needed_reason_email(self, action_needed_reason): - """Returns the default email associated with the given action needed reason""" - if action_needed_reason is None or action_needed_reason == self.ActionNeededReasons.OTHER: - return None - - # Get the email body - template_path = f"emails/action_needed_reasons/{action_needed_reason}.txt" - template = get_template(template_path) - - recipient = self.creator if flag_is_active(None, "profile_feature") else self.submitter - # Return the content of the rendered views - context = {"domain_request": self, "recipient": recipient} - body_text = template.render(context=context).strip().lstrip("\n") - return body_text - @transition( field="status", source=[ diff --git a/src/registrar/templates/django/admin/includes/detail_table_fieldset.html b/src/registrar/templates/django/admin/includes/detail_table_fieldset.html index 8b8748f80..2aa5c0a60 100644 --- a/src/registrar/templates/django/admin/includes/detail_table_fieldset.html +++ b/src/registrar/templates/django/admin/includes/detail_table_fieldset.html @@ -143,6 +143,15 @@ This is using a custom implementation fieldset.html (see admin/fieldset.html) {% endwith %} {% endblock field_readonly %} +{% block field_other %} + {% if field.field.name == "action_needed_reason_email" %} + {{ field.field }} +
No email will be sent.
+ {% else %} + {{ field.field }} + {% endif %} +{% endblock field_other %} + {% block after_help_text %} {% if field.field.name == "action_needed_reason_email" %} {% comment %}