mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-17 10:07:04 +02:00
Refactor
This commit is contained in:
parent
ed653e4562
commit
025a128bae
2 changed files with 58 additions and 77 deletions
|
@ -1731,38 +1731,19 @@ 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:
|
||||||
if obj.status == original_obj.status:
|
# We send out an email -- store which one we send out.
|
||||||
# If the status hasn't changed, let the base function take care of it
|
# Set the email to a default one if a custom email isn't provided.
|
||||||
return super().save_model(request, obj, form, change)
|
default_email = self._get_action_needed_reason_default_email(obj, obj.action_needed_reason)
|
||||||
|
body_text = default_email.get("email_body_text")
|
||||||
# == 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
|
# Set the action_needed_reason_email to the default
|
||||||
reason_changed = obj.action_needed_reason != original_obj.action_needed_reason
|
reason_changed = obj.action_needed_reason != original_obj.action_needed_reason
|
||||||
|
@ -1770,7 +1751,17 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
|
||||||
if body_text and is_default_email and reason_changed:
|
if body_text and is_default_email and reason_changed:
|
||||||
obj.action_needed_reason_email = body_text
|
obj.action_needed_reason_email = body_text
|
||||||
|
|
||||||
return obj
|
# == 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)
|
||||||
|
|
||||||
|
# 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):
|
def _handle_status_change(self, request, obj, original_obj):
|
||||||
"""
|
"""
|
||||||
|
@ -1976,6 +1967,7 @@ 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:
|
||||||
|
if action_needed_reason != DomainRequest.ActionNeededReasons.OTHER:
|
||||||
enum_value = action_needed_reason.value
|
enum_value = action_needed_reason.value
|
||||||
custom_text = None
|
custom_text = None
|
||||||
if domain_request.action_needed_reason == enum_value and domain_request.action_needed_reason_email:
|
if domain_request.action_needed_reason == enum_value and domain_request.action_needed_reason_email:
|
||||||
|
@ -1984,9 +1976,9 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
|
||||||
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,
|
||||||
|
|
|
@ -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) {
|
|
||||||
noEmailMessage.innerHTML = emptyReasonText
|
|
||||||
showNoEmailMessage(actionNeededEmail, noEmailMessage);
|
|
||||||
} else if (actionNeededReasonDropdown.value == "other") {
|
|
||||||
noEmailMessage.innerHTML = noEmailText
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleChangeActionNeededEmail(actionNeededReasonDropdown, actionNeededEmail) {
|
|
||||||
actionNeededReasonDropdown.addEventListener("change", function() {
|
|
||||||
let reason = actionNeededReasonDropdown.value;
|
let reason = actionNeededReasonDropdown.value;
|
||||||
|
noEmailMessage.innerHTML = reason ? noEmailText : emptyReasonText;
|
||||||
// If a reason isn't specified, no email will be sent.
|
if (reason && reason != "other") {
|
||||||
// You also cannot save the model in this state.
|
// Show the email
|
||||||
// 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) {
|
|
||||||
let emailBody = emailData.email_body_text
|
|
||||||
if (emailBody) {
|
|
||||||
actionNeededEmail.value = emailBody
|
|
||||||
showActionNeededEmail(actionNeededEmail, noEmailMessage);
|
|
||||||
}else {
|
|
||||||
showNoEmailMessage(actionNeededEmail, noEmailMessage);
|
|
||||||
}
|
|
||||||
}else {
|
|
||||||
showNoEmailMessage(actionNeededEmail, noEmailMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Show the text field. Hide the "no email" message.
|
|
||||||
function showActionNeededEmail(actionNeededEmail, noEmailMessage){
|
|
||||||
showElement(actionNeededEmail);
|
showElement(actionNeededEmail);
|
||||||
hideElement(noEmailMessage);
|
hideElement(noEmailMessage);
|
||||||
}
|
} else {
|
||||||
|
// Show the no email message
|
||||||
// Hide the text field. Show the "no email" message.
|
|
||||||
function showNoEmailMessage(actionNeededEmail, noEmailMessage) {
|
|
||||||
hideElement(actionNeededEmail);
|
hideElement(actionNeededEmail);
|
||||||
showElement(noEmailMessage);
|
showElement(noEmailMessage);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleChangeActionNeededEmail(actionNeededReasonDropdown, actionNeededEmail, actionNeededEmailData) {
|
||||||
|
actionNeededReasonDropdown.addEventListener("change", function() {
|
||||||
|
let reason = actionNeededReasonDropdown.value;
|
||||||
|
let actionNeededEmailsJson = JSON.parse(actionNeededEmailData)
|
||||||
|
|
||||||
|
// 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
|
||||||
|
showElement(actionNeededEmail);
|
||||||
|
hideElement(noEmailMessage);
|
||||||
|
}else {
|
||||||
|
// Show the no email message
|
||||||
|
hideElement(actionNeededEmail);
|
||||||
|
showElement(noEmailMessage);
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
// Show the no email message
|
||||||
|
hideElement(actionNeededEmail);
|
||||||
|
showElement(noEmailMessage);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue