diff --git a/invenio_utilities_tuw/cli/drafts.py b/invenio_utilities_tuw/cli/drafts.py
index 2fe9787913c9bca4ad6fedb4af9dd381e4fd0728..784a0fd4bdf82a74db5903eab0de1ff9b4042fe9 100644
--- a/invenio_utilities_tuw/cli/drafts.py
+++ b/invenio_utilities_tuw/cli/drafts.py
@@ -18,6 +18,7 @@ from flask.cli import with_appcontext
from invenio_files_rest.models import ObjectVersion
from ..utils import get_draft_file_service, get_record_service
+from .options import option_as_user, option_owners, option_pid_type, option_pid_value
from .utils import (
convert_to_recid,
create_record_from_metadata,
@@ -27,41 +28,6 @@ from .utils import (
set_record_owners,
)
-option_as_user = click.option(
- "--as-user",
- "-u",
- "user",
- metavar="USER",
- default=None,
- required=True,
- help="email address of the user to use for record creation",
-)
-option_pid_type = click.option(
- "--type",
- "-t",
- "pid_type",
- metavar="PID_TYPE",
- default="recid",
- help="pid type (default: 'recid')",
-)
-option_pid_value = click.option(
- "--pid",
- "-p",
- "pid",
- metavar="PID_VALUE",
- 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()
def drafts():
diff --git a/invenio_utilities_tuw/cli/files.py b/invenio_utilities_tuw/cli/files.py
index 6f8f3a059ff74d9c02add06ee3483f58570227bb..293b34fe5f49b45557479066f3efc2635b2bb6a1 100644
--- a/invenio_utilities_tuw/cli/files.py
+++ b/invenio_utilities_tuw/cli/files.py
@@ -14,36 +14,11 @@ import click
from flask.cli import with_appcontext
from invenio_db import db
from invenio_files_rest.models import Bucket, FileInstance, ObjectVersion
-from invenio_rdm_records.records.models import DraftMetadata, RecordMetadata
from ..utils import get_record_service
+from .options import option_as_user, option_pid_type, option_pid_value
from .utils import convert_to_recid, get_identity_for_user
-option_as_user = click.option(
- "--as-user",
- "-u",
- "user",
- metavar="USER",
- default=None,
- required=True,
- help="email address of the user to use for record creation",
-)
-option_pid_type = click.option(
- "--type",
- "-t",
- "pid_type",
- metavar="PID_TYPE",
- default="recid",
- help="pid type (default: 'recid')",
-)
-option_pid_value = click.option(
- "--pid",
- "-p",
- "pid",
- metavar="PID_VALUE",
- help="persistent identifier of the record draft to operate on",
-)
-
@click.group()
def files():
@@ -157,10 +132,14 @@ def list_orphan_files():
"""List files that aren't referenced in any records (anymore)."""
# TODO iterate over all records & drafts, get their buckets
# and check which buckets from the db aren't listed
+ service = get_record_service()
+ record_model_cls = service.record_cls.model_cls
+ draft_model_cls = service.draft_cls.model_cls
+
bucket_ids = set(
(
r.bucket.id
- for r in (RecordMetadata.query.all() + DraftMetadata.query.all())
+ for r in (record_model_cls.query.all() + draft_model_cls.query.all())
if r.bucket is not None
)
)
diff --git a/invenio_utilities_tuw/cli/options.py b/invenio_utilities_tuw/cli/options.py
new file mode 100644
index 0000000000000000000000000000000000000000..7c82dc891d210fe48c8c69e98f40999ac395b074
--- /dev/null
+++ b/invenio_utilities_tuw/cli/options.py
@@ -0,0 +1,74 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2020-2021 TU Wien.
+#
+# Invenio-Utilities-TUW is free software; you can redistribute it and/or
+# modify it under the terms of the MIT License; see LICENSE file for more
+# details.
+
+"""Common options for CLI commands."""
+
+import click
+
+option_as_user = click.option(
+ "--as-user",
+ "-u",
+ "user",
+ metavar="USER",
+ default=None,
+ required=True,
+ help="email address of the user to impersonate for the task",
+)
+
+option_pid_type = click.option(
+ "--type",
+ "-t",
+ "pid_type",
+ metavar="PID_TYPE",
+ default="recid",
+ help="pid type for the lookup (default: 'recid')",
+)
+
+option_pid_value = click.option(
+ "--pid",
+ "-p",
+ "pid",
+ metavar="PID_VALUE",
+ required=True,
+ help="persistent identifier of the object to operate on",
+)
+
+option_pid_values = click.option(
+ "--pid",
+ "-p",
+ "pids",
+ metavar="PID_VALUE",
+ required=False,
+ multiple=True,
+ help="persistent identifier of the object 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)",
+)
+
+# user management options
+
+option_only_list_active_users = click.option(
+ "--only-active/--include-inactive",
+ "-a/-A",
+ default=True,
+ help="show only active users, or list all users",
+)
+option_hide_user_roles = click.option(
+ "--show-roles/--hide-roles",
+ "-r/-R",
+ default=False,
+ help="show (or hide) the roles associated with the users",
+)
diff --git a/invenio_utilities_tuw/cli/records.py b/invenio_utilities_tuw/cli/records.py
index 6e123eb7c2d421ee57f170a093aa87a2c59999cf..3b7c893492f9e320dac67ff609a27ca44ae8ed88 100644
--- a/invenio_utilities_tuw/cli/records.py
+++ b/invenio_utilities_tuw/cli/records.py
@@ -15,6 +15,13 @@ from flask.cli import with_appcontext
from invenio_files_rest.models import ObjectVersion
from ..utils import get_record_file_service, get_record_service
+from .options import (
+ option_as_user,
+ option_owners,
+ option_pid_type,
+ option_pid_value,
+ option_pid_values,
+)
from .utils import (
convert_to_recid,
get_identity_for_user,
@@ -23,50 +30,6 @@ from .utils import (
set_record_owners,
)
-option_as_user = click.option(
- "--as-user",
- "-u",
- "user",
- metavar="USER",
- default=None,
- required=True,
- help="email address of the user to use for record creation",
-)
-option_pid_type = click.option(
- "--type",
- "-t",
- "pid_type",
- metavar="PID_TYPE",
- default="recid",
- help="pid type (default: 'recid')",
-)
-option_pid_value = click.option(
- "--pid",
- "-p",
- "pid",
- metavar="PID_VALUE",
- required=True,
- help="persistent identifier of the record to operate on",
-)
-option_pid_values = click.option(
- "--pid",
- "-p",
- "pids",
- metavar="PID_VALUE",
- required=False,
- 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()
def records():
diff --git a/invenio_utilities_tuw/cli/users.py b/invenio_utilities_tuw/cli/users.py
index 4048856edfe64babd8998b80a0986d5f3727b1fe..eb25c876f2088080ddd003c6eab8874eba3c692b 100644
--- a/invenio_utilities_tuw/cli/users.py
+++ b/invenio_utilities_tuw/cli/users.py
@@ -12,6 +12,8 @@ import click
from flask.cli import with_appcontext
from invenio_accounts.models import User
+from .options import option_hide_user_roles, option_only_list_active_users
+
@click.group()
def users():
@@ -20,18 +22,8 @@ def users():
@users.command("list")
-@click.option(
- "--only-active/--include-inactive",
- "-a/-A",
- default=True,
- help="show only active users, or list all users",
-)
-@click.option(
- "--show-roles/--hide-roles",
- "-r/-R",
- default=False,
- help="show or hide the roles associated with the users",
-)
+@option_only_list_active_users
+@option_hide_user_roles
@with_appcontext
def list_users(only_active, show_roles):
"""List registered users."""