fix: add --fields
and --exclude
params for import/export cli
This commit is contained in:
parent
f43a066341
commit
d14b005fd2
|
@ -56,25 +56,22 @@ def importer_command_template(
|
||||||
bool,
|
bool,
|
||||||
typer.Option(help="Allow existing target records to be deleted.")] = False,
|
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?
|
||||||
dry_run: Annotated[
|
dry_run: Annotated[
|
||||||
bool,
|
bool,
|
||||||
typer.Option('--dry-run',
|
typer.Option('--dry-run',
|
||||||
help="Go through the motions, but rollback the transaction.")] = False,
|
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
|
Stub function which provides a common param signature; used with
|
||||||
|
|
|
@ -82,6 +82,14 @@ class Importer:
|
||||||
overwrite this attribute directly, for dynamic fields. If so
|
overwrite this attribute directly, for dynamic fields. If so
|
||||||
then ``get_fields()`` will return the new value. And really,
|
then ``get_fields()`` will return the new value. And really,
|
||||||
it's probably just as safe to read this attribute directly too.
|
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
|
allow_create = True
|
||||||
|
@ -183,6 +191,18 @@ class Importer:
|
||||||
self.supported_fields = self.get_supported_fields()
|
self.supported_fields = self.get_supported_fields()
|
||||||
self.fields = self.get_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
|
@property
|
||||||
def orientation(self):
|
def orientation(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -36,6 +36,20 @@ class TestImporter(DataTestCase):
|
||||||
self.assertTrue(imp.delete)
|
self.assertTrue(imp.delete)
|
||||||
self.assertFalse(imp.dry_run)
|
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):
|
def test_get_model_title(self):
|
||||||
model = self.app.model
|
model = self.app.model
|
||||||
imp = self.make_importer(model_class=model.Setting)
|
imp = self.make_importer(model_class=model.Setting)
|
||||||
|
|
Loading…
Reference in a new issue