mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-08-25 18:43:49 +02:00
Add import
This commit is contained in:
parent
5e5626aaba
commit
14b6382a2d
3 changed files with 23 additions and 11 deletions
|
@ -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(
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue