mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-23 19:20:47 +02:00
Update script
This commit is contained in:
parent
0961ecbb33
commit
3b0fa34ee2
2 changed files with 69 additions and 39 deletions
|
@ -883,6 +883,8 @@ class DomainInformationAdmin(ListHeaderAdmin):
|
||||||
"Type of organization",
|
"Type of organization",
|
||||||
{
|
{
|
||||||
"fields": [
|
"fields": [
|
||||||
|
"is_election_board",
|
||||||
|
"generic_org_type",
|
||||||
"organization_type",
|
"organization_type",
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
from registrar.signals import create_or_update_organization_type
|
||||||
from typing import List
|
from typing import List
|
||||||
from django.core.management import BaseCommand
|
from django.core.management import BaseCommand
|
||||||
from registrar.management.commands.utility.terminal_helper import TerminalColors, TerminalHelper, ScriptDataHelper
|
from registrar.management.commands.utility.terminal_helper import TerminalColors, TerminalHelper, ScriptDataHelper
|
||||||
from registrar.models import DomainInformation, DomainRequest, Domain
|
from registrar.models import DomainInformation, DomainRequest, Domain
|
||||||
|
from django.db import transaction
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,18 +25,37 @@ class Command(BaseCommand):
|
||||||
self.di_failed_to_update: List[DomainInformation] = []
|
self.di_failed_to_update: List[DomainInformation] = []
|
||||||
self.di_skipped: List[DomainInformation] = []
|
self.di_skipped: List[DomainInformation] = []
|
||||||
|
|
||||||
|
# Define a global variable for all domains with election offices
|
||||||
|
self.domains_with_election_offices_set = set()
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
def add_arguments(self, parser):
|
||||||
"""Adds command line arguments"""
|
"""Adds command line arguments"""
|
||||||
parser.add_argument("--debug", action=argparse.BooleanOptionalAction)
|
parser.add_argument("--debug", action=argparse.BooleanOptionalAction)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"election_office_filename",
|
"domain_election_office_filename",
|
||||||
help=("A JSON file that holds the location and filenames" "of all the data files used for migrations"),
|
help=("A JSON file that holds the location and filenames" "of all the data files used for migrations"),
|
||||||
)
|
)
|
||||||
|
|
||||||
def handle(self, **kwargs):
|
def handle(self, domain_election_office_filename, **kwargs):
|
||||||
"""Loops through each valid Domain object and updates its first_created value"""
|
"""Loops through each valid Domain object and updates its first_created value"""
|
||||||
debug = kwargs.get("debug")
|
debug = kwargs.get("debug")
|
||||||
domain_requests = DomainRequest.objects.filter(organization_type__isnull=True)
|
|
||||||
|
# Check if the provided file path is valid
|
||||||
|
if not os.path.isfile(domain_election_office_filename):
|
||||||
|
raise argparse.ArgumentTypeError(f"Invalid file path '{domain_election_office_filename}'")
|
||||||
|
|
||||||
|
|
||||||
|
with open(domain_election_office_filename, "r") as file:
|
||||||
|
for line in file:
|
||||||
|
# Remove any leading/trailing whitespace
|
||||||
|
domain = line.strip()
|
||||||
|
if domain not in self.domains_with_election_offices_set:
|
||||||
|
self.domains_with_election_offices_set.add(domain)
|
||||||
|
|
||||||
|
domain_requests = DomainRequest.objects.filter(
|
||||||
|
organization_type__isnull=True,
|
||||||
|
requested_domain__name__isnull=False
|
||||||
|
)
|
||||||
|
|
||||||
# Code execution will stop here if the user prompts "N"
|
# Code execution will stop here if the user prompts "N"
|
||||||
TerminalHelper.prompt_for_execution(
|
TerminalHelper.prompt_for_execution(
|
||||||
|
@ -51,7 +72,9 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
self.update_domain_requests(domain_requests, debug)
|
self.update_domain_requests(domain_requests, debug)
|
||||||
|
|
||||||
domain_infos = DomainInformation.objects.filter(domain_request__isnull=False, organization_type__isnull=True)
|
# We should actually be targeting all fields with no value for organization type,
|
||||||
|
# but do have a value for generic_org_type. This is because there is data that we can infer.
|
||||||
|
domain_infos = DomainInformation.objects.filter(organization_type__isnull=True)
|
||||||
# Code execution will stop here if the user prompts "N"
|
# Code execution will stop here if the user prompts "N"
|
||||||
TerminalHelper.prompt_for_execution(
|
TerminalHelper.prompt_for_execution(
|
||||||
system_exit_on_terminate=True,
|
system_exit_on_terminate=True,
|
||||||
|
@ -68,25 +91,28 @@ class Command(BaseCommand):
|
||||||
self.update_domain_informations(domain_infos, debug)
|
self.update_domain_informations(domain_infos, debug)
|
||||||
|
|
||||||
def update_domain_requests(self, domain_requests, debug):
|
def update_domain_requests(self, domain_requests, debug):
|
||||||
for request in domain_requests:
|
with transaction.atomic():
|
||||||
try:
|
for request in domain_requests:
|
||||||
# TODO - parse data from hfile ere
|
try:
|
||||||
if request.generic_org_type is not None:
|
# TODO - parse data from hfile ere
|
||||||
request.is_election_board = True
|
if request.generic_org_type is not None:
|
||||||
self.request_to_update.append(request)
|
domain_name = request.requested_domain.name
|
||||||
if debug:
|
request.is_election_board = domain_name in self.domains_with_election_offices_set
|
||||||
logger.info(f"Updating {request}")
|
request.save()
|
||||||
else:
|
self.request_to_update.append(request)
|
||||||
self.request_skipped.append(request)
|
if debug:
|
||||||
if debug:
|
logger.info(f"Updated {request} => {request.organization_type}")
|
||||||
logger.warning(f"Skipped updating {request}")
|
else:
|
||||||
except Exception as err:
|
self.request_skipped.append(request)
|
||||||
self.request_failed_to_update.append(request)
|
if debug:
|
||||||
logger.error(err)
|
logger.warning(f"Skipped updating {request}. No generic_org_type was found.")
|
||||||
logger.error(f"{TerminalColors.FAIL}" f"Failed to update {request}" f"{TerminalColors.ENDC}")
|
except Exception as err:
|
||||||
|
self.request_failed_to_update.append(request)
|
||||||
|
logger.error(err)
|
||||||
|
logger.error(f"{TerminalColors.FAIL}" f"Failed to update {request}" f"{TerminalColors.ENDC}")
|
||||||
|
|
||||||
# Do a bulk update on the organization_type field
|
# Do a bulk update on the organization_type field
|
||||||
ScriptDataHelper.bulk_update_fields(DomainRequest, self.request_to_update, ["organization_type"])
|
# ScriptDataHelper.bulk_update_fields(DomainRequest, self.request_to_update, ["is_election_board"])
|
||||||
|
|
||||||
# Log what happened
|
# Log what happened
|
||||||
log_header = "============= FINISHED UPDATE FOR DOMAINREQUEST ==============="
|
log_header = "============= FINISHED UPDATE FOR DOMAINREQUEST ==============="
|
||||||
|
@ -95,25 +121,27 @@ class Command(BaseCommand):
|
||||||
)
|
)
|
||||||
|
|
||||||
def update_domain_informations(self, domain_informations, debug):
|
def update_domain_informations(self, domain_informations, debug):
|
||||||
for info in domain_informations:
|
with transaction.atomic():
|
||||||
try:
|
for info in domain_informations:
|
||||||
# TODO - parse data from hfile ere
|
try:
|
||||||
if info.generic_org_type is not None:
|
if info.generic_org_type is not None:
|
||||||
info.is_election_board = True
|
domain_name = info.domain.name
|
||||||
self.di_to_update.append(info)
|
info.is_election_board = domain_name in self.domains_with_election_offices_set
|
||||||
if debug:
|
info.save()
|
||||||
logger.info(f"Updating {info}")
|
self.di_to_update.append(info)
|
||||||
else:
|
if debug:
|
||||||
self.di_skipped.append(info)
|
logger.info(f"Updated {info} => {info.organization_type}")
|
||||||
if debug:
|
else:
|
||||||
logger.warning(f"Skipped updating {info}")
|
self.di_skipped.append(info)
|
||||||
except Exception as err:
|
if debug:
|
||||||
self.di_failed_to_update.append(info)
|
logger.warning(f"Skipped updating {info}. No generic_org_type was found.")
|
||||||
logger.error(err)
|
except Exception as err:
|
||||||
logger.error(f"{TerminalColors.FAIL}" f"Failed to update {info}" f"{TerminalColors.ENDC}")
|
self.di_failed_to_update.append(info)
|
||||||
|
logger.error(err)
|
||||||
|
logger.error(f"{TerminalColors.FAIL}" f"Failed to update {info}" f"{TerminalColors.ENDC}")
|
||||||
|
|
||||||
# Do a bulk update on the organization_type field
|
# Do a bulk update on the organization_type field
|
||||||
ScriptDataHelper.bulk_update_fields(DomainInformation, self.di_to_update, ["organization_type"])
|
# ScriptDataHelper.bulk_update_fields(DomainInformation, self.di_to_update, ["organization_type", "is_election_board", "generic_org_type"])
|
||||||
|
|
||||||
# Log what happened
|
# Log what happened
|
||||||
log_header = "============= FINISHED UPDATE FOR DOMAININFORMATION ==============="
|
log_header = "============= FINISHED UPDATE FOR DOMAININFORMATION ==============="
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue