parameterize filters and sorts

This commit is contained in:
David Kennedy 2023-10-27 08:06:46 -04:00
parent 0b947ce754
commit 47251d9edb
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B
2 changed files with 30 additions and 36 deletions

View file

@ -752,25 +752,24 @@ 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')
response['Content-Disposition'] = 'attachment; filename="data_export.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="data_export.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="data_export.csv"'
response['Content-Disposition'] = 'attachment; filename="domains-current-federal.csv"'
csv_export.export_data_federal_to_csv(response)
return response

View file

@ -1,33 +1,31 @@
import csv
from registrar.models.domain import Domain
from registrar.models.domain_information import DomainInformation
from registrar.models.public_contact import PublicContact
# TODO pass sort order and filter as arguments rather than domains
def export_domains_to_writer(writer, domains, columns):
def export_domains_to_writer(writer, columns, sort_fields, filter_condition):
# 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)
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)
# create a dictionary of fields to include
# create a dictionary of fields which can be included in output
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,
'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': domain.state,
'Status': domainInfo.domain.state,
}
writer.writerow(
[FIELDS.get(column,'') for column in columns]
@ -53,9 +51,9 @@ def export_data_type_to_csv(csv_file):
'Status',
# 'Expiration Date'
]
# define domains to be exported
domains = Domain.objects.all().order_by('name')
export_domains_to_writer(writer, domains, columns)
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)
@ -69,10 +67,9 @@ def export_data_full_to_csv(csv_file):
'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)
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)
@ -86,8 +83,6 @@ def export_data_federal_to_csv(csv_file):
'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)
sort_fields = ['domain__name', 'federal_agency', 'organization_type']
filter_condition = {'organization_type__icontains': 'federal'}
export_domains_to_writer(writer, columns, sort_fields, filter_condition)