From 2b2e5fe9b8095998471869b92f3239003c6f4c5b Mon Sep 17 00:00:00 2001
From: Maximilian Moser <maximilian.moser@tuwien.ac.at>
Date: Fri, 29 Jan 2021 10:17:57 +0100
Subject: [PATCH] cli: refactor common click options into a single file
---
invenio_utilities_tuw/cli/drafts.py | 36 +-------------
invenio_utilities_tuw/cli/files.py | 33 +++----------
invenio_utilities_tuw/cli/options.py | 74 ++++++++++++++++++++++++++++
invenio_utilities_tuw/cli/records.py | 51 +++----------------
invenio_utilities_tuw/cli/users.py | 16 ++----
5 files changed, 92 insertions(+), 118 deletions(-)
create mode 100644 invenio_utilities_tuw/cli/options.py
diff --git a/invenio_utilities_tuw/cli/drafts.py b/invenio_utilities_tuw/cli/drafts.py
index 2fe9787..784a0fd 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 6f8f3a0..293b34f 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 0000000..7c82dc8
--- /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 6e123eb..3b7c893 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 4048856..eb25c87 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."""
--
GitLab