diff --git a/src/registrar/management/commands/create_federal_portfolio.py b/src/registrar/management/commands/create_federal_portfolio.py index f3debba56..37bd4765d 100644 --- a/src/registrar/management/commands/create_federal_portfolio.py +++ b/src/registrar/management/commands/create_federal_portfolio.py @@ -7,6 +7,8 @@ from registrar.management.commands.utility.terminal_helper import TerminalColors from registrar.models import DomainInformation, DomainRequest, FederalAgency, Suborganization, Portfolio, User from django.db.models import F +from registrar.models.utility.generic_helper import normalize_string + logger = logging.getLogger(__name__) @@ -99,10 +101,6 @@ class Command(BaseCommand): display_as_str=True, ) - # TODO - add post processing step to add suborg city, state, etc. - # This needs to be done after because of execution order. - # However, we do not need to necessarily prompt the user in this case. - def handle_populate_portfolio(self, federal_agency, parse_domains, parse_requests, both): """Attempts to create a portfolio. If successful, this function will also create new suborganizations""" @@ -126,9 +124,9 @@ class Command(BaseCommand): # Post process steps # Add suborg info to created or existing suborgs. Get the refreshed queryset for each. - self.post_process_suborganization(suborganizations.all(), domains.all(), domain_requests.all()) + self.post_process_suborganization_fields(suborganizations.all(), domains.all(), domain_requests.all()) - def post_process_suborganization(self, suborganizations, domains, requests): + def post_process_suborganization_fields(self, suborganizations, domains, requests): # Exclude domains and requests where the org name is the same, # and where we are missing some crucial information. domains = domains.exclude( diff --git a/src/registrar/management/commands/patch_suborganizations.py b/src/registrar/management/commands/patch_suborganizations.py index 197c6fe46..88ad611da 100644 --- a/src/registrar/management/commands/patch_suborganizations.py +++ b/src/registrar/management/commands/patch_suborganizations.py @@ -12,12 +12,19 @@ class Command(BaseCommand): help = "Clean up duplicate suborganizations that differ only by spaces and capitalization" def handle(self, **kwargs): - + # Maybe we should just do these manually? + extra_records_to_delete = [ + "Assistant Secretary for Preparedness and Response Office of the Secretary", + "US Geological Survey", + "USDA/OC", + ] # Step 1: delete duplicates + self.delete_suborganization_duplicates() + + def delete_suborganization_duplicates(self, extra_records_to_delete): # Find duplicates duplicates = {} all_suborgs = Suborganization.objects.all() - for suborg in all_suborgs: # Normalize name by removing extra spaces and converting to lowercase normalized_name = " ".join(suborg.name.split()).lower() @@ -124,6 +131,4 @@ class Command(BaseCommand): except Exception as e: logger.error(f"{TerminalColors.FAIL}Failed to clean up suborganizations: {str(e)}{TerminalColors.ENDC}") - - # Step 2: Add city, state, etc info to existing suborganizations. - + diff --git a/src/registrar/models/utility/generic_helper.py b/src/registrar/models/utility/generic_helper.py index 5e425f5a3..84dc28db1 100644 --- a/src/registrar/models/utility/generic_helper.py +++ b/src/registrar/models/utility/generic_helper.py @@ -343,3 +343,12 @@ def value_of_attribute(obj, attribute_name: str): if callable(value): value = value() return value + +def normalize_string(string_to_normalize, lowercase=True): + """Normalizes a given string. Returns a string without extra spaces, in all lowercase.""" + if not isinstance(string_to_normalize, str): + logger.error(f"normalize_string => {string_to_normalize} is not type str.") + return string_to_normalize + + new_string = " ".join(string_to_normalize.split()) + return new_string.lower() if lowercase else new_string