This commit is contained in:
zandercymatics 2024-07-12 11:54:27 -06:00
parent ed653e4562
commit 025a128bae
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
2 changed files with 58 additions and 77 deletions

View file

@ -1731,46 +1731,37 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
if not change: if not change:
return super().save_model(request, obj, form, change) return super().save_model(request, obj, form, change)
# == Handle non-status changes == #
# Get the original domain request from the database. # Get the original domain request from the database.
original_obj = models.DomainRequest.objects.get(pk=obj.pk) original_obj = models.DomainRequest.objects.get(pk=obj.pk)
# If the reason is in a state where we can send out an email, # == Handle action_needed_reason == #
# set the email to a default one if a custom email isn't provided. # Store the email that was sent out if one was sent and it isn't saved to a variable yet
if obj.action_needed_reason and obj.action_needed_reason != obj.ActionNeededReasons.OTHER: if not obj.action_needed_reason or obj.action_needed_reason == obj.ActionNeededReasons.OTHER:
obj = self._handle_existing_action_needed_reason_email(obj, original_obj) # Reset the action needed email to none if we don't send out an email.
else:
obj.action_needed_reason_email = None 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 obj.status == original_obj.status:
# If the status hasn't changed, let the base function take care of it # If the status hasn't changed, let the base function take care of it
return super().save_model(request, obj, form, change) 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 == # # We should only save if we don't display any errors in the steps above.
# Run some checks on the current object for invalid status changes if should_save:
obj, should_save = self._handle_status_change(request, obj, original_obj) return super().save_model(request, obj, form, change)
# 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
def _handle_status_change(self, request, obj, original_obj): def _handle_status_change(self, request, obj, original_obj):
""" """
@ -1976,17 +1967,18 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
for this particular domain request.""" for this particular domain request."""
emails = {} emails = {}
for action_needed_reason in domain_request.ActionNeededReasons: for action_needed_reason in domain_request.ActionNeededReasons:
enum_value = action_needed_reason.value if action_needed_reason != DomainRequest.ActionNeededReasons.OTHER:
custom_text = None enum_value = action_needed_reason.value
if domain_request.action_needed_reason == enum_value and domain_request.action_needed_reason_email: custom_text = None
custom_text = domain_request.action_needed_reason_email 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) 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""" """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 { return {
"subject_text": None, "subject_text": None,
"email_body_text": None, "email_body_text": None,

View file

@ -528,65 +528,54 @@ function initializeWidgetOnList(list, parentId) {
(function () { (function () {
let actionNeededReasonDropdown = document.querySelector("#id_action_needed_reason"); let actionNeededReasonDropdown = document.querySelector("#id_action_needed_reason");
let actionNeededEmail = document.querySelector("#id_action_needed_reason_email"); 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"); let noEmailMessage = document.getElementById("no-email-message");
const emptyReasonText = "---------" const emptyReasonText = "---------"
const noEmailText = "No email will be sent." const noEmailText = "No email will be sent."
if(actionNeededReasonDropdown && actionNeededEmail) { if(actionNeededReasonDropdown && actionNeededEmail && actionNeededEmailData) {
// Add a change listener to the action needed reason dropdown // Add a change listener to the action needed reason dropdown
handleChangeActionNeededEmail(actionNeededReasonDropdown, actionNeededEmail); handleChangeActionNeededEmail(actionNeededReasonDropdown, actionNeededEmail, actionNeededEmailData);
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {
if (!actionNeededReasonDropdown.value) { let reason = actionNeededReasonDropdown.value;
noEmailMessage.innerHTML = emptyReasonText noEmailMessage.innerHTML = reason ? noEmailText : emptyReasonText;
showNoEmailMessage(actionNeededEmail, noEmailMessage); if (reason && reason != "other") {
} else if (actionNeededReasonDropdown.value == "other") { // Show the email
noEmailMessage.innerHTML = noEmailText 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() { actionNeededReasonDropdown.addEventListener("change", function() {
let reason = actionNeededReasonDropdown.value; let reason = actionNeededReasonDropdown.value;
let actionNeededEmailsJson = JSON.parse(actionNeededEmailData)
// If a reason isn't specified, no email will be sent. // Show the "no email will be sent" text only if a reason is actually selected.
// You also cannot save the model in this state. noEmailMessage.innerHTML = reason ? noEmailText : emptyReasonText;
// This flow occurs if you switch back to the empty picker state. if (reason && reason in actionNeededEmailsJson) {
if(!reason) { let emailData = actionNeededEmailsJson[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) {
let emailBody = emailData.email_body_text let emailBody = emailData.email_body_text
if (emailBody) { if (emailBody) {
// Show the email
actionNeededEmail.value = emailBody actionNeededEmail.value = emailBody
showActionNeededEmail(actionNeededEmail, noEmailMessage); showElement(actionNeededEmail);
hideElement(noEmailMessage);
}else { }else {
showNoEmailMessage(actionNeededEmail, noEmailMessage); // Show the no email message
hideElement(actionNeededEmail);
showElement(noEmailMessage);
} }
}else { }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);
}
})(); })();