From dc1360dd2ba22109a2c5aeedc0cd37c72db4abf7 Mon Sep 17 00:00:00 2001 From: Maximilian Moser <maximilian.moser@tuwien.ac.at> Date: Fri, 29 Jan 2021 13:30:19 +0100 Subject: [PATCH] cli: add commands for showing the stored JSON for records/drafts --- invenio_utilities_tuw/cli/drafts.py | 18 ++++++++++++++++++ invenio_utilities_tuw/cli/options.py | 9 +++++++++ invenio_utilities_tuw/cli/records.py | 18 ++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/invenio_utilities_tuw/cli/drafts.py b/invenio_utilities_tuw/cli/drafts.py index a222b58..50f5479 100644 --- a/invenio_utilities_tuw/cli/drafts.py +++ b/invenio_utilities_tuw/cli/drafts.py @@ -23,6 +23,7 @@ from .options import ( option_owners, option_pid_type, option_pid_value, + option_pretty_print, option_vanity_pid, ) from .utils import ( @@ -147,6 +148,23 @@ def create_draft(metadata_path, publish, user, owners, vanity_pid): click.secho(recid, fg="green") +@drafts.command("show") +@option_pid_value +@option_pid_type +@option_as_user +@option_pretty_print +@with_appcontext +def show_draft(pid, pid_type, user, pretty_print): + """Show the stored data for the specified draft.""" + pid = convert_to_recid(pid, pid_type) + identity = get_identity_for_user(user) + service = get_record_service() + draft = service.read_draft(id_=pid, identity=identity) + indent = 2 if pretty_print else None + data = json.dumps(draft.data, indent=indent) + click.echo(data) + + @drafts.command("update") @click.argument("metadata_file", type=click.File("r")) @option_pid_value diff --git a/invenio_utilities_tuw/cli/options.py b/invenio_utilities_tuw/cli/options.py index de3be22..ff7d761 100644 --- a/invenio_utilities_tuw/cli/options.py +++ b/invenio_utilities_tuw/cli/options.py @@ -75,6 +75,15 @@ option_vanity_pid = click.option( help="vanity PID, to assign to the object (not recommended)", ) +option_pretty_print = click.option( + "--pretty-print", + "-P", + "pretty_print", + default=False, + is_flag=True, + help="pretty-print the result", +) + # user management options option_only_list_active_users = click.option( diff --git a/invenio_utilities_tuw/cli/records.py b/invenio_utilities_tuw/cli/records.py index e621932..9db3c37 100644 --- a/invenio_utilities_tuw/cli/records.py +++ b/invenio_utilities_tuw/cli/records.py @@ -21,6 +21,7 @@ from .options import ( option_pid_type, option_pid_value, option_pid_values, + option_pretty_print, ) from .utils import ( convert_to_recid, @@ -62,6 +63,23 @@ def list_records(user): raise +@records.command("show") +@option_pid_value +@option_pid_type +@option_as_user +@option_pretty_print +@with_appcontext +def show_record(pid, pid_type, user, pretty_print): + """Show the stored data for the specified draft.""" + pid = convert_to_recid(pid, pid_type) + identity = get_identity_for_user(user) + service = get_record_service() + record = service.read(id_=pid, identity=identity) + indent = 2 if pretty_print else None + data = json.dumps(record.data, indent=indent) + click.echo(data) + + @records.command("update") @click.argument("metadata_file", type=click.File("r")) @option_pid_value -- GitLab