mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-08-01 07:26:34 +02:00
cleanup
This commit is contained in:
parent
725441da70
commit
7c1e232d6b
2 changed files with 69 additions and 39 deletions
|
@ -15,9 +15,9 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.created_portfolios = []
|
self.updated_portfolios = set()
|
||||||
self.skipped_portfolios = []
|
self.skipped_portfolios = set()
|
||||||
self.failed_portfolios = []
|
self.failed_portfolios = set()
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
def add_arguments(self, parser):
|
||||||
"""Add three arguments:
|
"""Add three arguments:
|
||||||
|
@ -91,49 +91,61 @@ class Command(BaseCommand):
|
||||||
self.handle_portfolio_requests(portfolio, federal_agency)
|
self.handle_portfolio_requests(portfolio, federal_agency)
|
||||||
|
|
||||||
except Exception as exec:
|
except Exception as exec:
|
||||||
self.failed_portfolios.append(federal_agency)
|
self.failed_portfolios.add(federal_agency)
|
||||||
logger.error(exec)
|
logger.error(exec)
|
||||||
|
message = f"Failed to create portfolio '{portfolio}'"
|
||||||
|
TerminalHelper.colorful_logger(logger.info, TerminalColors.FAIL, message)
|
||||||
|
|
||||||
TerminalHelper.log_script_run_summary(
|
TerminalHelper.log_script_run_summary(
|
||||||
self.created_portfolios, self.failed_portfolios, self.skipped_portfolios, debug=False,
|
self.updated_portfolios,
|
||||||
skipped_header="----- SOME PORTFOLIOS WERE SKIPPED -----"
|
self.failed_portfolios,
|
||||||
|
self.skipped_portfolios,
|
||||||
|
debug=False,
|
||||||
|
skipped_header="----- SOME PORTFOLIOS WERE SKIPPED -----",
|
||||||
|
display_as_str=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
def create_portfolio(self, federal_agency):
|
def create_portfolio(self, federal_agency):
|
||||||
portfolio_args = {
|
# Get the org name / senior official
|
||||||
"federal_agency": federal_agency,
|
org_name = federal_agency.agency
|
||||||
"organization_name": federal_agency.agency,
|
so = federal_agency.so_federal_agency.first() if federal_agency.so_federal_agency.exists() else None
|
||||||
"organization_type": DomainRequest.OrganizationChoices.FEDERAL,
|
|
||||||
"creator": User.get_default_user(),
|
|
||||||
"notes": "Auto-generated record",
|
|
||||||
}
|
|
||||||
|
|
||||||
if federal_agency.so_federal_agency.exists():
|
# First just try to get an existing portfolio
|
||||||
portfolio_args["senior_official"] = federal_agency.so_federal_agency.first()
|
portfolio = Portfolio.objects.filter(organization_name=org_name).first()
|
||||||
|
if portfolio:
|
||||||
|
self.skipped_portfolios.add(portfolio)
|
||||||
|
TerminalHelper.colorful_logger(
|
||||||
|
logger.info,
|
||||||
|
TerminalColors.YELLOW,
|
||||||
|
f"Portfolio with organization name '{org_name}' already exists. Skipping create.",
|
||||||
|
)
|
||||||
|
return portfolio, False
|
||||||
|
|
||||||
portfolio, created = Portfolio.objects.get_or_create(
|
# Create new portfolio if it doesn't exist
|
||||||
organization_name=portfolio_args.get("organization_name"), defaults=portfolio_args
|
portfolio = Portfolio.objects.create(
|
||||||
|
organization_name=org_name,
|
||||||
|
federal_agency=federal_agency,
|
||||||
|
organization_type=DomainRequest.OrganizationChoices.FEDERAL,
|
||||||
|
creator=User.get_default_user(),
|
||||||
|
notes="Auto-generated record",
|
||||||
|
senior_official=so,
|
||||||
)
|
)
|
||||||
if created:
|
|
||||||
self.created_portfolios.append(portfolio)
|
|
||||||
message = f"Created portfolio '{portfolio}'"
|
|
||||||
TerminalHelper.colorful_logger(logger.info, TerminalColors.OKGREEN, message)
|
|
||||||
|
|
||||||
if portfolio_args.get("senior_official"):
|
self.updated_portfolios.add(portfolio)
|
||||||
message = f"Added senior official '{portfolio_args['senior_official']}'"
|
TerminalHelper.colorful_logger(logger.info, TerminalColors.OKGREEN, f"Created portfolio '{portfolio}'")
|
||||||
TerminalHelper.colorful_logger(logger.info, TerminalColors.OKGREEN, message)
|
|
||||||
else:
|
# Log if the senior official was added or not.
|
||||||
message = (
|
if portfolio.senior_official:
|
||||||
f"No senior official added to portfolio '{portfolio}'. "
|
message = f"Added senior official '{portfolio.senior_official}'"
|
||||||
"None was returned for the reverse relation `FederalAgency.so_federal_agency.first()`"
|
TerminalHelper.colorful_logger(logger.info, TerminalColors.OKGREEN, message)
|
||||||
)
|
|
||||||
TerminalHelper.colorful_logger(logger.info, TerminalColors.YELLOW, message)
|
|
||||||
else:
|
else:
|
||||||
self.skipped_portfolios.append(portfolio)
|
|
||||||
message = (
|
message = (
|
||||||
f"The given portfolio '{portfolio}' already exists in our DB. Skipping create."
|
f"No senior official added to portfolio '{org_name}'. "
|
||||||
|
"None was returned for the reverse relation `FederalAgency.so_federal_agency.first()`"
|
||||||
)
|
)
|
||||||
TerminalHelper.colorful_logger(logger.info, TerminalColors.YELLOW, message)
|
TerminalHelper.colorful_logger(logger.info, TerminalColors.YELLOW, message)
|
||||||
return portfolio, created
|
|
||||||
|
return portfolio, True
|
||||||
|
|
||||||
def create_suborganizations(self, portfolio: Portfolio, federal_agency: FederalAgency):
|
def create_suborganizations(self, portfolio: Portfolio, federal_agency: FederalAgency):
|
||||||
"""Create Suborganizations tied to the given portfolio based on DomainInformation objects"""
|
"""Create Suborganizations tied to the given portfolio based on DomainInformation objects"""
|
||||||
|
@ -155,7 +167,7 @@ class Command(BaseCommand):
|
||||||
existing_suborgs = Suborganization.objects.filter(name__in=org_names)
|
existing_suborgs = Suborganization.objects.filter(name__in=org_names)
|
||||||
if existing_suborgs.exists():
|
if existing_suborgs.exists():
|
||||||
message = f"Some suborganizations already exist for portfolio '{portfolio}'. Skipping create."
|
message = f"Some suborganizations already exist for portfolio '{portfolio}'. Skipping create."
|
||||||
TerminalHelper.colorful_logger(logger.warning, TerminalColors.YELLOW, message)
|
TerminalHelper.colorful_logger(logger.warning, TerminalColors.OKBLUE, message)
|
||||||
|
|
||||||
# Create new suborgs, as long as they don't exist in the db already
|
# Create new suborgs, as long as they don't exist in the db already
|
||||||
new_suborgs = []
|
new_suborgs = []
|
||||||
|
@ -191,7 +203,9 @@ class Command(BaseCommand):
|
||||||
DomainRequest.DomainRequestStatus.INELIGIBLE,
|
DomainRequest.DomainRequestStatus.INELIGIBLE,
|
||||||
DomainRequest.DomainRequestStatus.REJECTED,
|
DomainRequest.DomainRequestStatus.REJECTED,
|
||||||
]
|
]
|
||||||
domain_requests = DomainRequest.objects.filter(federal_agency=federal_agency, portfolio__isnull=True).exclude(status__in=invalid_states)
|
domain_requests = DomainRequest.objects.filter(federal_agency=federal_agency, portfolio__isnull=True).exclude(
|
||||||
|
status__in=invalid_states
|
||||||
|
)
|
||||||
if not domain_requests.exists():
|
if not domain_requests.exists():
|
||||||
message = f"""
|
message = f"""
|
||||||
Portfolio '{portfolio}' not added to domain requests: no valid records found.
|
Portfolio '{portfolio}' not added to domain requests: no valid records found.
|
||||||
|
@ -207,6 +221,7 @@ class Command(BaseCommand):
|
||||||
domain_request.portfolio = portfolio
|
domain_request.portfolio = portfolio
|
||||||
if domain_request.organization_name in suborgs:
|
if domain_request.organization_name in suborgs:
|
||||||
domain_request.sub_organization = suborgs.get(domain_request.organization_name)
|
domain_request.sub_organization = suborgs.get(domain_request.organization_name)
|
||||||
|
self.updated_portfolios.add(portfolio)
|
||||||
|
|
||||||
DomainRequest.objects.bulk_update(domain_requests, ["portfolio", "sub_organization"])
|
DomainRequest.objects.bulk_update(domain_requests, ["portfolio", "sub_organization"])
|
||||||
message = f"Added portfolio '{portfolio}' to {len(domain_requests)} domain requests."
|
message = f"Added portfolio '{portfolio}' to {len(domain_requests)} domain requests."
|
||||||
|
|
|
@ -206,8 +206,18 @@ class TerminalHelper:
|
||||||
if skipped_header is None:
|
if skipped_header is None:
|
||||||
skipped_header = "----- SOME DATA WAS INVALID (NEEDS MANUAL PATCHING) -----"
|
skipped_header = "----- SOME DATA WAS INVALID (NEEDS MANUAL PATCHING) -----"
|
||||||
|
|
||||||
|
# Give the user the option to see failed / skipped records if any exist.
|
||||||
|
debug_anyway = False
|
||||||
|
if not debug and update_failed_count > 0 or update_skipped_count > 0:
|
||||||
|
debug_anyway = TerminalHelper.prompt_for_execution(
|
||||||
|
system_exit_on_terminate=False,
|
||||||
|
prompt_message=f"You will see {update_failed_count} failed and {update_skipped_count} skipped records.",
|
||||||
|
verify_message="** Some records were skipped, or some failed to update. **",
|
||||||
|
prompt_title="Do you wish to see the full list of failed, skipped and updated records?",
|
||||||
|
)
|
||||||
|
|
||||||
# Prepare debug messages
|
# Prepare debug messages
|
||||||
if debug:
|
if debug or debug_anyway:
|
||||||
updated_display = [str(u) for u in to_update] if display_as_str else to_update
|
updated_display = [str(u) for u in to_update] if display_as_str else to_update
|
||||||
skipped_display = [str(s) for s in skipped] if display_as_str else skipped
|
skipped_display = [str(s) for s in skipped] if display_as_str else skipped
|
||||||
failed_display = [str(f) for f in failed_to_update] if display_as_str else failed_to_update
|
failed_display = [str(f) for f in failed_to_update] if display_as_str else failed_to_update
|
||||||
|
@ -220,7 +230,7 @@ class TerminalHelper:
|
||||||
# Print out a list of everything that was changed, if we have any changes to log.
|
# Print out a list of everything that was changed, if we have any changes to log.
|
||||||
# Otherwise, don't print anything.
|
# Otherwise, don't print anything.
|
||||||
TerminalHelper.print_conditional(
|
TerminalHelper.print_conditional(
|
||||||
debug,
|
True,
|
||||||
f"{debug_messages.get('success') if update_success_count > 0 else ''}"
|
f"{debug_messages.get('success') if update_success_count > 0 else ''}"
|
||||||
f"{debug_messages.get('skipped') if update_skipped_count > 0 else ''}"
|
f"{debug_messages.get('skipped') if update_skipped_count > 0 else ''}"
|
||||||
f"{debug_messages.get('failed') if update_failed_count > 0 else ''}",
|
f"{debug_messages.get('failed') if update_failed_count > 0 else ''}",
|
||||||
|
@ -371,7 +381,9 @@ class TerminalHelper:
|
||||||
logger.info(print_statement)
|
logger.info(print_statement)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def prompt_for_execution(system_exit_on_terminate: bool, prompt_message: str, prompt_title: str) -> bool:
|
def prompt_for_execution(
|
||||||
|
system_exit_on_terminate: bool, prompt_message: str, prompt_title: str, verify_message=None
|
||||||
|
) -> bool:
|
||||||
"""Create to reduce code complexity.
|
"""Create to reduce code complexity.
|
||||||
Prompts the user to inspect the given string
|
Prompts the user to inspect the given string
|
||||||
and asks if they wish to proceed.
|
and asks if they wish to proceed.
|
||||||
|
@ -383,6 +395,9 @@ class TerminalHelper:
|
||||||
if system_exit_on_terminate:
|
if system_exit_on_terminate:
|
||||||
action_description_for_selecting_no = "exit"
|
action_description_for_selecting_no = "exit"
|
||||||
|
|
||||||
|
if verify_message is None:
|
||||||
|
verify_message = "*** IMPORTANT: VERIFY THE FOLLOWING LOOKS CORRECT ***"
|
||||||
|
|
||||||
# Allow the user to inspect the command string
|
# Allow the user to inspect the command string
|
||||||
# and ask if they wish to proceed
|
# and ask if they wish to proceed
|
||||||
proceed_execution = TerminalHelper.query_yes_no_exit(
|
proceed_execution = TerminalHelper.query_yes_no_exit(
|
||||||
|
@ -390,7 +405,7 @@ class TerminalHelper:
|
||||||
=====================================================
|
=====================================================
|
||||||
{prompt_title}
|
{prompt_title}
|
||||||
=====================================================
|
=====================================================
|
||||||
*** IMPORTANT: VERIFY THE FOLLOWING LOOKS CORRECT ***
|
{verify_message}
|
||||||
|
|
||||||
{prompt_message}
|
{prompt_message}
|
||||||
{TerminalColors.FAIL}
|
{TerminalColors.FAIL}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue