fix: add --key
(or --keys
) param for import/export commands
This commit is contained in:
parent
15b2cb07ba
commit
a73896b75d
|
@ -135,44 +135,43 @@ class ImportCommandHandler(GenericHandler):
|
|||
|
||||
def import_command_template(
|
||||
|
||||
# model keys
|
||||
models: Annotated[
|
||||
Optional[List[str]],
|
||||
typer.Argument(help="Model(s) to process. Can specify one or more, "
|
||||
"or omit to process default models.")] = None,
|
||||
|
||||
# list models
|
||||
list_models: Annotated[
|
||||
bool,
|
||||
typer.Option('--list-models', '-l',
|
||||
help="List available target models and exit.")] = False,
|
||||
|
||||
# allow create?
|
||||
create: Annotated[
|
||||
bool,
|
||||
typer.Option(help="Allow new target records to be created.")] = True,
|
||||
|
||||
# allow update?
|
||||
update: Annotated[
|
||||
bool,
|
||||
typer.Option(help="Allow existing target records to be updated.")] = True,
|
||||
|
||||
# allow delete?
|
||||
delete: Annotated[
|
||||
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,
|
||||
help="List of fields to process. See also --exclude and --key.")] = None,
|
||||
|
||||
excluded_fields: Annotated[
|
||||
str,
|
||||
typer.Option('--exclude',
|
||||
help="List of fields *not* to process. See also --fields.")] = None,
|
||||
|
||||
# dry run?
|
||||
keys: Annotated[
|
||||
str,
|
||||
typer.Option('--key', '--keys',
|
||||
help="List of fields to use as record key/identifier. See also --fields.")] = None,
|
||||
|
||||
dry_run: Annotated[
|
||||
bool,
|
||||
typer.Option('--dry-run',
|
||||
|
@ -226,6 +225,7 @@ def import_command(fn):
|
|||
|
||||
|
||||
def file_import_command_template(
|
||||
|
||||
input_file_path: Annotated[
|
||||
Path,
|
||||
typer.Option('--input-path',
|
||||
|
@ -233,6 +233,7 @@ def file_import_command_template(
|
|||
help="Path to input file(s). Can be a folder "
|
||||
"if app logic can guess the filename(s); "
|
||||
"otherwise must be complete file path.")] = ...,
|
||||
|
||||
):
|
||||
"""
|
||||
Stub function to provide signature for import/export commands
|
||||
|
|
|
@ -309,10 +309,17 @@ class Importer:
|
|||
|
||||
:returns: List of "key" field names.
|
||||
"""
|
||||
if hasattr(self, 'key'):
|
||||
keys = None
|
||||
# nb. prefer 'keys' but use 'key' as fallback
|
||||
if hasattr(self, 'keys'):
|
||||
keys = self.keys
|
||||
elif hasattr(self, 'key'):
|
||||
keys = self.key
|
||||
if keys:
|
||||
if isinstance(keys, str):
|
||||
keys = [keys]
|
||||
keys = self.config.parse_list(keys)
|
||||
# nb. save for next time
|
||||
self.keys = keys
|
||||
return keys
|
||||
|
||||
return list(get_primary_keys(self.model_class))
|
||||
|
|
|
@ -82,8 +82,10 @@ class TestImporter(DataTestCase):
|
|||
model = self.app.model
|
||||
imp = self.make_importer(model_class=model.Setting)
|
||||
self.assertEqual(imp.get_keys(), ['name'])
|
||||
imp.key = 'value'
|
||||
self.assertEqual(imp.get_keys(), ['value'])
|
||||
with patch.multiple(imp, create=True, key='value'):
|
||||
self.assertEqual(imp.get_keys(), ['value'])
|
||||
with patch.multiple(imp, create=True, keys=['foo', 'bar']):
|
||||
self.assertEqual(imp.get_keys(), ['foo', 'bar'])
|
||||
|
||||
def test_process_data(self):
|
||||
model = self.app.model
|
||||
|
|
Loading…
Reference in a new issue