From a66fb36432f3c028cfe5b92f342bd55973ee0103 Mon Sep 17 00:00:00 2001 From: Rebecca Hsieh Date: Thu, 15 Feb 2024 10:52:14 -0800 Subject: [PATCH] Update function naming and length check --- src/registrar/tests/test_reports.py | 16 ++++++++-------- src/registrar/utility/csv_export.py | 15 ++++++++------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/registrar/tests/test_reports.py b/src/registrar/tests/test_reports.py index fa5acc96d..011c60b93 100644 --- a/src/registrar/tests/test_reports.py +++ b/src/registrar/tests/test_reports.py @@ -10,7 +10,7 @@ from registrar.models.user_domain_role import UserDomainRole from registrar.tests.common import MockEppLib from registrar.utility.csv_export import ( - write_body, + write_csv, get_default_start_date, get_default_end_date, ) @@ -403,7 +403,7 @@ def test_export_domains_to_writer_security_emails(self): } self.maxDiff = None # Call the export functions - write_body( + write_csv( writer, columns, sort_fields, filter_condition, get_domain_managers=False, should_write_header=True ) @@ -427,7 +427,7 @@ def test_export_domains_to_writer_security_emails(self): expected_content = expected_content.replace(",,", "").replace(",", "").replace(" ", "").strip() self.assertEqual(csv_content, expected_content) - def test_write_body(self): + def test_write_csv(self): """Test that write_body returns the existing domain, test that sort by domain name works, test that filter works""" @@ -462,7 +462,7 @@ def test_write_body(self): ], } # Call the export functions - write_body( + write_csv( writer, columns, sort_fields, filter_condition, get_domain_managers=False, should_write_header=True ) # Reset the CSV file's position to the beginning @@ -512,7 +512,7 @@ def test_write_body_additional(self): ], } # Call the export functions - write_body( + write_csv( writer, columns, sort_fields, filter_condition, get_domain_managers=False, should_write_header=True ) # Reset the CSV file's position to the beginning @@ -591,7 +591,7 @@ def test_write_body_with_date_filter_pulls_domains_in_range(self): } # Call the export functions - write_body( + write_csv( writer, columns, sort_fields, @@ -599,7 +599,7 @@ def test_write_body_with_date_filter_pulls_domains_in_range(self): get_domain_managers=False, should_write_header=True, ) - write_body( + write_csv( writer, columns, sort_fields_for_deleted_domains, @@ -664,7 +664,7 @@ def test_export_domains_to_writer_domain_managers(self): } self.maxDiff = None # Call the export functions - write_body( + write_csv( writer, columns, sort_fields, filter_condition, get_domain_managers=True, should_write_header=True ) diff --git a/src/registrar/utility/csv_export.py b/src/registrar/utility/csv_export.py index 1e4895f80..90e80f551 100644 --- a/src/registrar/utility/csv_export.py +++ b/src/registrar/utility/csv_export.py @@ -136,7 +136,7 @@ def update_columns_with_domain_managers(columns, max_dm_count): columns.append(f"Domain manager email {i}") -def write_body( +def write_csv( writer, columns, sort_fields, @@ -161,7 +161,7 @@ def write_body( # Reduce the memory overhead when performing the write operation paginator = Paginator(all_domain_infos, 1000) - if get_domain_managers: + if get_domain_managers and len(all_domain_infos) > 0: # We want to get the max amont of domain managers an # account has to set the column header dynamically max_dm_count = max(len(domain_info.domain.permissions.all()) for domain_info in all_domain_infos) @@ -179,6 +179,7 @@ def write_body( # It indicates that DomainInformation.domain is None. logger.error("csv_export -> Error when parsing row, domain was None") continue + if should_write_header: write_header(writer, columns) @@ -219,7 +220,7 @@ def export_data_type_to_csv(csv_file): Domain.State.ON_HOLD, ], } - write_body(writer, columns, sort_fields, filter_condition, get_domain_managers=True, should_write_header=True) + write_csv(writer, columns, sort_fields, filter_condition, get_domain_managers=True, should_write_header=True) def export_data_full_to_csv(csv_file): @@ -250,7 +251,7 @@ def export_data_full_to_csv(csv_file): Domain.State.ON_HOLD, ], } - write_body(writer, columns, sort_fields, filter_condition, get_domain_managers=False, should_write_header=True) + write_csv(writer, columns, sort_fields, filter_condition, get_domain_managers=False, should_write_header=True) def export_data_federal_to_csv(csv_file): @@ -282,7 +283,7 @@ def export_data_federal_to_csv(csv_file): Domain.State.ON_HOLD, ], } - write_body(writer, columns, sort_fields, filter_condition, get_domain_managers=False, should_write_header=True) + write_csv(writer, columns, sort_fields, filter_condition, get_domain_managers=False, should_write_header=True) def get_default_start_date(): @@ -349,8 +350,8 @@ def export_data_growth_to_csv(csv_file, start_date, end_date): "domain__deleted__gte": start_date_formatted, } - write_body(writer, columns, sort_fields, filter_condition, get_domain_managers=False, should_write_header=True) - write_body( + write_csv(writer, columns, sort_fields, filter_condition, get_domain_managers=False, should_write_header=True) + write_csv( writer, columns, sort_fields_for_deleted_domains,