diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 19d876728..3562383bd 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1812,8 +1812,70 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin): return response def change_view(self, request, object_id, form_url="", extra_context=None): + """Display restricted warning, + Setup the auditlog trail and pass it in extra context.""" obj = self.get_object(request, object_id) self.display_restricted_warning(request, obj) + + # Retrieve and order audit log entries by timestamp in ascending order + audit_log_entries = LogEntry.objects.filter(object_id=object_id).order_by("timestamp") + + # Initialize variables for tracking status changes and filtered entries + filtered_entries = [] + + # Process each log entry to filter based on the change criteria + for log_entry in audit_log_entries: + changes = log_entry.changes + status_changed = "status" in changes + rejection_reason_changed = "rejection_reason" in changes + action_needed_reason_changed = "action_needed_reason" in changes + + # Check if the log entry meets the filtering criteria + if status_changed or (not status_changed and (rejection_reason_changed or action_needed_reason_changed)): + entry = {} + + # Handle status change + if status_changed: + entry["status"] = DomainRequest.DomainRequestStatus(changes["status"][1]).label + # last_status = entry["status"] + + # Handle rejection reason change + if rejection_reason_changed: + rejection_reason = changes["rejection_reason"][1] + entry["rejection_reason"] = ( + "" if rejection_reason == "None" else DomainRequest.RejectionReasons(rejection_reason).label + ) + # Handle case where rejection reason changed but not status + if not status_changed: + entry["status"] = DomainRequest.DomainRequestStatus.REJECTED.label + + # Handle action needed reason change + if action_needed_reason_changed: + action_needed_reason = changes["action_needed_reason"][1] + entry["action_needed_reason"] = ( + "" + if action_needed_reason == "None" + else DomainRequest.ActionNeededReasons(action_needed_reason).label + ) + # Handle case where action needed reason changed but not status + if not status_changed: + entry["status"] = DomainRequest.DomainRequestStatus.ACTION_NEEDED.label + + # Add actor and timestamp information + entry["actor"] = log_entry.actor + entry["timestamp"] = log_entry.timestamp + + # Append the filtered entry to the list + filtered_entries.append(entry) + + # Reverse the filtered entries list to get newest to oldest order + filtered_entries.reverse() + + # Initialize extra_context and add filtered entries + if extra_context is None: + extra_context = {} + extra_context["filtered_entries"] = filtered_entries + return super().change_view(request, object_id, form_url, extra_context) diff --git a/src/registrar/models/domain_request.py b/src/registrar/models/domain_request.py index 8dfa419c9..a403f3256 100644 --- a/src/registrar/models/domain_request.py +++ b/src/registrar/models/domain_request.py @@ -17,8 +17,6 @@ from .utility.time_stamped_model import TimeStampedModel from ..utility.email import send_templated_email, EmailSendingError from itertools import chain -from auditlog.models import AuditlogHistoryField # type: ignore - logger = logging.getLogger(__name__) @@ -35,11 +33,7 @@ class DomainRequest(TimeStampedModel): ] # https://django-auditlog.readthedocs.io/en/latest/usage.html#object-history - # If we note any performace degradation due to this addition, - # we can query the auditlogs table in admin.py and add the results to - # extra_context in the change_view method for DomainRequestAdmin. - # This is the more straightforward way so trying it first. - history = AuditlogHistoryField() + # history = AuditlogHistoryField() # Constants for choice fields class DomainRequestStatus(models.TextChoices): diff --git a/src/registrar/templates/django/admin/includes/detail_table_fieldset.html b/src/registrar/templates/django/admin/includes/detail_table_fieldset.html index 791753042..46c4f56ea 100644 --- a/src/registrar/templates/django/admin/includes/detail_table_fieldset.html +++ b/src/registrar/templates/django/admin/includes/detail_table_fieldset.html @@ -68,51 +68,52 @@ This is using a custom implementation fieldset.html (see admin/fieldset.html) {% endblock field_readonly %} {% block after_help_text %} - {% if field.field.name == "status" and original_object.history.count > 0 %} -
Status | +User | +Changed at | +
---|---|---|
Status | -User | -Changed at | -
{{ value.1|default:"None" }} - {% for rk, rv in log_entry.changes_display_dict.items %} - {% if rk == "rejection reason" %} - - {{ rv.1|default:"None" }} - {% endif %} - {% endfor %} - | -- {{ log_entry.actor|default:"None" }} - - | -{{ log_entry.timestamp|date:"Y-m-d H:i:s" }} | -+ {% if entry.status %} + {{ entry.status|default:"None" }} + {% else %} + Error {% endif %} - {% endfor %} - {% endfor %} - | -