From 77e5c918e7cacf5cba4e216dbaeae9ccf77c261e Mon Sep 17 00:00:00 2001 From: David Kennedy Date: Tue, 4 Jun 2024 11:51:39 -0400 Subject: [PATCH] added skipEppSave command line option, may change option in future commit --- src/registrar/admin.py | 48 +++++++++++++++---- .../management/commands/import_tables.py | 9 +++- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index ce00bbc88..6573079f9 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -2252,18 +2252,46 @@ class PublicContactResource(resources.ModelResource): class Meta: model = models.PublicContact - def import_row(self, row, instance_loader, using_transactions=True, dry_run=False, raise_errors=None, **kwargs): - """Override kwargs skip_epp_save and set to True""" - kwargs["skip_epp_save"] = True - return super().import_row( - row, - instance_loader, - using_transactions=using_transactions, - dry_run=dry_run, - raise_errors=raise_errors, + def __init__(self): + """Sets global variables for code tidyness""" + super().__init__() + self.skip_epp_save=False + + def import_data( + self, + dataset, + dry_run=False, + raise_errors=False, + use_transactions=None, + collect_failed_rows=False, + rollback_on_validation_errors=False, + **kwargs + ): + """Override import_data to set self.skip_epp_save if in kwargs""" + self.skip_epp_save = kwargs.get('skip_epp_save', False) + return super().import_data( + dataset, + dry_run, + raise_errors, + use_transactions, + collect_failed_rows, + rollback_on_validation_errors, **kwargs, ) + # def import_row(self, row, instance_loader, using_transactions=True, dry_run=False, raise_errors=None, **kwargs): + # """Override kwargs skip_epp_save and set to True""" + # kwargs["skip_epp_save"] = True + # super().import_data() + # return super().import_row( + # row, + # instance_loader, + # using_transactions=using_transactions, + # dry_run=dry_run, + # raise_errors=raise_errors, + # **kwargs, + # ) + def save_instance(self, instance, is_create, using_transactions=True, dry_run=False): """Override save_instance setting skip_epp_save to True""" self.before_save_instance(instance, using_transactions, dry_run) @@ -2276,7 +2304,7 @@ class PublicContactResource(resources.ModelResource): # we don't have transactions and we want to do a dry_run pass else: - instance.save(skip_epp_save=True) + instance.save(skip_epp_save=self.skip_epp_save) self.after_save_instance(instance, using_transactions, dry_run) diff --git a/src/registrar/management/commands/import_tables.py b/src/registrar/management/commands/import_tables.py index 3594d3215..cbe3dc6ee 100644 --- a/src/registrar/management/commands/import_tables.py +++ b/src/registrar/management/commands/import_tables.py @@ -1,3 +1,4 @@ +import argparse import logging import os import pyzipper @@ -14,6 +15,10 @@ logger = logging.getLogger(__name__) class Command(BaseCommand): help = "Imports tables from a zip file, exported_tables.zip, containing CSV files in the tmp directory." + def add_arguments(self, parser): + """Add command line arguments.""" + parser.add_argument('--skipEppSave', default=True, action=argparse.BooleanOptionalAction) + def handle(self, **options): """Extracts CSV files from a zip archive and imports them into the respective tables""" @@ -21,6 +26,8 @@ class Command(BaseCommand): logger.error("import_tables cannot be run in production") return + self.skip_epp_save = options.get("skipEppSave") + table_names = [ "User", "Contact", @@ -73,7 +80,7 @@ class Command(BaseCommand): resource_instance = resourceclass() with open(csv_filename, "r") as csvfile: dataset = tablib.Dataset().load(csvfile.read(), format="csv") - result = resource_instance.import_data(dataset, dry_run=False, skip_epp_save=True) + result = resource_instance.import_data(dataset, dry_run=False, skip_epp_save=self.skip_epp_save) if result.has_errors(): logger.error(f"Errors occurred while importing {csv_filename}: {result.row_errors()}")