mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-08-18 15:34:16 +02:00
Fix sneaky bug
why
This commit is contained in:
parent
2ca7cd468f
commit
2d509cfe62
1 changed files with 31 additions and 42 deletions
|
@ -6,6 +6,7 @@ from django.core.management import BaseCommand, CommandError
|
||||||
from registrar.management.commands.utility.terminal_helper import TerminalColors, TerminalHelper
|
from registrar.management.commands.utility.terminal_helper import TerminalColors, TerminalHelper
|
||||||
from registrar.models import DomainInformation, DomainRequest, FederalAgency, Suborganization, Portfolio, User
|
from registrar.models import DomainInformation, DomainRequest, FederalAgency, Suborganization, Portfolio, User
|
||||||
from django.db.models import F
|
from django.db.models import F
|
||||||
|
from django.db.models import Q
|
||||||
|
|
||||||
from registrar.models.utility.generic_helper import normalize_string
|
from registrar.models.utility.generic_helper import normalize_string
|
||||||
|
|
||||||
|
@ -96,29 +97,18 @@ class Command(BaseCommand):
|
||||||
For a given portfolio, it adds suborgs, and associates
|
For a given portfolio, it adds suborgs, and associates
|
||||||
the suborg and portfolio to domains and domain requests.
|
the suborg and portfolio to domains and domain requests.
|
||||||
"""
|
"""
|
||||||
all_suborganizations = []
|
|
||||||
all_domains = []
|
|
||||||
all_domain_requests = []
|
|
||||||
for federal_agency in agencies:
|
for federal_agency in agencies:
|
||||||
message = f"Processing federal agency '{federal_agency.agency}'..."
|
message = f"Processing federal agency '{federal_agency.agency}'..."
|
||||||
TerminalHelper.colorful_logger(logger.info, TerminalColors.MAGENTA, message)
|
TerminalHelper.colorful_logger(logger.info, TerminalColors.MAGENTA, message)
|
||||||
try:
|
try:
|
||||||
portfolio, created = self.create_portfolio(federal_agency)
|
portfolio, created = self.create_portfolio(federal_agency)
|
||||||
suborganizations = self.create_suborganizations(portfolio, federal_agency)
|
self.create_suborganizations(portfolio, federal_agency)
|
||||||
domains = []
|
|
||||||
domain_requests = []
|
|
||||||
if created and parse_domains or both:
|
if created and parse_domains or both:
|
||||||
domains = self.handle_portfolio_domains(portfolio, federal_agency)
|
self.handle_portfolio_domains(portfolio, federal_agency)
|
||||||
|
|
||||||
if parse_requests or both:
|
if parse_requests or both:
|
||||||
domain_requests = self.handle_portfolio_requests(portfolio, federal_agency)
|
self.handle_portfolio_requests(portfolio, federal_agency)
|
||||||
|
|
||||||
if suborganizations:
|
|
||||||
all_suborganizations.extend(suborganizations)
|
|
||||||
if all_domains:
|
|
||||||
all_domains.extend(domains)
|
|
||||||
if domain_requests:
|
|
||||||
all_domain_requests.extend(domain_requests)
|
|
||||||
except Exception as exec:
|
except Exception as exec:
|
||||||
self.failed_portfolios.add(federal_agency)
|
self.failed_portfolios.add(federal_agency)
|
||||||
logger.error(exec)
|
logger.error(exec)
|
||||||
|
@ -126,9 +116,8 @@ class Command(BaseCommand):
|
||||||
TerminalHelper.colorful_logger(logger.info, TerminalColors.FAIL, message)
|
TerminalHelper.colorful_logger(logger.info, TerminalColors.FAIL, message)
|
||||||
|
|
||||||
# Post process steps
|
# Post process steps
|
||||||
# Add suborg info to created or existing suborgs.
|
# Add additional suborg info where applicable.
|
||||||
if all_suborganizations:
|
updated_suborg_count = self.post_process_suborganization_fields(agencies)
|
||||||
updated_suborg_count = self.post_process_suborganization_fields(all_suborganizations, all_domains, all_domain_requests)
|
|
||||||
message = f"Added city and state_territory information to {updated_suborg_count} suborgs."
|
message = f"Added city and state_territory information to {updated_suborg_count} suborgs."
|
||||||
TerminalHelper.colorful_logger(logger.info, TerminalColors.MAGENTA, message)
|
TerminalHelper.colorful_logger(logger.info, TerminalColors.MAGENTA, message)
|
||||||
|
|
||||||
|
@ -221,8 +210,6 @@ class Command(BaseCommand):
|
||||||
else:
|
else:
|
||||||
TerminalHelper.colorful_logger(logger.warning, TerminalColors.YELLOW, "No suborganizations added")
|
TerminalHelper.colorful_logger(logger.warning, TerminalColors.YELLOW, "No suborganizations added")
|
||||||
|
|
||||||
return new_suborgs if len(new_suborgs) > 0 else []
|
|
||||||
|
|
||||||
def handle_portfolio_requests(self, portfolio: Portfolio, federal_agency: FederalAgency):
|
def handle_portfolio_requests(self, portfolio: Portfolio, federal_agency: FederalAgency):
|
||||||
"""
|
"""
|
||||||
Associate portfolio with domain requests for a federal agency.
|
Associate portfolio with domain requests for a federal agency.
|
||||||
|
@ -260,8 +247,6 @@ class Command(BaseCommand):
|
||||||
message = f"Added portfolio '{portfolio}' to {len(domain_requests)} domain requests."
|
message = f"Added portfolio '{portfolio}' to {len(domain_requests)} domain requests."
|
||||||
TerminalHelper.colorful_logger(logger.info, TerminalColors.OKGREEN, message)
|
TerminalHelper.colorful_logger(logger.info, TerminalColors.OKGREEN, message)
|
||||||
|
|
||||||
return list(domain_requests) if len(domain_requests) > 0 else []
|
|
||||||
|
|
||||||
def handle_portfolio_domains(self, portfolio: Portfolio, federal_agency: FederalAgency):
|
def handle_portfolio_domains(self, portfolio: Portfolio, federal_agency: FederalAgency):
|
||||||
"""
|
"""
|
||||||
Associate portfolio with domains for a federal agency.
|
Associate portfolio with domains for a federal agency.
|
||||||
|
@ -288,35 +273,39 @@ class Command(BaseCommand):
|
||||||
message = f"Added portfolio '{portfolio}' to {len(domain_infos)} domains."
|
message = f"Added portfolio '{portfolio}' to {len(domain_infos)} domains."
|
||||||
TerminalHelper.colorful_logger(logger.info, TerminalColors.OKGREEN, message)
|
TerminalHelper.colorful_logger(logger.info, TerminalColors.OKGREEN, message)
|
||||||
|
|
||||||
return list(domain_infos) if len(domain_infos) > 0 else []
|
def post_process_suborganization_fields(self, agencies):
|
||||||
|
|
||||||
def post_process_suborganization_fields(self, suborganizations, domains, requests):
|
|
||||||
"""Post-process suborganization fields by pulling data from related domains and requests.
|
"""Post-process suborganization fields by pulling data from related domains and requests.
|
||||||
|
|
||||||
This function updates suborganization city and state_territory fields based on
|
This function updates suborganization city and state_territory fields based on
|
||||||
related domain information and domain request information.
|
related domain information and domain request information.
|
||||||
"""
|
"""
|
||||||
domains = DomainInformation.objects.filter(id__in=[domain.id for domain in domains]).exclude(
|
# Assuming that org name, portfolio, and suborg all aren't null
|
||||||
portfolio__isnull=True,
|
# we assume that we want to add suborg info.
|
||||||
organization_name__isnull=True,
|
# as long as the org name doesnt match the portfolio name (as that implies it is the portfolio).
|
||||||
sub_organization__isnull=True,
|
should_add_suborgs_filter = Q(
|
||||||
organization_name__iexact=F("portfolio__organization_name"),
|
organization_name__isnull=False,
|
||||||
|
portfolio__isnull=False,
|
||||||
|
sub_organization__isnull=False,
|
||||||
|
) & ~Q(organization_name__iexact=F("portfolio__organization_name"))
|
||||||
|
domains = DomainInformation.objects.filter(
|
||||||
|
should_add_suborgs_filter,
|
||||||
|
federal_agency__in=agencies,
|
||||||
|
portfolio__isnull=False
|
||||||
)
|
)
|
||||||
requests = DomainRequest.objects.filter(id__in=[request.id for request in requests]).exclude(
|
requests = DomainRequest.objects.filter(
|
||||||
portfolio__isnull=True,
|
should_add_suborgs_filter,
|
||||||
organization_name__isnull=True,
|
federal_agency__in=agencies,
|
||||||
sub_organization__isnull=True,
|
portfolio__isnull=False
|
||||||
organization_name__iexact=F("portfolio__organization_name"),
|
|
||||||
)
|
)
|
||||||
domains_dict = {domain.organization_name: domain for domain in domains}
|
domains_dict = {domain.organization_name: domain for domain in domains}
|
||||||
requests_dict = {request.organization_name: request for request in requests}
|
requests_dict = {request.organization_name: request for request in requests}
|
||||||
logger.info(f"domains_dict: {domains_dict}")
|
suborgs_to_edit = Suborganization.objects.filter(
|
||||||
logger.info(f"requests_dict: {domains_dict}")
|
Q(id__in=domains.values_list("sub_organization", flat=True)) |
|
||||||
|
Q(id__in=requests.values_list("sub_organization", flat=True))
|
||||||
for suborg in suborganizations:
|
)
|
||||||
|
for suborg in suborgs_to_edit:
|
||||||
domain = domains_dict.get(suborg.name, None)
|
domain = domains_dict.get(suborg.name, None)
|
||||||
request = requests_dict.get(suborg.name, None)
|
request = requests_dict.get(suborg.name, None)
|
||||||
logger.info(f"suborg {suborg}: domain: {domain} , request: {request}")
|
|
||||||
|
|
||||||
# PRIORITY:
|
# PRIORITY:
|
||||||
# 1. Domain info
|
# 1. Domain info
|
||||||
|
@ -346,4 +335,4 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
logger.info(f"{suborg}: city: {suborg.city}, state: {suborg.state_territory}")
|
logger.info(f"{suborg}: city: {suborg.city}, state: {suborg.state_territory}")
|
||||||
|
|
||||||
return Suborganization.objects.bulk_update(suborganizations, ["city", "state_territory"])
|
return Suborganization.objects.bulk_update(suborgs_to_edit, ["city", "state_territory"])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue