fix: add --fields and --exclude params for import/export cli

This commit is contained in:
Lance Edgar 2024-12-05 22:15:38 -06:00
parent f43a066341
commit d14b005fd2
3 changed files with 44 additions and 13 deletions

View file

@ -56,25 +56,22 @@ def importer_command_template(
bool,
typer.Option(help="Allow existing target records to be deleted.")] = False,
# fields
fields: Annotated[
str,
typer.Option('--fields',
help="List of fields to process. See also --exclude.")] = None,
excluded_fields: Annotated[
str,
typer.Option('--exclude',
help="List of fields *not* to process. See also --fields.")] = None,
# dry run?
dry_run: Annotated[
bool,
typer.Option('--dry-run',
help="Go through the motions, but rollback the transaction.")] = False,
# # fields
# fields: Annotated[
# str,
# typer.Option('--fields',
# help="List of fields to process. If specified, "
# "any field not listed is excluded regardless "
# "of --exclude.")] = None,
# exclude_fields: Annotated[
# str,
# typer.Option('--exclude',
# help="List of fields not to process. If "
# "specified, any field not listed is (not?) included "
# "based on app logic and/or --fields.")] = None,
):
"""
Stub function which provides a common param signature; used with

View file

@ -82,6 +82,14 @@ class Importer:
overwrite this attribute directly, for dynamic fields. If so
then ``get_fields()`` will return the new value. And really,
it's probably just as safe to read this attribute directly too.
.. attribute:: excluded_fields
This attribute will often not exist, but is mentioned here for
reference.
It may be specified via constructor param in which case each
field listed therein will be removed from :attr:`fields`.
"""
allow_create = True
@ -183,6 +191,18 @@ class Importer:
self.supported_fields = self.get_supported_fields()
self.fields = self.get_fields()
# fields could be comma-delimited string from cli param
if isinstance(self.fields, str):
self.fields = self.config.parse_list(self.fields)
# discard any fields caller asked to exclude
excluded = getattr(self, 'excluded_fields', None)
if excluded:
if isinstance(excluded, str):
excluded = self.config.parse_list(excluded)
self.fields = [f for f in self.fields
if f not in excluded]
@property
def orientation(self):
"""

View file

@ -36,6 +36,20 @@ class TestImporter(DataTestCase):
self.assertTrue(imp.delete)
self.assertFalse(imp.dry_run)
def test_constructor_fields(self):
model = self.app.model
# basic importer
imp = self.make_importer(model_class=model.Setting, fields='name')
self.assertEqual(imp.fields, ['name'])
def test_constructor_excluded_fields(self):
model = self.app.model
# basic importer
imp = self.make_importer(model_class=model.Setting, excluded_fields='value')
self.assertEqual(imp.fields, ['name'])
def test_get_model_title(self):
model = self.app.model
imp = self.make_importer(model_class=model.Setting)