Remove endpoint for email content

We don't really need this. We can just store the email content on the DOM. There is no real performance impact -- its not a lot of content in the grand scheme of things and we'd just be doing a lot of api calls anyway
This commit is contained in:
zandercymatics 2024-06-21 13:02:15 -06:00
parent 5ae8df2bdb
commit abc9fb78bc
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
7 changed files with 36 additions and 44 deletions

View file

@ -1845,6 +1845,7 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
# Initialize extra_context and add filtered entries # Initialize extra_context and add filtered entries
extra_context = extra_context or {} extra_context = extra_context or {}
extra_context["filtered_audit_log_entries"] = filtered_audit_log_entries extra_context["filtered_audit_log_entries"] = filtered_audit_log_entries
extra_context["action_needed_reason_emails"] = obj.get_all_action_needed_reason_emails_as_json()
# Call the superclass method with updated extra_context # Call the superclass method with updated extra_context
return super().change_view(request, object_id, form_url, extra_context) return super().change_view(request, object_id, form_url, extra_context)

View file

@ -572,21 +572,20 @@ function initializeWidgetOnList(list, parentId) {
return; return;
} }
fetch(`/get-domain-requests-json/${pk}/action-needed-email/${reason}`) let actionNeededEmails = JSON.parse(document.getElementById('action-needed-emails-data').textContent)
.then(response => response.json()) let emailData = actionNeededEmails[reason];
.then(data => { if (emailData) {
if (data.error) { let emailBody = emailData.email_body_text
console.log('Error in AJAX call: ' + data.error); if (emailBody) {
return; actionNeededEmail.value = emailBody
}
if(data && data.email_body_text) {
actionNeededEmail.value = data.email_body_text
showActionNeededEmail(actionNeededEmail); showActionNeededEmail(actionNeededEmail);
}else { }else {
showNoEmailMessage(actionNeededEmail); showNoEmailMessage(actionNeededEmail);
} }
}); }else {
showNoEmailMessage(actionNeededEmail);
}
}); });
} }

View file

@ -22,7 +22,7 @@ from registrar.views.admin_views import (
) )
from registrar.views.domain_request import Step from registrar.views.domain_request import Step
from registrar.views.domain_requests_json import get_domain_requests_json, get_action_needed_email from registrar.views.domain_requests_json import get_domain_requests_json
from registrar.views.domains_json import get_domains_json from registrar.views.domains_json import get_domains_json
from registrar.views.utility import always_404 from registrar.views.utility import always_404
from api.views import available, get_current_federal, get_current_full from api.views import available, get_current_federal, get_current_full
@ -213,11 +213,6 @@ urlpatterns = [
), ),
path("get-domains-json/", get_domains_json, name="get_domains_json"), path("get-domains-json/", get_domains_json, name="get_domains_json"),
path("get-domain-requests-json/", get_domain_requests_json, name="get_domain_requests_json"), path("get-domain-requests-json/", get_domain_requests_json, name="get_domain_requests_json"),
path(
"get-domain-requests-json/<int:pk>/action-needed-email/<str:reason>",
get_action_needed_email,
name="get_action_needed_email",
),
] ]
# Djangooidc strips out context data from that context, so we define a custom error # Djangooidc strips out context data from that context, so we define a custom error

View file

@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Union from typing import Union
import logging import logging
import json
from django.apps import apps from django.apps import apps
from django.conf import settings from django.conf import settings
@ -1220,6 +1221,15 @@ class DomainRequest(TimeStampedModel):
return True return True
def get_all_action_needed_reason_emails_as_json(self):
"""Returns a json dictionary of every action needed reason and its associated email
for this particular domain request."""
emails = {}
for action_needed_reason in self.ActionNeededReasons:
enum_value = action_needed_reason.value
emails[enum_value] = self.get_action_needed_reason_default_email_text(enum_value)
return json.dumps(emails)
def get_action_needed_reason_default_email_text(self, action_needed_reason: str): def get_action_needed_reason_default_email_text(self, action_needed_reason: str):
"""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 == self.ActionNeededReasons.OTHER: if action_needed_reason is None or action_needed_reason == self.ActionNeededReasons.OTHER:

View file

@ -109,6 +109,19 @@ This is using a custom implementation fieldset.html (see admin/fieldset.html)
{% else %} {% else %}
<p>No changelog to display.</p> <p>No changelog to display.</p>
{% endif %} {% endif %}
{% comment %}
Store the action needed reason emails in a json-based dictionary.
This allows us to change the action_needed_reason_email field dynamically, depending on value.
The alternative to this is an API endpoint.
Given that we have a limited number of emails, doing it this way makes sense.
{% endcomment %}
{% if action_needed_reason_emails %}
<script id="action-needed-emails-data" type="application/json">
{{ action_needed_reason_emails|safe }}
</script>
{% endif %}
<div id="action_needed_reason_email_readonly" class="dja-readonly-textarea-container padding-1 margin-top-2 thin-border display-none"> <div id="action_needed_reason_email_readonly" class="dja-readonly-textarea-container padding-1 margin-top-2 thin-border display-none">
<label class="max-full" for="action_needed_reason_email_view_more"> <label class="max-full" for="action_needed_reason_email_view_more">
<strong>Auto-generated email (sent to submitter)</strong> <strong>Auto-generated email (sent to submitter)</strong>

View file

@ -24,7 +24,6 @@ SAMPLE_KWARGS = {
"object_id": "3", "object_id": "3",
"domain": "whitehouse.gov", "domain": "whitehouse.gov",
"user_pk": "1", "user_pk": "1",
"reason": "bad_name",
} }
# Our test suite will ignore some namespaces. # Our test suite will ignore some namespaces.

View file

@ -1,4 +1,3 @@
import logging
from django.http import JsonResponse from django.http import JsonResponse
from django.core.paginator import Paginator from django.core.paginator import Paginator
from registrar.models import DomainRequest from registrar.models import DomainRequest
@ -6,10 +5,6 @@ from django.utils.dateformat import format
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.urls import reverse from django.urls import reverse
from django.db.models import Q from django.db.models import Q
from django.core.exceptions import PermissionDenied
logger = logging.getLogger(__name__)
@login_required @login_required
@ -102,23 +97,3 @@ def get_domain_requests_json(request):
"unfiltered_total": unfiltered_total, "unfiltered_total": unfiltered_total,
} }
) )
@login_required
def get_action_needed_email(request, pk, reason):
"""
Given the primary key of a DomainRequest and the action_needed reason,
this will return the email that would be generated for the given user.
"""
# Q: Do we need both checks? I'd think we can just check on the group, right?
staff_or_superuser = request.user.is_staff or request.user.is_superuser
has_access = request.user.has_perm("registrar.full_access_permission") or request.user.has_perm(
"registrar.analyst_access_permission"
)
if staff_or_superuser and not has_access:
raise PermissionDenied("You do not have permission to access this resource.")
domain_request = DomainRequest.objects.filter(id=pk).first()
reason_dict = domain_request.get_action_needed_reason_default_email_text(reason)
return JsonResponse(reason_dict)