mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-08-15 05:54:11 +02:00
Add some better logging and options
This commit is contained in:
parent
27993e5245
commit
8e6115acf0
1 changed files with 60 additions and 28 deletions
|
@ -60,7 +60,9 @@ class Command(BaseCommand):
|
||||||
self.handle_portfolio_domains(portfolio, federal_agency)
|
self.handle_portfolio_domains(portfolio, federal_agency)
|
||||||
|
|
||||||
def create_or_modify_portfolio(self, federal_agency):
|
def create_or_modify_portfolio(self, federal_agency):
|
||||||
# TODO - state_territory, city, etc fields???
|
"""Tries to create a portfolio record based off of a federal agency.
|
||||||
|
If the record already exists, we prompt the user to proceed then
|
||||||
|
update the record."""
|
||||||
portfolio_args = {
|
portfolio_args = {
|
||||||
"federal_agency": federal_agency,
|
"federal_agency": federal_agency,
|
||||||
"organization_name": federal_agency.agency,
|
"organization_name": federal_agency.agency,
|
||||||
|
@ -81,38 +83,44 @@ class Command(BaseCommand):
|
||||||
TerminalHelper.colorful_logger(logger.info, TerminalColors.OKGREEN, message)
|
TerminalHelper.colorful_logger(logger.info, TerminalColors.OKGREEN, message)
|
||||||
return portfolio
|
return portfolio
|
||||||
else:
|
else:
|
||||||
if len(existing_portfolio) > 1:
|
|
||||||
raise ValueError(f"Could not update portfolio '{portfolio}': multiple records exist.")
|
proceed = TerminalHelper.prompt_for_execution(
|
||||||
|
system_exit_on_terminate=False,
|
||||||
|
info_to_inspect=f"""The given portfolio '{federal_agency.agency}' already exists in our DB.
|
||||||
|
If you cancel, the rest of the script will still execute but this record will not update.
|
||||||
|
""",
|
||||||
|
prompt_title="Do you wish to modify this record?",
|
||||||
|
)
|
||||||
|
if not proceed:
|
||||||
|
if len(existing_portfolio) > 1:
|
||||||
|
raise ValueError(f"Could not use portfolio '{federal_agency.agency}': multiple records exist.")
|
||||||
|
else:
|
||||||
|
# Just return the portfolio object without modifying it
|
||||||
|
return existing_portfolio.get()
|
||||||
|
|
||||||
|
if len(existing_portfolio) > 1:
|
||||||
|
raise ValueError(f"Could not update portfolio '{federal_agency.agency}': multiple records exist.")
|
||||||
|
|
||||||
# TODO a dialog to confirm / deny doing this
|
|
||||||
existing_portfolio.update(**portfolio_args)
|
existing_portfolio.update(**portfolio_args)
|
||||||
message = f"Modified portfolio '{federal_agency.agency}'"
|
message = f"Modified portfolio '{existing_portfolio.first()}'"
|
||||||
TerminalHelper.colorful_logger(logger.info, TerminalColors.MAGENTA, message)
|
TerminalHelper.colorful_logger(logger.info, TerminalColors.MAGENTA, message)
|
||||||
return existing_portfolio.get()
|
return existing_portfolio.get()
|
||||||
|
|
||||||
def create_suborganizations(self, portfolio: Portfolio, federal_agency: FederalAgency):
|
def create_suborganizations(self, portfolio: Portfolio, federal_agency: FederalAgency):
|
||||||
non_federal_agency = FederalAgency.objects.get(agency="Non-Federal Agency")
|
"""Given a list of organization_names on DomainInformation objects (filtered by agency),
|
||||||
valid_agencies = DomainInformation.objects.filter(federal_agency=federal_agency).exclude(
|
create multiple Suborganizations tied to the given portfolio"""
|
||||||
Q(federal_agency=non_federal_agency) | Q(federal_agency__isnull=True)
|
valid_agencies = DomainInformation.objects.filter(federal_agency=federal_agency)
|
||||||
)
|
|
||||||
|
|
||||||
org_names = valid_agencies.values_list("organization_name", flat=True)
|
org_names = valid_agencies.values_list("organization_name", flat=True)
|
||||||
if len(org_names) < 1:
|
if len(org_names) < 1:
|
||||||
message =f"No suborganizations found for {federal_agency.agency}"
|
message =f"No suborganizations found for {federal_agency}"
|
||||||
TerminalHelper.colorful_logger(logger.warning, TerminalColors.YELLOW, message)
|
TerminalHelper.colorful_logger(logger.warning, TerminalColors.YELLOW, message)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Check if we need to update any existing suborgs first.
|
# Check if we need to update any existing suborgs first.
|
||||||
# This step is optional.
|
# This step is optional.
|
||||||
existing_suborgs = Suborganization.objects.filter(name__in=org_names)
|
existing_suborgs = Suborganization.objects.filter(name__in=org_names)
|
||||||
if len(existing_suborgs) > 1:
|
if len(existing_suborgs) > 0:
|
||||||
# TODO - we need a prompt here if any are found
|
self._update_existing_suborganizations(portfolio, existing_suborgs)
|
||||||
for org in existing_suborgs:
|
|
||||||
org.portfolio = portfolio
|
|
||||||
|
|
||||||
Suborganization.objects.bulk_update(existing_suborgs, ["portfolio"])
|
|
||||||
message = f"Updated {len(existing_suborgs)} suborganizations"
|
|
||||||
TerminalHelper.colorful_logger(logger.info, TerminalColors.OKGREEN, message)
|
|
||||||
|
|
||||||
# Add any suborgs that don't presently exist
|
# Add any suborgs that don't presently exist
|
||||||
excluded_org_names = existing_suborgs.values_list("name", flat=True)
|
excluded_org_names = existing_suborgs.values_list("name", flat=True)
|
||||||
|
@ -132,15 +140,39 @@ class Command(BaseCommand):
|
||||||
)
|
)
|
||||||
suborgs.append(suborg)
|
suborgs.append(suborg)
|
||||||
|
|
||||||
Suborganization.objects.bulk_create(suborgs)
|
if len(org_names) > 1:
|
||||||
|
Suborganization.objects.bulk_create(suborgs)
|
||||||
|
message = f"Added {len(suborgs)} suborganizations"
|
||||||
|
TerminalHelper.colorful_logger(logger.info, TerminalColors.OKGREEN, message)
|
||||||
|
else:
|
||||||
|
message =f"No suborganizations added"
|
||||||
|
TerminalHelper.colorful_logger(logger.warning, TerminalColors.YELLOW, message)
|
||||||
|
|
||||||
|
def _update_existing_suborganizations(self, portfolio, orgs_to_update):
|
||||||
|
proceed = TerminalHelper.prompt_for_execution(
|
||||||
|
system_exit_on_terminate=False,
|
||||||
|
info_to_inspect=f"""Some suborganizations already exist in our DB.
|
||||||
|
If you cancel, the rest of the script will still execute but these records will not update.
|
||||||
|
|
||||||
|
==Proposed Changes==
|
||||||
|
The following suborgs will be updated: {[org.name for org in orgs_to_update]}
|
||||||
|
""",
|
||||||
|
prompt_title="Do you wish to modify existing suborganizations?",
|
||||||
|
)
|
||||||
|
if not proceed:
|
||||||
|
return
|
||||||
|
|
||||||
|
for org in orgs_to_update:
|
||||||
|
org.portfolio = portfolio
|
||||||
|
|
||||||
|
Suborganization.objects.bulk_update(orgs_to_update, ["portfolio"])
|
||||||
|
message = f"Updated {len(orgs_to_update)} suborganizations"
|
||||||
|
TerminalHelper.colorful_logger(logger.info, TerminalColors.MAGENTA, message)
|
||||||
|
|
||||||
# TODO - this is inaccurate when the suborg name does not equal the org name - i.e. if the
|
|
||||||
# record exists already as well
|
|
||||||
message = f"Added {len(org_names)} suborganizations..."
|
|
||||||
TerminalHelper.colorful_logger(logger.info, TerminalColors.OKGREEN, message)
|
|
||||||
|
|
||||||
# TODO rename
|
|
||||||
def _update_portfolio_location_details(self, portfolio: Portfolio, domain_info: DomainInformation):
|
def _update_portfolio_location_details(self, portfolio: Portfolio, domain_info: DomainInformation):
|
||||||
|
"""Adds location information to the given portfolio based off of the values in
|
||||||
|
DomainInformation"""
|
||||||
location_props = [
|
location_props = [
|
||||||
"address_line1",
|
"address_line1",
|
||||||
"address_line2",
|
"address_line2",
|
||||||
|
@ -168,9 +200,9 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
for domain_request in domain_requests:
|
for domain_request in domain_requests:
|
||||||
domain_request.portfolio = portfolio
|
domain_request.portfolio = portfolio
|
||||||
|
|
||||||
DomainRequest.objects.bulk_update(domain_requests, ["portfolio"])
|
DomainRequest.objects.bulk_update(domain_requests, ["portfolio"])
|
||||||
message = f"Added 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)
|
||||||
|
|
||||||
def handle_portfolio_domains(self, portfolio: Portfolio, federal_agency: FederalAgency):
|
def handle_portfolio_domains(self, portfolio: Portfolio, federal_agency: FederalAgency):
|
||||||
|
@ -186,5 +218,5 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
DomainInformation.objects.bulk_update(domain_infos, ["portfolio"])
|
DomainInformation.objects.bulk_update(domain_infos, ["portfolio"])
|
||||||
|
|
||||||
message = f"Added 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)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue