diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 3b641d9df..53ca897be 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -751,39 +751,55 @@ class DomainAdmin(ListHeaderAdmin): change_form_template = "django/admin/domain_change_form.html" change_list_template = "django/admin/domain_change_list.html" readonly_fields = ["state"] - + def export_data_type(self, request): # match the CSV example with all the fields - response = HttpResponse(content_type='text/csv') - response['Content-Disposition'] = 'attachment; filename="domains-by-type.csv"' + response = HttpResponse(content_type="text/csv") + response["Content-Disposition"] = 'attachment; filename="domains-by-type.csv"' csv_export.export_data_type_to_csv(response) return response def export_data_full(self, request): # Smaller export based on 1 - response = HttpResponse(content_type='text/csv') - response['Content-Disposition'] = 'attachment; filename="domains-current-full.csv"' + response = HttpResponse(content_type="text/csv") + response[ + "Content-Disposition" + ] = 'attachment; filename="domains-current-full.csv"' csv_export.export_data_full_to_csv(response) return response - + def export_data_federal(self, request): # Federal only - response = HttpResponse(content_type='text/csv') - response['Content-Disposition'] = 'attachment; filename="domains-current-federal.csv"' + response = HttpResponse(content_type="text/csv") + response[ + "Content-Disposition" + ] = 'attachment; filename="domains-current-federal.csv"' csv_export.export_data_federal_to_csv(response) return response - + def get_urls(self): from django.urls import path - + urlpatterns = super().get_urls() info = self.model._meta.app_label, self.model._meta.model_name my_url = [ - path('export_data_type/', self.export_data_type, name='%s_%s_export_data_type' % info), - path('export_data_full/', self.export_data_full, name='%s_%s_export_data_full' % info), - path('export_data_federal/', self.export_data_federal, name='%s_%s_export_data_federal' % info), + path( + "export_data_type/", + self.export_data_type, + name="%s_%s_export_data_type" % info, + ), + path( + "export_data_full/", + self.export_data_full, + name="%s_%s_export_data_full" % info, + ), + path( + "export_data_federal/", + self.export_data_federal, + name="%s_%s_export_data_federal" % info, + ), ] return my_url + urlpatterns diff --git a/src/registrar/utility/csv_export.py b/src/registrar/utility/csv_export.py index ff60f8ff3..fbb2325ff 100644 --- a/src/registrar/utility/csv_export.py +++ b/src/registrar/utility/csv_export.py @@ -2,87 +2,99 @@ import csv from registrar.models.domain_information import DomainInformation from registrar.models.public_contact import PublicContact + def export_domains_to_writer(writer, columns, sort_fields, filter_condition): # write columns headers to writer writer.writerow(columns) - domainInfos = DomainInformation.objects.filter(**filter_condition).order_by(*sort_fields) + domainInfos = DomainInformation.objects.filter(**filter_condition).order_by( + *sort_fields + ) for domainInfo in domainInfos: - security_contacts = domainInfo.domain.contacts.filter(contact_type=PublicContact.ContactTypeChoices.SECURITY) + security_contacts = domainInfo.domain.contacts.filter( + contact_type=PublicContact.ContactTypeChoices.SECURITY + ) # create a dictionary of fields which can be included in output FIELDS = { - 'Domain name': domainInfo.domain.name, - 'Domain type': domainInfo.organization_type, - 'Federal agency': domainInfo.federal_agency, - 'Organization name': domainInfo.organization_name, - 'City': domainInfo.city, - 'State': domainInfo.state_territory, - 'AO': domainInfo.authorizing_official.first_name + " " + domainInfo.authorizing_official.last_name, - 'AO email': domainInfo.authorizing_official.email, - 'Submitter': domainInfo.submitter.first_name + " " + domainInfo.submitter.last_name, - 'Submitter title': domainInfo.submitter.title, - 'Submitter email': domainInfo.submitter.email, - 'Submitter phone': domainInfo.submitter.phone, - 'Security Contact Email': security_contacts[0].email if security_contacts.exists() else " ", - 'Status': domainInfo.domain.state, + "Domain name": domainInfo.domain.name, + "Domain type": domainInfo.organization_type, + "Federal agency": domainInfo.federal_agency, + "Organization name": domainInfo.organization_name, + "City": domainInfo.city, + "State": domainInfo.state_territory, + "AO": domainInfo.authorizing_official.first_name + + " " + + domainInfo.authorizing_official.last_name, + "AO email": domainInfo.authorizing_official.email, + "Submitter": domainInfo.submitter.first_name + + " " + + domainInfo.submitter.last_name, + "Submitter title": domainInfo.submitter.title, + "Submitter email": domainInfo.submitter.email, + "Submitter phone": domainInfo.submitter.phone, + "Security Contact Email": security_contacts[0].email + if security_contacts.exists() + else " ", + "Status": domainInfo.domain.state, } - writer.writerow( - [FIELDS.get(column,'') for column in columns] - ) + writer.writerow([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', + "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' ] - sort_fields = ['domain__name'] + sort_fields = ["domain__name"] filter_condition = {} export_domains_to_writer(writer, columns, sort_fields, filter_condition) + def export_data_full_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', - 'Security Contact Email', + "Domain name", + "Domain type", + "Federal agency", + "Organization name", + "City", + "State", + "Security Contact Email", ] - sort_fields = ['domain__name', 'federal_agency', 'organization_type'] + sort_fields = ["domain__name", "federal_agency", "organization_type"] filter_condition = {} export_domains_to_writer(writer, columns, sort_fields, filter_condition) + def export_data_federal_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', - 'Security Contact Email', + "Domain name", + "Domain type", + "Federal agency", + "Organization name", + "City", + "State", + "Security Contact Email", ] - sort_fields = ['domain__name', 'federal_agency', 'organization_type'] - filter_condition = {'organization_type__icontains': 'federal'} - export_domains_to_writer(writer, columns, sort_fields, filter_condition) \ No newline at end of file + sort_fields = ["domain__name", "federal_agency", "organization_type"] + filter_condition = {"organization_type__icontains": "federal"} + export_domains_to_writer(writer, columns, sort_fields, filter_condition)