diff --git a/src/registrar/management/commands/populate_domain_updated_federal_agency.py b/src/registrar/management/commands/populate_domain_updated_federal_agency.py index edaccd30a..9e369089f 100644 --- a/src/registrar/management/commands/populate_domain_updated_federal_agency.py +++ b/src/registrar/management/commands/populate_domain_updated_federal_agency.py @@ -1,5 +1,7 @@ """" -TODO: write description +Data migration: Renaming deprecated Federal Agencies to +their new updated names ie (U.S. Peace Corps to Peace Corps) +within Domain Information and Domain Requests """ import logging @@ -32,14 +34,16 @@ class Command(BaseCommand): "U.S. Peace Corps": "Peace Corps", "Chemical Safety Board": "U.S. Chemical Safety Board", "Nuclear Waste Technical Review Board": "U.S. Nuclear Waste Technical Review Board", - "State, Local, and Tribal Government": "Non-Federal Agency" + "State, Local, and Tribal Government": "Non-Federal Agency", } def handle(self, **options): """ - TODO: Update description here - If it's NULL for a domain request, it should return an error + Renames the Federal Agency to the correct new naming + for both Domain Information and Domain Requests objects. + NOTE: If it's NULL for a domain request, we skip it as + a user most likely hasn't gotten to it yet. """ logger.info("Transferring federal agencies to FederalAgency object") # DomainInformation object we populate with updated_federal_agency which are then bulk updated @@ -61,49 +65,54 @@ class Command(BaseCommand): federal_agency_row = self.find_federal_agency_row(domain_info) domain_info.updated_federal_agency = federal_agency_row domain_infos_to_update.append(domain_info) - logger.info(f"DomainInformation {domain_info} updated_federal_agency set to: {domain_info.updated_federal_agency}") + logger.info( + f"DomainInformation {domain_info} updated_federal_agency set to: {domain_info.updated_federal_agency}" + ) except Exception as err: logger.info(f"DomainInformation for {domain_info} failed to update updated_federal_agency: {err}") domain_infos_with_errors.append(domain_info) - ScriptDataHelper.bulk_update_fields( - DomainInformation, domain_infos_to_update, ["updated_federal_agency"] - ) + ScriptDataHelper.bulk_update_fields(DomainInformation, domain_infos_to_update, ["updated_federal_agency"]) for domain_request in domain_requests: try: if not domain_request.federal_agency: - # TODO: Make sure to clarify this in the description - # If null it's skipped bc user hasn't gotten to it yet domain_requests_skipped.append(domain_request) else: federal_agency_row = self.find_federal_agency_row(domain_request) domain_request.updated_federal_agency = federal_agency_row domain_requests_to_update.append(domain_request) - logger.info(f"DomainRequest {domain_request} updated_federal_agency set to: {domain_request.updated_federal_agency}") + logger.info( + f"DomainRequest {domain_request} updated_federal_agency set to: {domain_request.updated_federal_agency}" + ) except Exception as err: logger.info(f"DomainRequest for {domain_request} failed to update updated_federal_agency: {err}") domain_requests_with_errors.append(domain_request) - ScriptDataHelper.bulk_update_fields( - DomainRequest, domain_requests_to_update, ["updated_federal_agency"] - ) - + ScriptDataHelper.bulk_update_fields(DomainRequest, domain_requests_to_update, ["updated_federal_agency"]) + logger.info(f"{len(domain_infos_to_update)} DomainInformation rows updated update_federal_agency.") - logger.info(f"{len(domain_infos_with_errors)} DomainInformation rows errored when updating update_federal_agency.") + logger.info( + f"{len(domain_infos_with_errors)} DomainInformation rows errored when updating update_federal_agency." + ) logger.info(f"{len(domain_requests_to_update)} DomainRequest rows updated update_federal_agency.") logger.info(f"{len(domain_requests_skipped)} DomainRequest rows with null federal_agency skipped.") - logger.info(f"{len(domain_requests_with_errors)} DomainRequest rows errored when updating update_federal_agency.\n{domain_requests_with_errors}") + logger.info( + f"{len(domain_requests_with_errors)} DomainRequest rows errored when updating update_federal_agency.\n{domain_requests_with_errors}" + ) def find_federal_agency_row(self, domain_object): - ''' - TODO: Add description - We are grabbing the "previous set ups" Federal Agency - Domain Information objects without a federal agency default to Non-Federal Agency - ''' + """ + We grab the "old" federal agency object to rename and set the new object. + If the old name is null, we set it to "Non-Federal Agency". + Otherwise, we grab the key of the older name in + the rename_deprecated_federal_agency list and set it to the + value which is the new updated name we want and then grab that object + """ + federal_agency = domain_object.federal_agency - if not federal_agency: + if not federal_agency: federal_agency = "Non-Federal Agency" if federal_agency in self.rename_deprecated_federal_agency.keys(): federal_agency = self.rename_deprecated_federal_agency[federal_agency] - return FederalAgency.objects.filter(agency=federal_agency).get() \ No newline at end of file + return FederalAgency.objects.filter(agency=federal_agency).get()