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 %}