This commit is contained in:
zandercymatics 2024-06-24 09:37:31 -06:00
parent 26ddf317d5
commit 02e0f23120
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
3 changed files with 12 additions and 13 deletions

View file

@ -12,12 +12,11 @@ class Command(BaseCommand, PopulateScriptTemplate):
def handle(self, **kwargs): def handle(self, **kwargs):
"""Loops through each valid User object and updates its verification_type value""" """Loops through each valid User object and updates its verification_type value"""
filter_condition = {"verification_type__isnull": True} filter_condition = {"verification_type__isnull": True}
self.mass_populate_field(User, filter_condition, ["verification_type"]) self.mass_update_records(User, filter_condition, ["verification_type"])
def populate_field(self, field_to_update): def update_record(self, record: User):
"""Defines how we update the verification_type field""" """Defines how we update the verification_type field"""
field_to_update.set_user_verification_type() record.set_user_verification_type()
logger.info( logger.info(
f"{TerminalColors.OKCYAN}Updating {field_to_update} => " f"{TerminalColors.OKCYAN}Updating {record} => " f"{record.verification_type}{TerminalColors.OKCYAN}"
f"{field_to_update.verification_type}{TerminalColors.OKCYAN}"
) )

View file

@ -1,6 +1,6 @@
import logging import logging
from django.core.management import BaseCommand from django.core.management import BaseCommand
from registrar.management.commands.utility.terminal_helper import PopulateScriptTemplate from registrar.management.commands.utility.terminal_helper import PopulateScriptTemplate, TerminalColors
from registrar.models import FederalAgency, DomainRequest from registrar.models import FederalAgency, DomainRequest
@ -19,7 +19,7 @@ class Command(BaseCommand, PopulateScriptTemplate):
"""Loops through each valid User object and updates its verification_type value""" """Loops through each valid User object and updates its verification_type value"""
# Get all existing domain requests. Select_related allows us to skip doing db queries. # Get all existing domain requests. Select_related allows us to skip doing db queries.
self.all_domain_requests = DomainRequest.objects.select_related("federal_agency").distinct() self.all_domain_requests = DomainRequest.objects.select_related("federal_agency")
self.mass_update_records( self.mass_update_records(
FederalAgency, filter_conditions={"agency__isnull": False}, fields_to_update=["federal_type"] FederalAgency, filter_conditions={"agency__isnull": False}, fields_to_update=["federal_type"]
) )
@ -28,11 +28,11 @@ class Command(BaseCommand, PopulateScriptTemplate):
"""Defines how we update the federal_type field on each record.""" """Defines how we update the federal_type field on each record."""
request = self.all_domain_requests.filter(federal_agency__agency=record.agency).first() request = self.all_domain_requests.filter(federal_agency__agency=record.agency).first()
record.federal_type = request.federal_type record.federal_type = request.federal_type
logger.info(f"{TerminalColors.OKCYAN}Updating {str(record)} => {record.federal_type}{TerminalColors.OKCYAN}")
def should_skip_record(self, record) -> bool: # noqa def should_skip_record(self, record) -> bool: # noqa
"""Defines the conditions in which we should skip updating a record.""" """Defines the conditions in which we should skip updating a record."""
requests = self.all_domain_requests.filter(federal_agency__agency=record.agency, federal_type__isnull=False) requests = self.all_domain_requests.filter(federal_agency__agency=record.agency, federal_type__isnull=False)
# Check if all federal_type values are the same. Skip the record otherwise. # Check if all federal_type values are the same. Skip the record otherwise.
distinct_federal_types = requests.values('federal_type').distinct() distinct_federal_types = requests.values("federal_type").distinct()
return distinct_federal_types.count() != 1 return distinct_federal_types.count() != 1

View file

@ -59,7 +59,6 @@ class ScriptDataHelper:
model_class.objects.bulk_update(page.object_list, fields_to_update) model_class.objects.bulk_update(page.object_list, fields_to_update)
# This template handles logging and bulk updating for you, for repetitive scripts that update a few fields. # This template handles logging and bulk updating for you, for repetitive scripts that update a few fields.
# It is the ultimate lazy mans shorthand. Don't use this for anything terribly complicated. # It is the ultimate lazy mans shorthand. Don't use this for anything terribly complicated.
# See the transfer_federal_agency.py file for example usage - its really quite simple! # See the transfer_federal_agency.py file for example usage - its really quite simple!
@ -150,10 +149,11 @@ class PopulateScriptTemplate(ABC):
return False return False
class TerminalHelper: class TerminalHelper:
@staticmethod @staticmethod
def log_script_run_summary(to_update, failed_to_update, skipped, debug: bool, log_header=None, display_as_str=False): def log_script_run_summary(
to_update, failed_to_update, skipped, debug: bool, log_header=None, display_as_str=False
):
"""Prints success, failed, and skipped counts, as well as """Prints success, failed, and skipped counts, as well as
all affected objects.""" all affected objects."""
update_success_count = len(to_update) update_success_count = len(to_update)