mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-08-05 01:11:55 +02:00
Undo copy_contacts_to_users
This script goes against the AC which just wants to modify the existing script, rather than this new one
This commit is contained in:
parent
c2dd91215a
commit
194dfe2efd
3 changed files with 5 additions and 63 deletions
|
@ -1,50 +0,0 @@
|
|||
import logging
|
||||
from django.core.management import BaseCommand
|
||||
from registrar.management.commands.utility.terminal_helper import PopulateScriptTemplate, TerminalColors
|
||||
from registrar.models import User, Contact
|
||||
from registrar.models.utility.domain_helper import DomainHelper
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Command(BaseCommand, PopulateScriptTemplate):
|
||||
help = "Loops through each valid User object and updates its verification_type value"
|
||||
|
||||
# Get the fields that exist on both User and Contact. Excludes id.
|
||||
common_fields = DomainHelper.get_common_fields(User, Contact)
|
||||
|
||||
def handle(self, **kwargs):
|
||||
"""Loops through each valid User object and updates its verification_type value"""
|
||||
|
||||
# Don't change the email field.
|
||||
if "email" in self.common_fields:
|
||||
self.common_fields.remove("email")
|
||||
|
||||
filter_condition = {"contact__isnull": False}
|
||||
self.mass_populate_field(User, filter_condition, self.common_fields)
|
||||
|
||||
skipped_users = User.objects.filter(contact__isnull=True)
|
||||
|
||||
if skipped_users and len(skipped_users) > 0:
|
||||
logger.warning(
|
||||
f"""{TerminalColors.YELLOW}
|
||||
===== SKIPPED USERS =====
|
||||
{list(skipped_users)}
|
||||
|
||||
{TerminalColors.ENDC}
|
||||
""",
|
||||
)
|
||||
|
||||
def populate_field(self, object_to_update):
|
||||
"""Defines how we update the user field on mass_populate_field()"""
|
||||
new_value = None
|
||||
for field in self.common_fields:
|
||||
# Grab the value that contact has stored for this field
|
||||
new_value = getattr(object_to_update.contact, field)
|
||||
|
||||
# Set it on the user field
|
||||
setattr(object_to_update, field, new_value)
|
||||
|
||||
logger.info(
|
||||
f"{TerminalColors.OKCYAN}Updating {object_to_update}{TerminalColors.ENDC}"
|
||||
)
|
|
@ -14,10 +14,10 @@ class Command(BaseCommand, PopulateScriptTemplate):
|
|||
filter_condition = {"verification_type__isnull": True}
|
||||
self.mass_populate_field(User, filter_condition, ["verification_type"])
|
||||
|
||||
def populate_field(self, object_to_update):
|
||||
def populate_field(self, field_to_update):
|
||||
"""Defines how we update the verification_type field"""
|
||||
object_to_update.set_user_verification_type()
|
||||
field_to_update.set_user_verification_type()
|
||||
logger.info(
|
||||
f"{TerminalColors.OKCYAN}Updating {object_to_update} => "
|
||||
f"{object_to_update.verification_type}{TerminalColors.OKCYAN}"
|
||||
f"{TerminalColors.OKCYAN}Updating {field_to_update} => "
|
||||
f"{field_to_update.verification_type}{TerminalColors.OKCYAN}"
|
||||
)
|
||||
|
|
|
@ -64,14 +64,6 @@ class PopulateScriptTemplate(ABC):
|
|||
Contains an ABC for generic populate scripts
|
||||
"""
|
||||
|
||||
def get_objects_to_update(self, sender, filter_conditions):
|
||||
"""Given a model of type 'object', perform a filter operation on
|
||||
filter_conditions and return the result.
|
||||
|
||||
For example: User.objects.filter(contact__isnull: False)
|
||||
"""
|
||||
return sender.objects.filter(**filter_conditions)
|
||||
|
||||
def mass_populate_field(self, sender, filter_conditions, fields_to_update):
|
||||
"""Loops through each valid "sender" object - specified by filter_conditions - and
|
||||
updates fields defined by fields_to_update using populate_function.
|
||||
|
@ -79,7 +71,7 @@ class PopulateScriptTemplate(ABC):
|
|||
You must define populate_field before you can use this function.
|
||||
"""
|
||||
|
||||
objects = self.get_objects_to_update(sender, filter_conditions)
|
||||
objects = sender.objects.filter(**filter_conditions)
|
||||
|
||||
# Code execution will stop here if the user prompts "N"
|
||||
TerminalHelper.prompt_for_execution(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue