Logic refactor

This commit is contained in:
zandercymatics 2024-07-08 14:13:29 -06:00
parent f18b10d669
commit 425cfbcb4e
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
4 changed files with 48 additions and 45 deletions

View file

@ -1735,16 +1735,17 @@ 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:
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:
# If the status hasn't changed, let the base function take care of it
@ -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):

View file

@ -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) {

View file

@ -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=[

View file

@ -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 }}
<p id="no-email-message" class="{% if original_object.action_needed_reason %}display-none{% endif %}">No email will be sent.</p>
{% else %}
{{ field.field }}
{% endif %}
{% endblock field_other %}
{% block after_help_text %}
{% if field.field.name == "action_needed_reason_email" %}
{% comment %}