Display logic in admin

This commit is contained in:
zandercymatics 2024-08-22 14:11:28 -06:00
parent 056a3ecf36
commit 7fb186e24b
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
2 changed files with 38 additions and 0 deletions

View file

@ -7,6 +7,7 @@ from django import forms
from django.db.models import Value, CharField, Q from django.db.models import Value, CharField, Q
from django.db.models.functions import Concat, Coalesce from django.db.models.functions import Concat, Coalesce
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.conf import settings
from django.shortcuts import redirect from django.shortcuts import redirect
from django_fsm import get_available_FIELD_transitions, FSMField from django_fsm import get_available_FIELD_transitions, FSMField
from registrar.models.domain_information import DomainInformation from registrar.models.domain_information import DomainInformation
@ -306,6 +307,7 @@ class DomainRequestAdminForm(forms.ModelForm):
return cleaned_data return cleaned_data
def _check_for_valid_rejection_reason(self, rejection_reason) -> bool: def _check_for_valid_rejection_reason(self, rejection_reason) -> bool:
""" """
Checks if the rejection_reason field is not none. Checks if the rejection_reason field is not none.
@ -1914,6 +1916,19 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
else: else:
obj.action_needed_reason_email = default_email obj.action_needed_reason_email = default_email
if obj.status in DomainRequest.get_statuses_that_send_emails():
if not settings.IS_PRODUCTION:
profile_flag = flag_is_active(None, "profile_feature")
if profile_flag and hasattr(obj, "creator"):
recipient = obj.creator
elif not profile_flag and hasattr(obj, "submitter"):
recipient = obj.submitter
else
recipient = None
if recipient and recipient.email:
self._check_for_valid_email(request, recipient.email)
# == Handle status == # # == Handle status == #
if obj.status == original_obj.status: if obj.status == original_obj.status:
# If the status hasn't changed, let the base function take care of it # If the status hasn't changed, let the base function take care of it
@ -1926,6 +1941,17 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
if should_save: if should_save:
return super().save_model(request, obj, form, change) return super().save_model(request, obj, form, change)
def _check_for_valid_email(self, request, email):
"""Certain emails are whitelisted in non-production environments,
so we should display that information using this function.
"""
allowed = models.AllowedEmail.is_allowed_email(email)
error_message = f"Could not send email. The email '{email}' does not exist within the whitelist."
if not allowed:
messages.warning(request, error_message)
def _handle_status_change(self, request, obj, original_obj): def _handle_status_change(self, request, obj, original_obj):
""" """
Checks for various conditions when a status change is triggered. Checks for various conditions when a status change is triggered.

View file

@ -577,6 +577,18 @@ class DomainRequest(TimeStampedModel):
blank=True, blank=True,
) )
@classmethod
def get_statuses_that_send_emails(cls):
"""Returns a list of statuses that send an email to the user"""
excluded_statuses = [
cls.DomainRequestStatus.INELIGIBLE,
cls.DomainRequestStatus.IN_REVIEW
]
return [
status for status in cls.DomainRequestStatus
if status not in excluded_statuses
]
def sync_organization_type(self): def sync_organization_type(self):
""" """
Updates the organization_type (without saving) to match Updates the organization_type (without saving) to match