Skip to content

Commit

Permalink
Merge pull request #2869 from cisagov/hotgov/2355-rejection-reason-em…
Browse files Browse the repository at this point in the history
…ails

#2355: Custom rejection reason emails - [BACKUP] - MIGRATION
  • Loading branch information
zandercymatics authored Oct 10, 2024
2 parents 3db0388 + 8b70554 commit a1d9904
Show file tree
Hide file tree
Showing 15 changed files with 773 additions and 344 deletions.
43 changes: 20 additions & 23 deletions src/registrar/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
from django.db.models import Value, CharField, Q
from django.db.models.functions import Concat, Coalesce
from django.http import HttpResponseRedirect
from registrar.utility.admin_helpers import (
get_action_needed_reason_default_email,
get_rejection_reason_default_email,
get_field_links_as_list,
)
from django.conf import settings
from django.shortcuts import redirect
from django_fsm import get_available_FIELD_transitions, FSMField
Expand All @@ -20,11 +25,6 @@
from registrar.models.user_domain_role import UserDomainRole
from waffle.admin import FlagAdmin
from waffle.models import Sample, Switch
from registrar.utility.admin_helpers import (
get_all_action_needed_reason_emails,
get_action_needed_reason_default_email,
get_field_links_as_list,
)
from registrar.models import Contact, Domain, DomainRequest, DraftDomain, User, Website, SeniorOfficial
from registrar.utility.constants import BranchChoices
from registrar.utility.errors import FSMDomainRequestError, FSMErrorCodes
Expand Down Expand Up @@ -237,6 +237,7 @@ class Meta:
}
labels = {
"action_needed_reason_email": "Email",
"rejection_reason_email": "Email",
}

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -1750,6 +1751,7 @@ def status_history(self, obj):
"status_history",
"status",
"rejection_reason",
"rejection_reason_email",
"action_needed_reason",
"action_needed_reason_email",
"investigator",
Expand Down Expand Up @@ -1905,25 +1907,11 @@ def save_model(self, request, obj, form, change):
# Get the original domain request from the database.
original_obj = models.DomainRequest.objects.get(pk=obj.pk)

# == Handle action_needed_reason == #

reason_changed = obj.action_needed_reason != original_obj.action_needed_reason
if reason_changed:
# Track the fact that we sent out an email
request.session["action_needed_email_sent"] = True

# Set the action_needed_reason_email to the default if nothing exists.
# Since this check occurs after save, if the user enters a value then we won't update.

default_email = get_action_needed_reason_default_email(request, obj, obj.action_needed_reason)
if obj.action_needed_reason_email:
emails = get_all_action_needed_reason_emails(request, obj)
is_custom_email = obj.action_needed_reason_email not in emails.values()
if not is_custom_email:
obj.action_needed_reason_email = default_email
else:
obj.action_needed_reason_email = default_email
# == Handle action needed and rejected emails == #
# Edge case: this logic is handled by javascript, so contexts outside that must be handled
obj = self._handle_custom_emails(obj)

# == Handle allowed emails == #
if obj.status in DomainRequest.get_statuses_that_send_emails() and not settings.IS_PRODUCTION:
self._check_for_valid_email(request, obj)

Expand All @@ -1939,6 +1927,15 @@ def save_model(self, request, obj, form, change):
if should_save:
return super().save_model(request, obj, form, change)

def _handle_custom_emails(self, obj):
if obj.status == DomainRequest.DomainRequestStatus.ACTION_NEEDED:
if obj.action_needed_reason and not obj.action_needed_reason_email:
obj.action_needed_reason_email = get_action_needed_reason_default_email(obj, obj.action_needed_reason)
elif obj.status == DomainRequest.DomainRequestStatus.REJECTED:
if obj.rejection_reason and not obj.rejection_reason_email:
obj.rejection_reason_email = get_rejection_reason_default_email(obj, obj.rejection_reason)
return obj

def _check_for_valid_email(self, request, obj):
"""Certain emails are whitelisted in non-production environments,
so we should display that information using this function.
Expand Down
Loading

0 comments on commit a1d9904

Please sign in to comment.