Update populate_first_ready.py

This commit is contained in:
zandercymatics 2024-01-08 11:39:40 -07:00
parent 69fc902fc4
commit fe29eaa9fb
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7

View file

@ -12,6 +12,12 @@ logger = logging.getLogger(__name__)
class Command(BaseCommand):
help = "Loops through each valid Domain object and updates its first_created value"
def __init__(self):
super().__init__()
self.to_update: List[Domain] = []
self.failed_to_update: List[Domain] = []
self.skipped: List[Domain] = []
def add_arguments(self, parser):
"""Adds command line arguments"""
parser.add_argument("--debug", action=argparse.BooleanOptionalAction)
@ -19,14 +25,10 @@ class Command(BaseCommand):
def handle(self, **kwargs):
"""Loops through each valid Domain object and updates its first_created value"""
debug = kwargs.get("debug")
# Get all valid domains
valid_states = [Domain.State.READY, Domain.State.ON_HOLD, Domain.State.DELETED]
domains = Domain.objects.filter(first_ready=None, state__in=valid_states)
# Keep track of what we want to update, what failed, and what was skipped
to_update: List[Domain] = []
failed_to_update: List[Domain] = []
skipped: List[Domain] = []
# Code execution will stop here if the user prompts "N"
TerminalHelper.prompt_for_execution(
system_exit_on_terminate=True,
@ -40,18 +42,34 @@ class Command(BaseCommand):
for domain in domains:
try:
update_first_ready_for_domain(domain, debug)
self.update_first_ready_for_domain(domain, debug)
except Exception as err:
failed_to_update.append(domain)
self.failed_to_update.append(domain)
logger.error(err)
logger.error(
f"{TerminalColors.FAIL}"
f"Failed to update {domain}"
f"{TerminalColors.ENDC}"
)
ScriptDataHelper.bulk_update_fields(Domain, to_update, ["first_ready"])
# Do a bulk update on all fields
ScriptDataHelper.bulk_update_fields(Domain, self.to_update, ["first_ready"])
# Log what happened
TerminalHelper.log_script_run_summary(
to_update, failed_to_update, skipped, debug
self.to_update, self.failed_to_update, self.skipped, debug
)
def update_first_ready_for_domain(self, domain: Domain, debug: bool):
"""Grabs the created_at field and associates it with the first_ready column.
Appends the result to the to_update list."""
created_at = domain.created_at
if created_at is not None:
domain.first_ready = domain.created_at
self.to_update.append(domain)
if debug:
logger.info(f"Updating {domain}")
else:
self.skipped.append(domain)
if debug:
logger.warning(f"Skipped updating {domain}")