mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-28 16:29:54 +02:00
Logic refactor
This commit is contained in:
parent
f18b10d669
commit
425cfbcb4e
4 changed files with 48 additions and 45 deletions
|
@ -1735,15 +1735,16 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
|
||||||
# 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 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_text(obj, obj.action_needed_reason)
|
text = self._get_action_needed_reason_default_email(obj, obj.action_needed_reason)
|
||||||
body_text = text.get("email_body_text")
|
body_text = text.get("email_body_text")
|
||||||
if body_text:
|
reason_changed = obj.action_needed_reason != original_obj.action_needed_reason
|
||||||
body_text.strip().lstrip("\n")
|
is_default_email = body_text == obj.action_needed_reason_email
|
||||||
is_default_email = body_text == obj.action_needed_reason_email
|
if body_text and is_default_email and reason_changed:
|
||||||
reason_changed = obj.action_needed_reason != original_obj.action_needed_reason
|
obj.action_needed_reason_email = body_text
|
||||||
if is_default_email and reason_changed:
|
should_save = True
|
||||||
obj.action_needed_reason_email = body_text
|
elif not obj.action_needed_reason and obj.action_needed_reason_email:
|
||||||
|
obj.action_needed_reason_email = None
|
||||||
should_save = True
|
should_save = True
|
||||||
|
|
||||||
if obj.status == original_obj.status:
|
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:
|
if domain_request.action_needed_reason == enum_value and domain_request.action_needed_reason_email:
|
||||||
custom_text = 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
|
domain_request, enum_value, custom_text
|
||||||
)
|
)
|
||||||
return json.dumps(emails)
|
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"""
|
"""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 action_needed_reason is None or action_needed_reason == domain_request.ActionNeededReasons.OTHER:
|
||||||
return {
|
return {
|
||||||
|
@ -1980,28 +1981,34 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
|
||||||
"email_body_text": None,
|
"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
|
if flag_is_active(None, "profile_feature"): # type: ignore
|
||||||
recipient = domain_request.creator
|
recipient = domain_request.creator
|
||||||
else:
|
else:
|
||||||
recipient = domain_request.submitter
|
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}
|
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 {
|
return {
|
||||||
"subject_text": subject_template.render(context=context),
|
"subject_text": subject_text,
|
||||||
"email_body_text": template.render(context=context) if not custom_text else custom_text,
|
"email_body_text": email_body_text_cleaned,
|
||||||
}
|
}
|
||||||
|
|
||||||
def process_log_entry(self, log_entry):
|
def process_log_entry(self, log_entry):
|
||||||
|
|
|
@ -531,6 +531,12 @@ function initializeWidgetOnList(list, parentId) {
|
||||||
if(actionNeededReasonDropdown && actionNeededEmail) {
|
if(actionNeededReasonDropdown && actionNeededEmail) {
|
||||||
// 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);
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
if (!actionNeededReasonDropdown.value || actionNeededReasonDropdown.value == "other") {
|
||||||
|
showNoEmailMessage(actionNeededEmail);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleChangeActionNeededEmail(actionNeededReasonDropdown, actionNeededEmail) {
|
function handleChangeActionNeededEmail(actionNeededReasonDropdown, actionNeededEmail) {
|
||||||
|
|
|
@ -874,14 +874,10 @@ class DomainRequest(TimeStampedModel):
|
||||||
def _send_action_needed_reason_email(self, send_email=True, email_content=None):
|
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"""
|
"""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_name: str = ""
|
||||||
email_template_subject_name: str = ""
|
email_template_subject_name: str = ""
|
||||||
|
|
||||||
# Check if the current email that we sent out is the same as our defaults.
|
if email_content is not None:
|
||||||
# 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:
|
|
||||||
email_template_name = "custom_email.txt"
|
email_template_name = "custom_email.txt"
|
||||||
|
|
||||||
# Check for the "type" of action needed reason.
|
# Check for the "type" of action needed reason.
|
||||||
|
@ -916,21 +912,6 @@ class DomainRequest(TimeStampedModel):
|
||||||
wrap_email=True,
|
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(
|
@transition(
|
||||||
field="status",
|
field="status",
|
||||||
source=[
|
source=[
|
||||||
|
|
|
@ -143,6 +143,15 @@ This is using a custom implementation fieldset.html (see admin/fieldset.html)
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
{% endblock field_readonly %}
|
{% 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 %}
|
{% block after_help_text %}
|
||||||
{% if field.field.name == "action_needed_reason_email" %}
|
{% if field.field.name == "action_needed_reason_email" %}
|
||||||
{% comment %}
|
{% comment %}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue