mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-26 04:28:39 +02:00
Minor changes
This commit is contained in:
parent
02e0f23120
commit
a0e9db74eb
2 changed files with 14 additions and 12 deletions
|
@ -7,10 +7,12 @@ from registrar.models import FederalAgency, DomainRequest
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
# This command uses the PopulateScriptTemplate.
|
|
||||||
# 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.
|
|
||||||
class Command(BaseCommand, PopulateScriptTemplate):
|
class Command(BaseCommand, PopulateScriptTemplate):
|
||||||
|
"""
|
||||||
|
This command uses the PopulateScriptTemplate.
|
||||||
|
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.
|
||||||
|
"""
|
||||||
help = "Loops through each valid User object and updates its verification_type value"
|
help = "Loops through each valid User object and updates its verification_type value"
|
||||||
prompt_title = "Do you wish to update all Federal Agencies?"
|
prompt_title = "Do you wish to update all Federal Agencies?"
|
||||||
display_run_summary_items_as_str = True
|
display_run_summary_items_as_str = True
|
||||||
|
|
|
@ -82,15 +82,15 @@ class PopulateScriptTemplate(ABC):
|
||||||
"""Defines how we update each field. Must be defined before using mass_update_records."""
|
"""Defines how we update each field. Must be defined before using mass_update_records."""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def mass_update_records(self, sender, filter_conditions, fields_to_update, debug=True):
|
def mass_update_records(self, object_class, filter_conditions, fields_to_update, debug=True):
|
||||||
"""Loops through each valid "sender" object - specified by filter_conditions - and
|
"""Loops through each valid "object_class" object - specified by filter_conditions - and
|
||||||
updates fields defined by fields_to_update using update_record.
|
updates fields defined by fields_to_update using update_record.
|
||||||
|
|
||||||
You must define update_record before you can use this function.
|
You must define update_record before you can use this function.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
records = sender.objects.filter(**filter_conditions)
|
records = object_class.objects.filter(**filter_conditions)
|
||||||
readable_class_name = self.get_class_name(sender)
|
readable_class_name = self.get_class_name(object_class)
|
||||||
|
|
||||||
# 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(
|
||||||
|
@ -104,9 +104,9 @@ class PopulateScriptTemplate(ABC):
|
||||||
)
|
)
|
||||||
logger.info("Updating...")
|
logger.info("Updating...")
|
||||||
|
|
||||||
to_update: List[sender] = []
|
to_update: List[object_class] = []
|
||||||
to_skip: List[sender] = []
|
to_skip: List[object_class] = []
|
||||||
failed_to_update: List[sender] = []
|
failed_to_update: List[object_class] = []
|
||||||
for record in records:
|
for record in records:
|
||||||
try:
|
try:
|
||||||
if not self.should_skip_record(record):
|
if not self.should_skip_record(record):
|
||||||
|
@ -121,7 +121,7 @@ class PopulateScriptTemplate(ABC):
|
||||||
logger.error(fail_message)
|
logger.error(fail_message)
|
||||||
|
|
||||||
# Do a bulk update on the desired field
|
# Do a bulk update on the desired field
|
||||||
ScriptDataHelper.bulk_update_fields(sender, to_update, fields_to_update)
|
ScriptDataHelper.bulk_update_fields(object_class, to_update, fields_to_update)
|
||||||
|
|
||||||
# Log what happened
|
# Log what happened
|
||||||
TerminalHelper.log_script_run_summary(
|
TerminalHelper.log_script_run_summary(
|
||||||
|
@ -144,7 +144,7 @@ class PopulateScriptTemplate(ABC):
|
||||||
return f"{TerminalColors.FAIL}" f"Failed to update {record}" f"{TerminalColors.ENDC}"
|
return f"{TerminalColors.FAIL}" f"Failed to update {record}" f"{TerminalColors.ENDC}"
|
||||||
|
|
||||||
def should_skip_record(self, record) -> bool: # noqa
|
def should_skip_record(self, record) -> bool: # noqa
|
||||||
"""Defines the condition in which we should skip updating a record."""
|
"""Defines the condition in which we should skip updating a record. Override as needed."""
|
||||||
# By default - don't skip
|
# By default - don't skip
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue