From 0b947ce75427cbe8c3f980a4e3d94a7334f48f92 Mon Sep 17 00:00:00 2001 From: David Kennedy Date: Fri, 27 Oct 2023 06:58:11 -0400 Subject: [PATCH] modularization of reporting functions --- src/registrar/admin.py | 1 + src/registrar/utility/csv_export.py | 129 +++++++++++++++++----------- 2 files changed, 78 insertions(+), 52 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index c4ff83214..e61ddc38e 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -752,6 +752,7 @@ class DomainAdmin(ListHeaderAdmin): change_list_template = "django/admin/domain_change_list.html" readonly_fields = ["state"] + # TODO file names and function names specific to report type def export_data_type(self, request): # match the CSV example with all the fields response = HttpResponse(content_type='text/csv') diff --git a/src/registrar/utility/csv_export.py b/src/registrar/utility/csv_export.py index c7fb9a0f1..ac28cd61d 100644 --- a/src/registrar/utility/csv_export.py +++ b/src/registrar/utility/csv_export.py @@ -3,66 +3,91 @@ from registrar.models.domain import Domain from registrar.models.domain_information import DomainInformation from registrar.models.public_contact import PublicContact -def export_data_type_to_csv(csv_file): - writer = csv.writer(csv_file) - # Write your data to the CSV here - writer.writerow( - [ - 'Domain name', - 'Domain type', - 'Federal agency', - 'Organization name', - 'City', - 'State', - 'AO', - 'AO email', - 'Submitter', - 'Submitter title', - 'Submitter email', - 'Submitter phone', - 'Security Contact Email', - 'Status', - # 'Expiration Date' - ] - ) # Include the appropriate headers - # Loop through and write your data rows +# TODO pass sort order and filter as arguments rather than domains +def export_domains_to_writer(writer, domains, columns): + # write columns headers to writer + writer.writerow(columns) + for domain in Domain.objects.all().order_by('name'): domain_information, _ = DomainInformation.objects.get_or_create(domain=domain) security_contacts = domain.contacts.filter(contact_type=PublicContact.ContactTypeChoices.SECURITY) + # create a dictionary of fields to include + FIELDS = { + 'Domain name': domain.name, + 'Domain type': domain_information.federal_type, + 'Federal agency': domain_information.federal_agency, + 'Organization name': domain_information.organization_name, + 'City': domain_information.city, + 'State': domain_information.state_territory, + 'AO': domain_information.authorizing_official.first_name + " " + domain_information.authorizing_official.last_name, + 'AO email': domain_information.authorizing_official.email, + 'Submitter': domain_information.submitter.first_name + " " + domain_information.submitter.last_name, + 'Submitter title': domain_information.submitter.title, + 'Submitter email': domain_information.submitter.email, + 'Submitter phone': domain_information.submitter.phone, + 'Security Contact Email': security_contacts[0].email if security_contacts.exists() else " ", + 'Status': domain.state, + } writer.writerow( - [ - domain.name, - domain_information.federal_type, - domain_information.federal_agency, - domain_information.organization_name, - domain_information.city, - domain_information.state_territory, - domain_information.authorizing_official.first_name + " " + domain_information.authorizing_official.last_name, - domain_information.authorizing_official.email, - domain_information.submitter.first_name + " " + domain_information.submitter.last_name, - domain_information.submitter.title, - domain_information.submitter.email, - domain_information.submitter.phone, - security_contacts[0].email if security_contacts.exists() else " ", - # domain.contacts.all().filter(contact_type=PublicContact.ContactTypeChoices.SECURITY)[0].email if len(domain.contacts.all().filter(contact_type=PublicContact.ContactTypeChoices.SECURITY)) else " ", - domain.state, - # domain.expiration_date, - ] - ) # Include the appropriate fields + [FIELDS.get(column,'') for column in columns] + ) + +def export_data_type_to_csv(csv_file): + writer = csv.writer(csv_file) + # define columns to include in export + columns = [ + 'Domain name', + 'Domain type', + 'Federal agency', + 'Organization name', + 'City', + 'State', + 'AO', + 'AO email', + 'Submitter', + 'Submitter title', + 'Submitter email', + 'Submitter phone', + 'Security Contact Email', + 'Status', + # 'Expiration Date' + ] + # define domains to be exported + domains = Domain.objects.all().order_by('name') + export_domains_to_writer(writer, domains, columns) def export_data_full_to_csv(csv_file): writer = csv.writer(csv_file) - # Write your data to the CSV here - writer.writerow(['Name', 'State', ...]) # Include the appropriate headers - # Loop through and write your data rows - for data_row in Domain.objects.all(): - writer.writerow([data_row.name, data_row.state, ...]) # Include the appropriate fields + # define columns to include in export + columns = [ + 'Domain name', + 'Domain type', + 'Federal agency', + 'Organization name', + 'City', + 'State', + 'Security Contact Email', + ] + # define domains to be exported + # TODO order by fields in domain information + domains = Domain.objects.all().order_by('name') + export_domains_to_writer(writer, domains, columns) def export_data_federal_to_csv(csv_file): writer = csv.writer(csv_file) - # Write your data to the CSV here - writer.writerow(['Name', 'State', ...]) # Include the appropriate headers - # Loop through and write your data rows - for data_row in Domain.objects.all(): - writer.writerow([data_row.name, data_row.state, ...]) # Include the appropriate fields \ No newline at end of file + # define columns to include in export + columns = [ + 'Domain name', + 'Domain type', + 'Federal agency', + 'Organization name', + 'City', + 'State', + 'Security Contact Email', + ] + # define domains to be exported + # TODO order by fields in domain information + # TODO filter by domain type + domains = Domain.objects.all().order_by('name') + export_domains_to_writer(writer, domains, columns) \ No newline at end of file