Add script to update public contacts

This commit is contained in:
zandercymatics 2025-03-12 14:50:14 -06:00
parent b0d3968077
commit 3bf4807d8c
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
3 changed files with 51 additions and 13 deletions

View file

@ -0,0 +1,36 @@
import logging
from django.core.management import BaseCommand
from registrar.management.commands.utility.terminal_helper import PopulateScriptTemplate, TerminalHelper
from registrar.models import PublicContact, Domain
from django.db.models import Q
from registrar.utility.enums import DefaultEmail
logger = logging.getLogger(__name__)
class Command(BaseCommand, PopulateScriptTemplate):
help = "Loops through each default PublicContact and updates some values on each"
def handle(self, **kwargs):
"""Loops through each valid User object and updates its verification_type value"""
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)
def update_record(self, record: PublicContact):
"""Defines how we update the verification_type field"""
record.name = "CSD/CB Attn: .gov TLD"
record.street1 = "1110 N. Glebe Rd"
record.email = DefaultEmail.PUBLIC_CONTACT_DEFAULT
TerminalHelper.colorful_logger("INFO", "OKCYAN", f"Updating default values for '{record}'.")
TerminalHelper.colorful_logger("INFO", "MAGENTA", f"Attempting EPP update.")
# 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)
TerminalHelper.colorful_logger("INFO", "OKCYAN", f"Updated record in EPP.")

View file

@ -119,10 +119,11 @@ class PopulateScriptTemplate(ABC):
readable_class_name = self.get_class_name(object_class)
# for use in the execution prompt.
proposed_changes = f"""==Proposed Changes==
Number of {readable_class_name} objects to change: {len(records)}
These fields will be updated on each record: {fields_to_update}
"""
proposed_changes = (
"==Proposed Changes==\n"
f"Number of {readable_class_name} objects to change: {len(records)}\n"
f"These fields will be updated on each record: {fields_to_update}"
)
if verbose:
proposed_changes = f"""{proposed_changes}

View file

@ -870,7 +870,8 @@ class Domain(TimeStampedModel, DomainHelper):
logger.error("Error changing to new registrant error code is %s, error is %s" % (e.code, e))
# TODO-error handling better here?
def _set_singleton_contact(self, contact: PublicContact, expectedType: str): # noqa
@classmethod
def _set_singleton_contact(cls, contact: PublicContact, expectedType: str): # noqa
"""Sets the contacts by adding them to the registry as new contacts,
updates the contact if it is already in epp,
deletes any additional contacts of the matching type for this domain
@ -889,12 +890,12 @@ class Domain(TimeStampedModel, DomainHelper):
# domain and type but a different id
# like in highlander where there can only be one
duplicate_contacts = PublicContact.objects.exclude(registry_id=contact.registry_id).filter(
domain=self, contact_type=contact.contact_type
domain=cls, contact_type=contact.contact_type
)
# if no record exists with this contact type
# make contact in registry, duplicate and errors handled there
errorCode = self._make_contact_in_registry(contact)
errorCode = cls._make_contact_in_registry(contact)
# contact is already added to the domain, but something may have changed on it
alreadyExistsInRegistry = errorCode == ErrorCode.OBJECT_EXISTS
@ -915,11 +916,11 @@ class Domain(TimeStampedModel, DomainHelper):
if isRegistrant:
# send update domain only for registant contacts
existing_contact.delete()
self._add_registrant_to_existing_domain(contact)
cls._add_registrant_to_existing_domain(contact)
else:
# remove the old contact and add a new one
try:
self._update_domain_with_contact(contact=existing_contact, rem=True)
cls._update_domain_with_contact(contact=existing_contact, rem=True)
existing_contact.delete()
except Exception as err:
logger.error("Raising error after removing and adding a new contact")
@ -928,13 +929,13 @@ class Domain(TimeStampedModel, DomainHelper):
# update domain with contact or update the contact itself
if not isEmptySecurity:
if not alreadyExistsInRegistry and not isRegistrant:
self._update_domain_with_contact(contact=contact, rem=False)
cls._update_domain_with_contact(contact=contact, rem=False)
# if already exists just update
elif alreadyExistsInRegistry:
current_contact = PublicContact.objects.filter(registry_id=contact.registry_id).get()
if current_contact.email != contact.email:
self._update_epp_contact(contact=contact)
cls._update_epp_contact(contact=contact)
else:
logger.info("removing security contact and setting default again")
@ -944,10 +945,10 @@ class Domain(TimeStampedModel, DomainHelper):
# don't let user delete the default without adding a new email
if current_contact.email != PublicContact.get_default_security().email:
# remove the contact
self._update_domain_with_contact(contact=current_contact, rem=True)
cls._update_domain_with_contact(contact=current_contact, rem=True)
current_contact.delete()
# add new contact
security_contact = self.get_default_security_contact()
security_contact = cls.get_default_security_contact()
security_contact.save()
@security_contact.setter # type: ignore