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