From 0abec1900ff06b517d5d6cc8c9e87fec3fdc033e Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Fri, 14 Mar 2025 09:42:57 -0600 Subject: [PATCH] Script cleanup and lint --- .../update_default_public_contacts.py | 24 ++++++++++++++++--- .../commands/utility/terminal_helper.py | 13 ++++++---- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/registrar/management/commands/update_default_public_contacts.py b/src/registrar/management/commands/update_default_public_contacts.py index e2878e249..cab208d69 100644 --- a/src/registrar/management/commands/update_default_public_contacts.py +++ b/src/registrar/management/commands/update_default_public_contacts.py @@ -1,4 +1,5 @@ import logging +import argparse from django.core.management import BaseCommand from registrar.management.commands.utility.terminal_helper import PopulateScriptTemplate, TerminalHelper from registrar.models import PublicContact, Domain @@ -13,8 +14,25 @@ logger = logging.getLogger(__name__) class Command(BaseCommand, PopulateScriptTemplate): help = "Loops through each default PublicContact and updates some values on each" + def add_arguments(self, parser): + """Adds command line arguments""" + parser.add_argument( + "--overwrite_updated_contacts", + action=argparse.BooleanOptionalAction, + help=( + "Loops over PublicContacts with the email 'help@get.gov' when enabled." + "Use this setting if the record was updated in the DB but not correctly in EPP." + ), + ) + def handle(self, **kwargs): """Loops through each valid User object and updates its verification_type value""" + overwrite_updated_contacts = kwargs.get("overwrite_updated_contacts") + default_emails = {email for email in DefaultEmail} + + # Don't update records we've already updated + if not overwrite_updated_contacts: + default_emails.remove(DefaultEmail.PUBLIC_CONTACT_DEFAULT) # We should only update DEFAULT records. This means that if all values are not default, # we should skip as this could lead to data corruption. @@ -28,10 +46,10 @@ class Command(BaseCommand, PopulateScriptTemplate): }, "street1": {"1110 n. glebe rd", "cisa – ngr stop 0645", "4200 wilson blvd."}, "pc": {"22201", "20598-0645"}, - "email": {email for email in DefaultEmail}, + "email": default_emails, } - old_emails = [email for email in DefaultEmail if email != DefaultEmail.PUBLIC_CONTACT_DEFAULT] - filter_condition = {"email__in": old_emails} + + filter_condition = {"email__in": default_emails} self.mass_update_records(PublicContact, filter_condition, [], skip_bulk_update=True) def update_record(self, record: PublicContact): diff --git a/src/registrar/management/commands/utility/terminal_helper.py b/src/registrar/management/commands/utility/terminal_helper.py index 89ab83b21..fec21fd4d 100644 --- a/src/registrar/management/commands/utility/terminal_helper.py +++ b/src/registrar/management/commands/utility/terminal_helper.py @@ -86,7 +86,9 @@ class PopulateScriptTemplate(ABC): """ raise NotImplementedError - def mass_update_records(self, object_class, filter_conditions, fields_to_update, debug=True, verbose=False, skip_bulk_update=False): + def mass_update_records( + self, object_class, filter_conditions, fields_to_update, debug=True, verbose=False, skip_bulk_update=False + ): """Loops through each valid "object_class" object - specified by filter_conditions - and updates fields defined by fields_to_update using update_record. @@ -105,10 +107,11 @@ class PopulateScriptTemplate(ABC): verbose: Whether to print a detailed run summary *before* run confirmation. Default: False. - - skip_bulk_update: Whether to avoid doing a bulk update or not. - This setting assumes that you are doing a save in the update_record class. - IMPORANT: this setting invalidates 'fields_to_update'. + + skip_bulk_update: Whether to avoid doing a bulk update or not. + This setting assumes that you are doing a save in the update_record class. + IMPORANT: this setting invalidates 'fields_to_update'. + Default: False Raises: NotImplementedError: If you do not define update_record before using this function.