add invited DMs to reports

This commit is contained in:
Rachid Mrad 2024-03-28 19:03:00 -04:00
parent 44487b40b2
commit 5cd70dad34
No known key found for this signature in database
3 changed files with 77 additions and 32 deletions

View file

@ -669,6 +669,24 @@ class MockDb(TestCase):
user=meoward_user, domain=self.domain_12, role=UserDomainRole.Roles.MANAGER user=meoward_user, domain=self.domain_12, role=UserDomainRole.Roles.MANAGER
) )
_, created = DomainInvitation.objects.get_or_create(
email=meoward_user.email, domain=self.domain_1, status=DomainInvitation.DomainInvitationStatus.RETRIEVED
)
_, created = DomainInvitation.objects.get_or_create(
email="woofwardthethird@rocks.com",
domain=self.domain_1,
status=DomainInvitation.DomainInvitationStatus.INVITED,
)
_, created = DomainInvitation.objects.get_or_create(
email="squeaker@rocks.com", domain=self.domain_2, status=DomainInvitation.DomainInvitationStatus.INVITED
)
_, created = DomainInvitation.objects.get_or_create(
email="squeaker@rocks.com", domain=self.domain_10, status=DomainInvitation.DomainInvitationStatus.INVITED
)
with less_console_noise(): with less_console_noise():
self.domain_request_1 = completed_domain_request( self.domain_request_1 = completed_domain_request(
status=DomainRequest.DomainRequestStatus.STARTED, name="city1.gov" status=DomainRequest.DomainRequestStatus.STARTED, name="city1.gov"
@ -698,6 +716,7 @@ class MockDb(TestCase):
DomainRequest.objects.all().delete() DomainRequest.objects.all().delete()
User.objects.all().delete() User.objects.all().delete()
UserDomainRole.objects.all().delete() UserDomainRole.objects.all().delete()
DomainInvitation.objects.all().delete()
def mock_user(): def mock_user():

View file

@ -478,7 +478,12 @@ class ExportDataTest(MockDb, MockEppLib):
def test_export_domains_to_writer_domain_managers(self): def test_export_domains_to_writer_domain_managers(self):
"""Test that export_domains_to_writer returns the """Test that export_domains_to_writer returns the
expected domain managers.""" expected domain managers.
An invited user, woofwardthethird, should also be pulled into this report.
squeaker@rocks.com is invited to domain2 (DNS_NEEDED) and domain10 (No managers).
She should show twice in this report but not in test_export_data_managed_domains_to_csv."""
with less_console_noise(): with less_console_noise():
# Create a CSV file in memory # Create a CSV file in memory
@ -521,14 +526,16 @@ class ExportDataTest(MockDb, MockEppLib):
expected_content = ( expected_content = (
"Domain name,Status,Expiration date,Domain type,Agency," "Domain name,Status,Expiration date,Domain type,Agency,"
"Organization name,City,State,AO,AO email," "Organization name,City,State,AO,AO email,"
"Security contact email,Domain manager email 1,Domain manager email 2,Domain manager email 3\n" "Security contact email,Domain manager 1,DM1 status,Domain manager 2,DM2 status,"
"adomain10.gov,Ready,,Federal,Armed Forces Retirement Home,,,, , ,\n" "Domain manager 3,DM3 status,Domain manager 4,DM4 status\n"
"adomain2.gov,Dns needed,,Interstate,,,,, , , ,meoward@rocks.com\n" "adomain10.gov,Ready,,Federal,Armed Forces Retirement Home,,,, , ,squeaker@rocks.com, I\n"
"cdomain11.govReadyFederal-ExecutiveWorldWarICentennialCommissionmeoward@rocks.com\n" "adomain2.gov,Dns needed,,Interstate,,,,, , , ,meoward@rocks.com, R,squeaker@rocks.com, I\n"
"cdomain11.govReadyFederal-ExecutiveWorldWarICentennialCommissionmeoward@rocks.comR\n"
"cdomain1.gov,Ready,,Federal - Executive,World War I Centennial Commission,,," "cdomain1.gov,Ready,,Federal - Executive,World War I Centennial Commission,,,"
", , , ,meoward@rocks.com,info@example.com,big_lebowski@dude.co\n" ", , , ,meoward@rocks.com,R,info@example.com,R,big_lebowski@dude.co,R,"
"woofwardthethird@rocks.com,I\n"
"ddomain3.gov,On hold,,Federal,Armed Forces Retirement Home,,,, , , ,,\n" "ddomain3.gov,On hold,,Federal,Armed Forces Retirement Home,,,, , , ,,\n"
"zdomain12.govReadyInterstatemeoward@rocks.com\n" "zdomain12.govReadyInterstatemeoward@rocks.comR\n"
) )
# Normalize line endings and remove commas, # Normalize line endings and remove commas,
# spaces and leading/trailing whitespace # spaces and leading/trailing whitespace
@ -538,7 +545,9 @@ class ExportDataTest(MockDb, MockEppLib):
def test_export_data_managed_domains_to_csv(self): def test_export_data_managed_domains_to_csv(self):
"""Test get counts for domains that have domain managers for two different dates, """Test get counts for domains that have domain managers for two different dates,
get list of managed domains at end_date.""" get list of managed domains at end_date.
An invited user, woofwardthethird, should also be pulled into this report."""
with less_console_noise(): with less_console_noise():
# Create a CSV file in memory # Create a CSV file in memory
@ -564,10 +573,12 @@ class ExportDataTest(MockDb, MockEppLib):
"Special district,School district,Election office\n" "Special district,School district,Election office\n"
"3,2,1,0,0,0,0,0,0,2\n" "3,2,1,0,0,0,0,0,0,2\n"
"\n" "\n"
"Domain name,Domain type,Domain manager email 1,Domain manager email 2,Domain manager email 3\n" "Domain name,Domain type,Domain manager 1,DM1 status,Domain manager 2,DM2 status,"
"cdomain11.govFederal-Executivemeoward@rocks.com\n" "Domain manager 3,DM3 status,Domain manager 4,DM4 status\n"
"cdomain1.gov,Federal - Executive,meoward@rocks.com,info@example.com,big_lebowski@dude.co\n" "cdomain11.govFederal-Executivemeoward@rocks.com, R\n"
"zdomain12.govInterstatemeoward@rocks.com\n" "cdomain1.gov,Federal - Executive,meoward@rocks.com,R,info@example.com,R,"
"big_lebowski@dude.co,R,woofwardthethird@rocks.com,I\n"
"zdomain12.govInterstatemeoward@rocks.com,R\n"
) )
# Normalize line endings and remove commas, # Normalize line endings and remove commas,

