diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 364aaf04a..c4ff83214 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1,4 +1,3 @@ -import csv import logging from django import forms from django.http import HttpResponse @@ -11,8 +10,8 @@ from django.http.response import HttpResponseRedirect from django.urls import reverse from epplibwrapper.errors import ErrorCode, RegistryError from registrar.models.domain import Domain -from registrar.models.domain_information import DomainInformation from registrar.models.utility.admin_sort_fields import AdminSortFields +from registrar.utility import csv_export from . import models from auditlog.models import LogEntry # type: ignore from auditlog.admin import LogEntryAdmin # type: ignore @@ -757,73 +756,21 @@ class DomainAdmin(ListHeaderAdmin): # match the CSV example with all the fields response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="data_export.csv"' - writer = csv.writer(response) - # 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 - for domain in Domain.objects.all(): - domain_information, _ = DomainInformation.objects.get_or_create(domain=domain) - 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, - domain.security_contact.email if domain.security_contact else " ", - domain.state, - # domain.expiration_date, - ] - ) # Include the appropriate fields + 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="data_export.csv"' - writer = csv.writer(response) - # 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 + 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="data_export.csv"' - writer = csv.writer(response) - # 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 + csv_export.export_data_federal_to_csv(response) return response def get_urls(self): diff --git a/src/registrar/templates/django/admin/domain_change_list.html b/src/registrar/templates/django/admin/domain_change_list.html index e3a142477..9eb624891 100644 --- a/src/registrar/templates/django/admin/domain_change_list.html +++ b/src/registrar/templates/django/admin/domain_change_list.html @@ -9,6 +9,7 @@ Add Domain + {% endif %}
  • Export domains (type)
  • @@ -18,6 +19,5 @@
  • Export domains (federal)
  • - {% endif %} {% endblock %} \ No newline at end of file diff --git a/src/registrar/utility/csv_export.py b/src/registrar/utility/csv_export.py new file mode 100644 index 000000000..c7fb9a0f1 --- /dev/null +++ b/src/registrar/utility/csv_export.py @@ -0,0 +1,68 @@ +import csv +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 + 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) + + 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 + +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 + +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