From f054cfa5cf3144fdaf8c73573177a2a78afca82c Mon Sep 17 00:00:00 2001 From: matthewswspence Date: Fri, 16 Aug 2024 15:35:58 -0500 Subject: [PATCH] First complete pass --- src/registrar/admin.py | 6 ++- .../commands/populate_domain_request_dates.py | 29 +++++++++++-- .../commands/utility/terminal_helper.py | 2 +- ...0118_add_domainrequest_submission_dates.py | 43 +++++++++++++++++++ src/registrar/models/domain_request.py | 9 ++-- 5 files changed, 78 insertions(+), 11 deletions(-) create mode 100644 src/registrar/migrations/0118_add_domainrequest_submission_dates.py diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 18c1052fc..3f3547341 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1649,7 +1649,9 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin): # Columns list_display = [ "requested_domain", - "submission_date", + "first_submitted_date", + "last_submitted_date", + "last_status_update", "status", "generic_org_type", "federal_type", @@ -1852,7 +1854,7 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin): # Table ordering # NOTE: This impacts the select2 dropdowns (combobox) # Currentl, there's only one for requests on DomainInfo - ordering = ["-submission_date", "requested_domain__name"] + ordering = ["-last_submitted_date", "requested_domain__name"] change_form_template = "django/admin/domain_request_change_form.html" diff --git a/src/registrar/management/commands/populate_domain_request_dates.py b/src/registrar/management/commands/populate_domain_request_dates.py index 7ea898c4c..90fc06dcf 100644 --- a/src/registrar/management/commands/populate_domain_request_dates.py +++ b/src/registrar/management/commands/populate_domain_request_dates.py @@ -2,20 +2,41 @@ import logging from django.core.management import BaseCommand from registrar.management.commands.utility.terminal_helper import PopulateScriptTemplate, TerminalColors from registrar.models import DomainRequest +from auditlog.models import LogEntry +from django.core.exceptions import ObjectDoesNotExist logger = logging.getLogger(__name__) class Command(BaseCommand, PopulateScriptTemplate): - help = "Loops through each valid domain request object and populates the last_status_update and first_submitted_date" + help = "Loops through each domain request object and populates the last_status_update and first_submitted_date" def handle(self, **kwargs): - """Loops through each valid DomainRequest object and populates its last_status_update and first_submitted_date values""" - self.mass_update_records(DomainRequest, ["last_status_update", "last_submitted_date"]) + """Loops through each DomainRequest object and populates its last_status_update and first_submitted_date values""" + self.mass_update_records(DomainRequest, None, ["last_status_update", "first_submitted_date"]) def update_record(self, record: DomainRequest): """Defines how we update the first_submitted_date and last_status_update fields""" - record.set_dates() + try: + # Retrieve and order audit log entries by timestamp in descending order + audit_log_entries = LogEntry.objects.filter(object_pk=record.pk).order_by("-timestamp") + + # Loop through logs in descending order to find most recent status change + for log_entry in audit_log_entries: + if "status" in log_entry.changes: + record.last_status_update = log_entry.timestamp.date() + break + + # Loop through logs in ascending order to find first submission + for log_entry in audit_log_entries.reverse(): + if log_entry.changes_dict['status'](1) == 'Submitted': + record.first_submitted_date = log_entry.timestamp.date() + + except ObjectDoesNotExist as e: + logger.error(f"Object with object_pk {record.pk} does not exist: {e}") + except Exception as e: + logger.error(f"An error occurred during update_record: {e}") + logger.info( f"{TerminalColors.OKCYAN}Updating {record} => first submitted date: " f"{record.first_submitted_date}{TerminalColors.OKCYAN}, last status update:" f"{record.last_status_update}{TerminalColors.OKCYAN}" ) diff --git a/src/registrar/management/commands/utility/terminal_helper.py b/src/registrar/management/commands/utility/terminal_helper.py index 2c69e1080..b9e11be5d 100644 --- a/src/registrar/management/commands/utility/terminal_helper.py +++ b/src/registrar/management/commands/utility/terminal_helper.py @@ -86,7 +86,7 @@ class PopulateScriptTemplate(ABC): You must define update_record before you can use this function. """ - records = object_class.objects.filter(**filter_conditions) + records = object_class.objects.filter(**filter_conditions) if filter_conditions else object_class.objects.all() readable_class_name = self.get_class_name(object_class) # Code execution will stop here if the user prompts "N" diff --git a/src/registrar/migrations/0118_add_domainrequest_submission_dates.py b/src/registrar/migrations/0118_add_domainrequest_submission_dates.py new file mode 100644 index 000000000..c971b2f9a --- /dev/null +++ b/src/registrar/migrations/0118_add_domainrequest_submission_dates.py @@ -0,0 +1,43 @@ +# Generated by Django 4.2.10 on 2024-08-16 15:28 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("registrar", "0117_alter_portfolioinvitation_portfolio_additional_permissions_and_more"), + ] + + operations = [ + migrations.RenameField( + model_name="domainrequest", + old_name="submission_date", + new_name="last_submitted_date", + ), + migrations.AlterField( + model_name="domainrequest", + name="last_submitted_date", + field=models.DateField( + blank=True, default=None, help_text="Date last submitted", null=True, verbose_name="last submitted on" + ), + ), + migrations.AddField( + model_name="domainrequest", + name="first_submitted_date", + field=models.DateField( + blank=True, default=None, help_text="Date initially submitted", null=True, verbose_name="first submitted on" + ), + ), + migrations.AddField( + model_name="domainrequest", + name="last_status_update", + field=models.DateField( + blank=True, + default=None, + help_text="Date of last status updated", + null=True, + verbose_name="last updated on", + ), + ), + ] diff --git a/src/registrar/models/domain_request.py b/src/registrar/models/domain_request.py index d2c90243c..e9711f0ae 100644 --- a/src/registrar/models/domain_request.py +++ b/src/registrar/models/domain_request.py @@ -568,7 +568,7 @@ class DomainRequest(TimeStampedModel): null=True, blank=True, default=None, - verbose_name="submitted at", + verbose_name="first submitted on", help_text="Date initially submitted", ) @@ -577,7 +577,7 @@ class DomainRequest(TimeStampedModel): null=True, blank=True, default=None, - verbose_name="submitted at", + verbose_name="last submitted on", help_text="Date last submitted", ) @@ -586,7 +586,7 @@ class DomainRequest(TimeStampedModel): null=True, blank=True, default=None, - verbose_name="last updated at", + verbose_name="last updated on", help_text="Date of last status updated", ) notes = models.TextField( @@ -816,7 +816,8 @@ class DomainRequest(TimeStampedModel): DraftDomain = apps.get_model("registrar.DraftDomain") if not DraftDomain.string_could_be_domain(self.requested_domain.name): raise ValueError("Requested domain is not a valid domain name.") - + + # if the domain has not been submitted before this must be the first time if not self.first_submitted_date: self.first_submitted_date = timezone.now().date()