This commit is contained in:
Rachid Mrad 2023-10-27 15:03:34 -04:00
parent 98a9604ef3
commit 4803aaf971
No known key found for this signature in database
GPG key ID: EF38E4CEC4A8F3CF
3 changed files with 59 additions and 51 deletions

View file

@ -762,18 +762,14 @@ class DomainAdmin(ListHeaderAdmin):
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[ response["Content-Disposition"] = 'attachment; filename="current-full.csv"'
"Content-Disposition"
] = 'attachment; filename="current-full.csv"'
csv_export.export_data_full_to_csv(response) csv_export.export_data_full_to_csv(response)
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[ response["Content-Disposition"] = 'attachment; filename="current-federal.csv"'
"Content-Disposition"
] = 'attachment; filename="current-federal.csv"'
csv_export.export_data_federal_to_csv(response) csv_export.export_data_federal_to_csv(response)
return response return response

View file

@ -1,21 +1,15 @@
from django.test import TestCase from django.test import TestCase
from django.core.files import File
from io import StringIO from io import StringIO
import csv import csv
from registrar.models.domain_information import DomainInformation from registrar.models.domain_information import DomainInformation
from registrar.models.domain import Domain from registrar.models.domain import Domain
from registrar.models.user import User from registrar.models.user import User
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from registrar.utility.csv_export import ( from registrar.utility.csv_export import export_domains_to_writer
export_domains_to_writer,
export_data_type_to_csv,
export_data_full_to_csv,
export_data_federal_to_csv,
)
class ExportDataTest(TestCase): class ExportDataTest(TestCase):
def setUp(self): def setUp(self):
username = "test_user" username = "test_user"
first_name = "First" first_name = "First"
last_name = "Last" last_name = "Last"
@ -24,12 +18,22 @@ class ExportDataTest(TestCase):
username=username, first_name=first_name, last_name=last_name, email=email username=username, first_name=first_name, last_name=last_name, email=email
) )
self.domain_1, _ = Domain.objects.get_or_create(name="cdomain1.gov", state=Domain.State.READY) self.domain_1, _ = Domain.objects.get_or_create(
self.domain_2, _ = Domain.objects.get_or_create(name="adomain2.gov", state=Domain.State.READY) name="cdomain1.gov", state=Domain.State.READY
self.domain_3, _ = Domain.objects.get_or_create(name="ddomain3.gov", state=Domain.State.READY) )
self.domain_4, _ = Domain.objects.get_or_create(name="bdomain4.gov", state=Domain.State.UNKNOWN) self.domain_2, _ = Domain.objects.get_or_create(
self.domain_4, _ = Domain.objects.get_or_create(name="bdomain4.gov", state=Domain.State.UNKNOWN) name="adomain2.gov", state=Domain.State.READY
)
self.domain_3, _ = Domain.objects.get_or_create(
name="ddomain3.gov", state=Domain.State.READY
)
self.domain_4, _ = Domain.objects.get_or_create(
name="bdomain4.gov", state=Domain.State.UNKNOWN
)
self.domain_4, _ = Domain.objects.get_or_create(
name="bdomain4.gov", state=Domain.State.UNKNOWN
)
self.domain_information_1, _ = DomainInformation.objects.get_or_create( self.domain_information_1, _ = DomainInformation.objects.get_or_create(
creator=self.user, creator=self.user,
domain=self.domain_1, domain=self.domain_1,
@ -53,14 +57,13 @@ class ExportDataTest(TestCase):
organization_type="federal", organization_type="federal",
federal_agency="Armed Forces Retirement Home", federal_agency="Armed Forces Retirement Home",
) )
def tearDown(self): def tearDown(self):
Domain.objects.all().delete() Domain.objects.all().delete()
DomainInformation.objects.all().delete() DomainInformation.objects.all().delete()
User.objects.all().delete() User.objects.all().delete()
super().tearDown() super().tearDown()
def test_export_domains_to_writer(self): def test_export_domains_to_writer(self):
"""Test that export_domains_to_writer returns the """Test that export_domains_to_writer returns the
existing domain, test that sort by domain name works, existing domain, test that sort by domain name works,
@ -68,7 +71,7 @@ class ExportDataTest(TestCase):
# Create a CSV file in memory # Create a CSV file in memory
csv_file = StringIO() csv_file = StringIO()
writer = csv.writer(csv_file) writer = csv.writer(csv_file)
# Define columns, sort fields, and filter condition # Define columns, sort fields, and filter condition
columns = [ columns = [
"Domain name", "Domain name",
@ -94,33 +97,36 @@ class ExportDataTest(TestCase):
# Reset the CSV file's position to the beginning # Reset the CSV file's position to the beginning
csv_file.seek(0) csv_file.seek(0)
# Read the content into a variable # Read the content into a variable
csv_content = csv_file.read() csv_content = csv_file.read()
# We expect READY domains, # We expect READY domains,
# sorted alphabetially by domain name # sorted alphabetially by domain name
expected_content = """\ expected_content = (
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,"
adomain2.gov,interstate,,,,, , , , , , , ,ready "AO email, Submitter,Submitter title,Submitter email,Submitter phone,"
cdomain1.gov,federal,World War I Centennial Commission,,,, , , , , , , ,ready "Security Contact Email,Status\n"
ddomain3.gov,federal,Armed Forces Retirement Home,,,, , , , , , , ,ready "adomain2.gov,interstate,,,,, , , , , , , ,ready\n"
""" "cdomain1.gov,federal,World War I Centennial Commission,,,"
", , , , , , , ,ready\n"
"ddomain3.gov,federal,Armed Forces Retirement Home,,,, , , , , , , ,ready\n"
)
# print(csv_content) # print(csv_content)
# self.maxDiff = None # self.maxDiff = None
# Normalize line endings and remove leading/trailing whitespace # Normalize line endings and remove leading/trailing whitespace
csv_content = csv_content.replace('\r\n', '\n').strip() csv_content = csv_content.replace("\r\n", "\n").strip()
expected_content = expected_content.strip() expected_content = expected_content.strip()
self.assertEqual(csv_content, expected_content) self.assertEqual(csv_content, expected_content)
def test_export_domains_to_writer_2(self): def test_export_domains_to_writer_2(self):
"""An additional test for filters and multi-column sort""" """An additional test for filters and multi-column sort"""
# Create a CSV file in memory # Create a CSV file in memory
csv_file = StringIO() csv_file = StringIO()
writer = csv.writer(csv_file) writer = csv.writer(csv_file)
# Define columns, sort fields, and filter condition # Define columns, sort fields, and filter condition
columns = [ columns = [
"Domain name", "Domain name",
@ -132,31 +138,34 @@ ddomain3.gov,federal,Armed Forces Retirement Home,,,, , , , , , , ,ready
"Security Contact Email", "Security Contact Email",
] ]
sort_fields = ["domain__name", "federal_agency", "organization_type"] sort_fields = ["domain__name", "federal_agency", "organization_type"]
filter_condition = {"organization_type__icontains": "federal", "domain__state": Domain.State.READY} filter_condition = {
"organization_type__icontains": "federal",
"domain__state": Domain.State.READY,
}
# Call the export function # Call the export function
export_domains_to_writer(writer, columns, sort_fields, filter_condition) export_domains_to_writer(writer, columns, sort_fields, filter_condition)
# Reset the CSV file's position to the beginning # Reset the CSV file's position to the beginning
csv_file.seek(0) csv_file.seek(0)
# Read the content into a variable # Read the content into a variable
csv_content = csv_file.read() csv_content = csv_file.read()
# We expect READY domains, # We expect READY domains,
# federal only # federal only
# sorted alphabetially by domain name # sorted alphabetially by domain name
expected_content = """\ expected_content = (
Domain name,Domain type,Federal agency,Organization name,City,State,Security Contact Email "Domain name,Domain type,Federal agency,Organization name,City,"
cdomain1.gov,federal,World War I Centennial Commission,,,, "State,Security Contact Email\n"
ddomain3.gov,federal,Armed Forces Retirement Home,,,, "cdomain1.gov,federal,World War I Centennial Commission,,,,\n"
""" "ddomain3.gov,federal,Armed Forces Retirement Home,,,,\n"
)
# print(csv_content) # print(csv_content)
# self.maxDiff = None # self.maxDiff = None
# Normalize line endings and remove leading/trailing whitespace
csv_content = csv_content.replace('\r\n', '\n').strip()
expected_content = expected_content.strip()
self.assertEqual(csv_content, expected_content)
# Normalize line endings and remove leading/trailing whitespace
csv_content = csv_content.replace("\r\n", "\n").strip()
expected_content = expected_content.strip()
self.assertEqual(csv_content, expected_content)

View file

@ -109,5 +109,8 @@ def export_data_federal_to_csv(csv_file):
"Security Contact Email", "Security Contact Email",
] ]
sort_fields = ["domain__name", "federal_agency", "organization_type"] sort_fields = ["domain__name", "federal_agency", "organization_type"]
filter_condition = {"organization_type__icontains": "federal", "domain__state": Domain.State.READY} filter_condition = {
"organization_type__icontains": "federal",
"domain__state": Domain.State.READY,
}
export_domains_to_writer(writer, columns, sort_fields, filter_condition) export_domains_to_writer(writer, columns, sort_fields, filter_condition)