diff --git a/invenio_utilities_tuw/cli/drafts.py b/invenio_utilities_tuw/cli/drafts.py
index 718c3fb3b8691dc18fcfca58f423a85d0230ca4f..07d393b158d5efb35ed9622a4504c106d178a68e 100644
--- a/invenio_utilities_tuw/cli/drafts.py
+++ b/invenio_utilities_tuw/cli/drafts.py
@@ -15,6 +15,8 @@ from .utils import (
create_record_from_metadata,
get_identity_for_user,
patch_metadata,
+ read_metadata,
+ set_record_owners,
)
option_as_user = click.option(
@@ -42,6 +44,15 @@ option_pid_value = click.option(
required=True,
help="persistent identifier of the record draft to operate on",
)
+option_owners = click.option(
+ "--owner",
+ "-o",
+ "owners",
+ metavar="OWNER",
+ required=False,
+ multiple=True,
+ help="email address of the record owner to set (can be specified multiple times)",
+)
@click.group()
@@ -83,8 +94,9 @@ def list_drafts(user):
default=False,
help="publish the draft after creation (default: false)",
)
+@option_owners
@with_appcontext
-def create_draft(metadata_path, publish, user):
+def create_draft(metadata_path, publish, user, owners):
"""Create a new record draft with the specified metadata.
The specified metadata path can either point to a JSON file containing the metadata,
@@ -97,9 +109,15 @@ def create_draft(metadata_path, publish, user):
"""
recid = None
identity = get_identity_for_user(user)
+ if owners:
+ owners = [get_identity_for_user(owner) for owner in owners]
if isfile(metadata_path):
- draft = create_record_from_metadata(metadata_path, identity)
+ metadata = read_metadata(metadata_path)
+ if owners:
+ metadata = set_record_owners(metadata, owners)
+
+ draft = create_record_from_metadata(metadata, identity)
recid = draft["id"]
elif isdir(metadata_path):
@@ -108,7 +126,11 @@ def create_draft(metadata_path, publish, user):
if not isfile(metadata_file_path):
raise Exception("metadata file does not exist: %s" % metadata_file_path)
- draft = create_record_from_metadata(metadata_file_path, identity)
+ metadata = read_metadata(metadata_file_path)
+ if owners:
+ metadata = set_record_owners(metadata, owners)
+
+ draft = create_record_from_metadata(metadata, identity)
recid = draft["id"]
file_names = []
if isdir(deposit_files_path):
@@ -155,8 +177,9 @@ def create_draft(metadata_path, publish, user):
default=False,
help="replace the draft's metadata entirely, or leave unmentioned fields as-is (default: replace)",
)
+@option_owners
@with_appcontext
-def update_draft(metadata_file, pid, pid_type, user, patch):
+def update_draft(metadata_file, pid, pid_type, user, patch, owners):
"""Update the specified draft's metadata."""
pid = convert_to_recid(pid, pid_type)
identity = get_identity_for_user(user)
@@ -167,6 +190,10 @@ def update_draft(metadata_file, pid, pid_type, user, patch):
draft_data = service.read_draft(id_=pid, identity=identity).data.copy()
metadata = patch_metadata(draft_data, metadata)
+ if owners:
+ owners = [get_identity_for_user(owner) for owner in owners]
+ metadata = set_record_owners(metadata, owners)
+
service.update_draft(id_=pid, identity=identity, data=metadata)
click.secho(pid, fg="green")
diff --git a/invenio_utilities_tuw/cli/records.py b/invenio_utilities_tuw/cli/records.py
index 840e54e60579ecdc62a3327591703fccfd52ce79..44b2747be07baa0abe6840376afe7661a4809e08 100644
--- a/invenio_utilities_tuw/cli/records.py
+++ b/invenio_utilities_tuw/cli/records.py
@@ -12,6 +12,7 @@ from .utils import (
get_identity_for_user,
get_object_uuid,
patch_metadata,
+ set_record_owners,
)
option_as_user = click.option(
@@ -48,6 +49,15 @@ option_pid_values = click.option(
multiple=True,
help="persistent identifier of the record to operate on (can be specified multiple times)",
)
+option_owners = click.option(
+ "--owner",
+ "-o",
+ "owners",
+ metavar="OWNER",
+ required=False,
+ multiple=True,
+ help="email address of the record owner to set (can be specified multiple times)",
+)
@click.group()
@@ -92,8 +102,9 @@ def list_records(user):
default=False,
help="replace the record's metadata entirely, or leave unmentioned fields as-is (default: replace)",
)
+@option_owners
@with_appcontext
-def update_record(metadata_file, pid, pid_type, user, patch):
+def update_record(metadata_file, pid, pid_type, user, patch, owners):
"""Update the specified draft's metadata."""
pid = convert_to_recid(pid, pid_type)
identity = get_identity_for_user(user)
@@ -104,6 +115,10 @@ def update_record(metadata_file, pid, pid_type, user, patch):
record_data = service.read(id_=pid, identity=identity).data.copy()
metadata = patch_metadata(record_data, metadata)
+ if owners:
+ owners = [get_identity_for_user(owner) for owner in owners]
+ metadata = set_record_owners(metadata, owners)
+
service.update(id_=pid, identity=identity, data=metadata)
click.secho(pid, fg="green")
diff --git a/invenio_utilities_tuw/cli/utils.py b/invenio_utilities_tuw/cli/utils.py
index 095e5ad80a5b10abe0d4987c54b6ad46e3d054eb..af3c32a825334039ac014a5dfda96c296b900a7a 100644
--- a/invenio_utilities_tuw/cli/utils.py
+++ b/invenio_utilities_tuw/cli/utils.py
@@ -11,8 +11,8 @@ from invenio_pidstore.models import PersistentIdentifier
from ..utils import get_record_service
-def create_record_from_metadata(metadata_file_path, identity):
- """Create a draft from the metadata in the specified JSON file."""
+def read_metadata(metadata_file_path):
+ """Read the record metadata from the specified JSON file."""
metadata = None
with open(metadata_file_path, "r") as metadata_file:
metadata = json.load(metadata_file)
@@ -20,6 +20,9 @@ def create_record_from_metadata(metadata_file_path, identity):
if metadata is None:
raise Exception("not a valid json file: %s" % metadata_file_path)
+
+def create_record_from_metadata(metadata, identity):
+ """Create a draft from the specified metadata."""
service = get_record_service()
draft = service.create(identity=identity, data=metadata)
return draft
@@ -81,3 +84,15 @@ def convert_to_recid(pid_value, pid_type):
pid_value = query.first().pid_value
return pid_value
+
+
+def set_record_owners(record_metadata, owners):
+ """Set the record's owners, assuming an RDM-Records metadata schema."""
+ metadata = record_metadata.copy()
+
+ owners = [owner.id for owner in owners]
+ if "access" not in metadata:
+ metadata["access"] = {}
+
+ metadata["access"]["owned_by"] = owners
+ return metadata