From ad6b8c1186b628138b5f9643d2875c6a5c958705 Mon Sep 17 00:00:00 2001
From: Maximilian Moser <maximilian.moser@tuwien.ac.at>
Date: Tue, 2 Mar 2021 21:08:57 +0100
Subject: [PATCH] Add 'raw' option to the show operations for drafts and
 records

* this new option allows dumping of the JSON data as stored in the
  database, rather than the record/draft projection
---
 invenio_utilities_tuw/cli/drafts.py  | 7 +++++--
 invenio_utilities_tuw/cli/options.py | 8 ++++++++
 invenio_utilities_tuw/cli/records.py | 7 +++++--
 3 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/invenio_utilities_tuw/cli/drafts.py b/invenio_utilities_tuw/cli/drafts.py
index dcc616f..704ee28 100644
--- a/invenio_utilities_tuw/cli/drafts.py
+++ b/invenio_utilities_tuw/cli/drafts.py
@@ -24,6 +24,7 @@ from .options import (
     option_pid_type,
     option_pid_value,
     option_pretty_print,
+    option_raw,
     option_vanity_pid,
 )
 from .utils import (
@@ -156,15 +157,17 @@ def create_draft(metadata_path, publish, user, owners, vanity_pid):
 @option_pid_type
 @option_as_user
 @option_pretty_print
+@option_raw
 @with_appcontext
-def show_draft(pid, pid_type, user, pretty_print):
+def show_draft(pid, pid_type, user, pretty_print, raw):
     """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)
+    data = draft._record.model.json if raw else draft.data
+    data = json.dumps(data, indent=indent)
     click.echo(data)
 
 
diff --git a/invenio_utilities_tuw/cli/options.py b/invenio_utilities_tuw/cli/options.py
index 62dcc43..afe860b 100644
--- a/invenio_utilities_tuw/cli/options.py
+++ b/invenio_utilities_tuw/cli/options.py
@@ -83,6 +83,14 @@ option_pretty_print = click.option(
     help="pretty-print the result",
 )
 
+option_raw = click.option(
+    "--raw",
+    "raw",
+    default=False,
+    is_flag=True,
+    help="print the raw or projected data",
+)
+
 # 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 ec2ca0c..d656d59 100644
--- a/invenio_utilities_tuw/cli/records.py
+++ b/invenio_utilities_tuw/cli/records.py
@@ -23,6 +23,7 @@ from .options import (
     option_pid_value,
     option_pid_values,
     option_pretty_print,
+    option_raw,
 )
 from .utils import (
     convert_to_recid,
@@ -70,15 +71,17 @@ def list_records(user):
 @option_pid_type
 @option_as_user
 @option_pretty_print
+@option_raw
 @with_appcontext
-def show_record(pid, pid_type, user, pretty_print):
+def show_record(pid, pid_type, user, pretty_print, raw):
     """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)
+    data = record._record.model.json if raw else record.data
+    data = json.dumps(data, indent=indent)
     click.echo(data)
 
 
-- 
GitLab