From 025a128baef4c7a3fc08113df5a529d3ddd491c8 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Fri, 12 Jul 2024 11:54:27 -0600 Subject: [PATCH] Refactor --- src/registrar/admin.py | 68 +++++++++++------------- src/registrar/assets/js/get-gov-admin.js | 67 ++++++++++------------- 2 files changed, 58 insertions(+), 77 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 7d5708e62..8f00ab519 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1731,46 +1731,37 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin): if not change: return super().save_model(request, obj, form, change) - # == Handle non-status changes == # # Get the original domain request from the database. original_obj = models.DomainRequest.objects.get(pk=obj.pk) - # If the reason is in a state where we can send out an email, - # set the email to a default one if a custom email isn't provided. - if obj.action_needed_reason and obj.action_needed_reason != obj.ActionNeededReasons.OTHER: - obj = self._handle_existing_action_needed_reason_email(obj, original_obj) - else: + # == Handle action_needed_reason == # + # Store the email that was sent out if one was sent and it isn't saved to a variable yet + if not obj.action_needed_reason or obj.action_needed_reason == obj.ActionNeededReasons.OTHER: + # Reset the action needed email to none if we don't send out an email. obj.action_needed_reason_email = None + else: + # We send out an email -- store which one we send out. + # Set the email to a default one if a custom email isn't provided. + default_email = self._get_action_needed_reason_default_email(obj, obj.action_needed_reason) + body_text = default_email.get("email_body_text") + # Set the action_needed_reason_email to the default + 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 + + # == Handle status == # if obj.status == original_obj.status: # If the status hasn't changed, let the base function take care of it return super().save_model(request, obj, form, change) + else: + # Run some checks on the current object for invalid status changes + obj, should_save = self._handle_status_change(request, obj, original_obj) - # == Handle status changes == # - # Run some checks on the current object for invalid status changes - obj, should_save = self._handle_status_change(request, obj, original_obj) - - # We should only save if we don't display any errors in the steps above. - if should_save: - return super().save_model(request, obj, form, change) - - def _handle_existing_action_needed_reason_email(self, obj, original_obj): - """ - Changes the action_needed_reason to the default email. - This occurs if the email changes and if it is empty. - """ - - # Get the default email - text = self._get_action_needed_reason_default_email(obj, obj.action_needed_reason) - body_text = text.get("email_body_text") - - # Set the action_needed_reason_email to the default - 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 - - return obj + # We should only save if we don't display any errors in the steps above. + if should_save: + return super().save_model(request, obj, form, change) def _handle_status_change(self, request, obj, original_obj): """ @@ -1976,17 +1967,18 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin): for this particular domain request.""" emails = {} for action_needed_reason in domain_request.ActionNeededReasons: - enum_value = action_needed_reason.value - custom_text = None - if domain_request.action_needed_reason == enum_value and domain_request.action_needed_reason_email: - custom_text = domain_request.action_needed_reason_email + if action_needed_reason != DomainRequest.ActionNeededReasons.OTHER: + enum_value = action_needed_reason.value + custom_text = None + 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(domain_request, enum_value, custom_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(self, domain_request, action_needed_reason: str, custom_text=None): + def _get_action_needed_reason_default_email(self, domain_request, action_needed_reason, 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: + if not action_needed_reason: return { "subject_text": None, "email_body_text": None, diff --git a/src/registrar/assets/js/get-gov-admin.js b/src/registrar/assets/js/get-gov-admin.js index f6aee5dbe..e41431ba3 100644 --- a/src/registrar/assets/js/get-gov-admin.js +++ b/src/registrar/assets/js/get-gov-admin.js @@ -528,65 +528,54 @@ function initializeWidgetOnList(list, parentId) { (function () { let actionNeededReasonDropdown = document.querySelector("#id_action_needed_reason"); let actionNeededEmail = document.querySelector("#id_action_needed_reason_email"); + let actionNeededEmailData = document.getElementById('action-needed-emails-data').textContent); let noEmailMessage = document.getElementById("no-email-message"); const emptyReasonText = "---------" const noEmailText = "No email will be sent." - if(actionNeededReasonDropdown && actionNeededEmail) { + if(actionNeededReasonDropdown && actionNeededEmail && actionNeededEmailData) { // Add a change listener to the action needed reason dropdown - handleChangeActionNeededEmail(actionNeededReasonDropdown, actionNeededEmail); + handleChangeActionNeededEmail(actionNeededReasonDropdown, actionNeededEmail, actionNeededEmailData); document.addEventListener('DOMContentLoaded', function() { - if (!actionNeededReasonDropdown.value) { - noEmailMessage.innerHTML = emptyReasonText - showNoEmailMessage(actionNeededEmail, noEmailMessage); - } else if (actionNeededReasonDropdown.value == "other") { - noEmailMessage.innerHTML = noEmailText + let reason = actionNeededReasonDropdown.value; + noEmailMessage.innerHTML = reason ? noEmailText : emptyReasonText; + if (reason && reason != "other") { + // Show the email + showElement(actionNeededEmail); + hideElement(noEmailMessage); + } else { + // Show the no email message + hideElement(actionNeededEmail); + showElement(noEmailMessage); } }); } - function handleChangeActionNeededEmail(actionNeededReasonDropdown, actionNeededEmail) { + function handleChangeActionNeededEmail(actionNeededReasonDropdown, actionNeededEmail, actionNeededEmailData) { actionNeededReasonDropdown.addEventListener("change", function() { let reason = actionNeededReasonDropdown.value; + let actionNeededEmailsJson = JSON.parse(actionNeededEmailData) - // If a reason isn't specified, no email will be sent. - // You also cannot save the model in this state. - // This flow occurs if you switch back to the empty picker state. - if(!reason) { - noEmailMessage.innerHTML = emptyReasonText - showNoEmailMessage(actionNeededEmail, noEmailMessage); - return; - }else if (reason === "other") { - noEmailMessage.innerHTML = noEmailText - } - - let actionNeededEmails = JSON.parse(document.getElementById('action-needed-emails-data').textContent) - let emailData = actionNeededEmails[reason]; - if (emailData) { + // Show the "no email will be sent" text only if a reason is actually selected. + noEmailMessage.innerHTML = reason ? noEmailText : emptyReasonText; + if (reason && reason in actionNeededEmailsJson) { + let emailData = actionNeededEmailsJson[reason]; let emailBody = emailData.email_body_text if (emailBody) { + // Show the email actionNeededEmail.value = emailBody - showActionNeededEmail(actionNeededEmail, noEmailMessage); + showElement(actionNeededEmail); + hideElement(noEmailMessage); }else { - showNoEmailMessage(actionNeededEmail, noEmailMessage); + // Show the no email message + hideElement(actionNeededEmail); + showElement(noEmailMessage); } }else { - showNoEmailMessage(actionNeededEmail, noEmailMessage); + // Show the no email message + hideElement(actionNeededEmail); + showElement(noEmailMessage); } - }); } - - // Show the text field. Hide the "no email" message. - function showActionNeededEmail(actionNeededEmail, noEmailMessage){ - showElement(actionNeededEmail); - hideElement(noEmailMessage); - } - - // Hide the text field. Show the "no email" message. - function showNoEmailMessage(actionNeededEmail, noEmailMessage) { - hideElement(actionNeededEmail); - showElement(noEmailMessage); - } - })();