use save instead of set

This commit is contained in:
zandercymatics 2025-03-14 09:35:23 -06:00
parent 0fa9ad1546
commit ec97f28442
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
2 changed files with 9 additions and 7 deletions

View file

@ -32,8 +32,7 @@ class Command(BaseCommand, PopulateScriptTemplate):
}
old_emails = [email for email in DefaultEmail if email != DefaultEmail.PUBLIC_CONTACT_DEFAULT]
filter_condition = {"email__in": old_emails}
fields_to_update = ["name", "street1", "pc", "email"]
self.mass_update_records(PublicContact, filter_condition, fields_to_update)
self.mass_update_records(PublicContact, filter_condition, [], skip_bulk_update=True)
def update_record(self, record: PublicContact):
"""Defines how we update the verification_type field"""
@ -42,9 +41,7 @@ class Command(BaseCommand, PopulateScriptTemplate):
record.pc = "22201"
record.email = DefaultEmail.PUBLIC_CONTACT_DEFAULT
TerminalHelper.colorful_logger("INFO", "OKCYAN", f"Updating default values for '{record}'.")
TerminalHelper.colorful_logger("INFO", "MAGENTA", f"Attempting to update record in EPP...")
# Since this function raises an error, this update will revert on both the model and here
Domain._set_singleton_contact(record, expectedType=record.contact_type)
record.save()
TerminalHelper.colorful_logger("INFO", "OKCYAN", f"Updated record in EPP.")
def should_skip_record(self, record) -> bool: # noqa

View file

@ -86,7 +86,7 @@ class PopulateScriptTemplate(ABC):
"""
raise NotImplementedError
def mass_update_records(self, object_class, filter_conditions, fields_to_update, debug=True, verbose=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,6 +105,10 @@ 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'.
Raises:
NotImplementedError: If you do not define update_record before using this function.
@ -155,7 +159,8 @@ class PopulateScriptTemplate(ABC):
logger.error(fail_message)
# Do a bulk update on the desired field
ScriptDataHelper.bulk_update_fields(object_class, to_update, fields_to_update)
if not skip_bulk_update:
ScriptDataHelper.bulk_update_fields(object_class, to_update, fields_to_update)
# Log what happened
TerminalHelper.log_script_run_summary(