*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects
Commit 2b2e5fe9 authored by Moser, Maximilian's avatar Moser, Maximilian
Browse files

cli: refactor common click options into a single file

parent 0ea042e3
No related branches found
No related tags found
No related merge requests found
......@@ -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():
......
......@@ -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
)
)
......
# -*- 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",
)
......@@ -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():
......
......@@ -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."""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment