refactored some for separation of concerns, and fixing permissions in template

This commit is contained in:
David Kennedy 2023-10-27 05:55:52 -04:00
parent 5fe2827565
commit e4b3706d9c
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B
3 changed files with 74 additions and 59 deletions

View file

@ -1,4 +1,3 @@
import csv
import logging import logging
from django import forms from django import forms
from django.http import HttpResponse from django.http import HttpResponse
@ -11,8 +10,8 @@ from django.http.response import HttpResponseRedirect
from django.urls import reverse from django.urls import reverse
from epplibwrapper.errors import ErrorCode, RegistryError from epplibwrapper.errors import ErrorCode, RegistryError
from registrar.models.domain import Domain from registrar.models.domain import Domain
from registrar.models.domain_information import DomainInformation
from registrar.models.utility.admin_sort_fields import AdminSortFields from registrar.models.utility.admin_sort_fields import AdminSortFields
from registrar.utility import csv_export
from . import models from . import models
from auditlog.models import LogEntry # type: ignore from auditlog.models import LogEntry # type: ignore
from auditlog.admin import LogEntryAdmin # type: ignore from auditlog.admin import LogEntryAdmin # type: ignore
@ -757,73 +756,21 @@ class DomainAdmin(ListHeaderAdmin):
# match the CSV example with all the fields # match the CSV example with all the fields
response = HttpResponse(content_type='text/csv') response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="data_export.csv"' response['Content-Disposition'] = 'attachment; filename="data_export.csv"'
writer = csv.writer(response) csv_export.export_data_type_to_csv(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
return response return response
def export_data_full(self, request): def export_data_full(self, request):
# Smaller export based on 1 # Smaller export based on 1
response = HttpResponse(content_type='text/csv') response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="data_export.csv"' response['Content-Disposition'] = 'attachment; filename="data_export.csv"'
writer = csv.writer(response) csv_export.export_data_full_to_csv(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
return response return response
def export_data_federal(self, request): def export_data_federal(self, request):
# Federal only # Federal only
response = HttpResponse(content_type='text/csv') response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="data_export.csv"' response['Content-Disposition'] = 'attachment; filename="data_export.csv"'
writer = csv.writer(response) csv_export.export_data_federal_to_csv(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
return response return response
def get_urls(self): def get_urls(self):

View file

@ -9,6 +9,7 @@
Add Domain Add Domain
</a> </a>
</li> </li>
{% endif %}
<li> <li>
<a href="{% url 'admin:registrar_domain_export_data_type' %}" class="button">Export domains (type)</a> <a href="{% url 'admin:registrar_domain_export_data_type' %}" class="button">Export domains (type)</a>
</li> </li>
@ -18,6 +19,5 @@
<li> <li>
<a href="{% url 'admin:registrar_domain_export_data_federal' %}" class="button">Export domains (federal)</a> <a href="{% url 'admin:registrar_domain_export_data_federal' %}" class="button">Export domains (federal)</a>
</li> </li>
{% endif %}
</ul> </ul>
{% endblock %} {% endblock %}

View file

@ -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