View file

@ -105,12 +105,22 @@ def parse_domain_row(columns, domain_info: DomainInformation, security_emails_di
} }
if get_domain_managers: if get_domain_managers:
# Get each domain managers email and add to list # Get lists of emails for active and invited domain managers
dm_emails = [dm.user.email for dm in domain.permissions.all()] dm_active_emails = [dm.user.email for dm in domain.permissions.all()]
dm_invited_emails = [
invite.email for invite in domain.invitations.filter(status=DomainInvitation.DomainInvitationStatus.INVITED)
]
# Set up the "matching header" + row field data # Set up the "matching headers" + row field data for email and status
for i, dm_email in enumerate(dm_emails, start=1): i = 0 # Declare i outside of the loop to avoid a reference before assignment in the second loop
FIELDS[f"Domain manager email {i}"] = dm_email for i, dm_email in enumerate(dm_active_emails, start=1):
FIELDS[f"Domain manager {i}"] = dm_email
FIELDS[f"DM{i} status"] = "R"
# Continue enumeration from where we left off and add data for invited domain managers
for j, dm_email in enumerate(dm_invited_emails, start=i + 1):
FIELDS[f"Domain manager {j}"] = dm_email
FIELDS[f"DM{j} status"] = "I"
row = [FIELDS.get(column, "") for column in columns] row = [FIELDS.get(column, "") for column in columns]
return row return row
@ -167,11 +177,15 @@ def write_domains_csv(
# Reduce the memory overhead when performing the write operation # Reduce the memory overhead when performing the write operation
paginator = Paginator(all_domain_infos, 1000) paginator = Paginator(all_domain_infos, 1000)
# The maximum amount of domain managers an account has # We get the number of domain managers (DMs) an the domain
# We get the max so we can set the column header accurately # that has the most DMs so we can set the header row appropriately
max_dm_active = 0 max_dm_active = 0
max_dm_invited = 0 max_dm_invited = 0
max_dm_count = 0 max_dm_total = 0
update_columns = False
# This var will live outside of the nested for loops to aggregate
# the data from those loops
total_body_rows = [] total_body_rows = []
for page_num in paginator.page_range: for page_num in paginator.page_range:
@ -182,23 +196,24 @@ def write_domains_csv(
# Get max number of domain managers # Get max number of domain managers
if get_domain_managers: if get_domain_managers:
dm_active = domain_info.domain.permissions.count() dm_active = domain_info.domain.permissions.count()
if dm_active > max_dm_active: dm_invited = domain_info.domain.invitations.filter(
max_dm_active = dm_active status=DomainInvitation.DomainInvitationStatus.INVITED
).count()
# Now let's get the domain managers who have not retrieved their invite yet
# Let's get the max number of whose
dm_invited = domain_info.domain.invitations.filter(status=DomainInvitation.DomainInvitationStatus.INVITED).count()
if dm_invited > max_dm_invited:
max_dm_invited = dm_invited
if dm_active > max_dm_active or dm_invited > max_dm_invited: if dm_active > max_dm_active or dm_invited > max_dm_invited:
max_dm_count = max_dm_active + max_dm_invited max_dm_active = max(dm_active, max_dm_active)
for i in range(1, max_dm_count + 1): max_dm_invited = max(dm_invited, max_dm_invited)
column_name = f"Domain manager email {i}" max_dm_total = max_dm_active + max_dm_invited
update_columns = True
if update_columns:
for i in range(1, max_dm_total + 1):
column_name = f"Domain manager {i}"
column2_name = f"DM{i} status" column2_name = f"DM{i} status"
if column_name not in columns: if column_name not in columns:
columns.append(column_name) columns.append(column_name)
columns.append(column2_name) columns.append(column2_name)
update_columns = False
try: try:
row = parse_domain_row(columns, domain_info, security_emails_dict, get_domain_managers) row = parse_domain_row(columns, domain_info, security_emails_dict, get_domain_managers)