diff --git a/cap/modules/deposit/api.py b/cap/modules/deposit/api.py index 38f1d4a781..6876adf6e5 100644 --- a/cap/modules/deposit/api.py +++ b/cap/modules/deposit/api.py @@ -53,6 +53,7 @@ from sqlalchemy.orm.exc import NoResultFound from werkzeug.local import LocalProxy +from cap.modules.deposit.egroups import CERNEgroupMixin from cap.modules.deposit.errors import ( DepositValidationError, DisconnectWebhookError, @@ -99,8 +100,6 @@ get_existing_or_register_user, ) -from cap.modules.deposit.egroups import CERNEgroupMixin - _datastore = LocalProxy(lambda: current_app.extensions["security"].datastore) PRESERVE_FIELDS = ( diff --git a/cap/modules/deposit/links.py b/cap/modules/deposit/links.py index e48b2147af..b08c54007e 100644 --- a/cap/modules/deposit/links.py +++ b/cap/modules/deposit/links.py @@ -58,7 +58,7 @@ def links_factory(pid, record=None, record_hit=None, **kwargs): ) for action in extract_actions_from_class(CAPDeposit): - if action != "review": + if action not in ["review", "egroups"]: links[action] = api_url_for('depid_actions', pid, action=action) return links diff --git a/cap/modules/deposit/serializers/schemas/json.py b/cap/modules/deposit/serializers/schemas/json.py index 2dd21c0cb3..ebb57e05f4 100644 --- a/cap/modules/deposit/serializers/schemas/json.py +++ b/cap/modules/deposit/serializers/schemas/json.py @@ -103,7 +103,7 @@ def remove_skip_values(self, data): keys = ["can_review", "review", "x_cap_permission", "egroups"] for key in keys: - if data.get(key, "") is None: + if not data.get(key, ""): del data[key] return data @@ -116,7 +116,7 @@ def remove_skip_values(self, data): can_admin = fields.Method("can_user_admin", dump_only=True) can_review = fields.Method("can_user_review", dump_only=True) review = fields.Method("get_review", dump_only=True) - links = fields.Method("get_links_with_review", dump_only=True) + links = fields.Method("get_links", dump_only=True) x_cap_permission = fields.Dict(attribute="x-cap-permission", dump_only=True) def get_webhooks(self, obj): @@ -177,12 +177,15 @@ def get_review(self, obj): else: return None - def get_links_with_review(self, obj): + def get_links(self, obj): links = obj["links"] if obj["deposit"].schema_is_reviewable(): links["review"] = links["publish"].replace("publish", "review") + if obj["deposit"].schema_egroups_enabled(): + links["egroups"] = links["publish"].replace("publish", "egroups") + return links def can_user_update(self, obj): diff --git a/cap/modules/records/serializers/__init__.py b/cap/modules/records/serializers/__init__.py index f8dbe839a3..f641b1be7a 100644 --- a/cap/modules/records/serializers/__init__.py +++ b/cap/modules/records/serializers/__init__.py @@ -30,6 +30,8 @@ search_responsify, ) +from cap.modules.records.serializers.author_xml import AuthorXMLSerializer +from cap.modules.records.serializers.csv import CSVSerializer from cap.modules.records.serializers.json import ( CAPJSONSerializer, RecordSerializer, @@ -37,8 +39,6 @@ from cap.modules.records.serializers.schemas.common import ( CommonRecordMetadataSchema, ) -from cap.modules.records.serializers.author_xml import AuthorXMLSerializer -from cap.modules.records.serializers.csv import CSVSerializer from cap.modules.records.serializers.schemas.json import ( BasicDepositSchema, PermissionsDepositSchema, diff --git a/cap/modules/records/serializers/schemas/common.py b/cap/modules/records/serializers/schemas/common.py index dbe80bf5ad..13a33fb4ed 100644 --- a/cap/modules/records/serializers/schemas/common.py +++ b/cap/modules/records/serializers/schemas/common.py @@ -27,7 +27,13 @@ from flask_login import current_user from invenio_files_rest.models import ObjectVersion from invenio_files_rest.serializer import ObjectVersionSchema -from marshmallow import Schema, ValidationError, fields, validates_schema +from marshmallow import ( + Schema, + ValidationError, + fields, + post_dump, + validates_schema, +) from cap.modules.records.utils import url_to_api_url from cap.modules.schemas.resolvers import resolve_schema_by_url @@ -173,6 +179,16 @@ def get_metadata(self, obj): class CommonRecordSchema(CommonRecordMetadataSchema, StrictKeysMixin): """Base record schema.""" + @post_dump + def remove_skip_values(self, data): + keys = ["egroups"] + + for key in keys: + if not data.get(key, ''): + del data[key] + + return data + id = fields.Str(attribute='pid.pid_value', dump_only=True) schema = fields.Method('get_schema', dump_only=True) diff --git a/cap/modules/records/serializers/schemas/json.py b/cap/modules/records/serializers/schemas/json.py index 3b931b10a9..16fc1e0676 100644 --- a/cap/modules/records/serializers/schemas/json.py +++ b/cap/modules/records/serializers/schemas/json.py @@ -79,7 +79,7 @@ def remove_skip_values(self, data): keys = ["can_review", "review", "egroups"] for key in keys: - if data.get(key, '') is None: + if not data.get(key, ''): del data[key] return data @@ -161,6 +161,8 @@ def get_links_with_review(self, obj): action="review", ) + links.pop('egroups', None) + return links diff --git a/cap/modules/schemas/cli.py b/cap/modules/schemas/cli.py index a722c4a31a..3faabe519f 100644 --- a/cap/modules/schemas/cli.py +++ b/cap/modules/schemas/cli.py @@ -167,13 +167,21 @@ def permissions( # create all combinations of actions and roles try: actions_roles = list(itertools.product(requested_actions, roles)) - schema.process_action_roles(schema_action, actions_roles) + roles_logs = schema.process_action_roles(schema_action, actions_roles) # create all combinations of actions and users actions_users = list(itertools.product(requested_actions, users)) - schema.process_action_users(schema_action, actions_users) + users_logs = schema.process_action_users(schema_action, actions_users) except IntegrityError: return click.secho("Action user/role already exists.", fg="red") click.secho("Process finished.", fg="green") + errors = [log for log in roles_logs if log.get('status') == 'error'] + errors += [log for log in users_logs if log.get('status') == 'error'] + + for e in errors: + click.secho( + f"User/Role \"{e.get('role')}\" - \"{e.get('message')}\"", + fg="yellow", + ) @fixtures.command() diff --git a/tests/integration/cli/test_schema_permissions.py b/tests/integration/cli/test_schema_permissions.py index 7e3ff83479..0263f8d7ca 100644 --- a/tests/integration/cli/test_schema_permissions.py +++ b/tests/integration/cli/test_schema_permissions.py @@ -205,4 +205,4 @@ def test_action_allow_read_schema_fails_if_already_in_db(app, db, users, create_ res = cli_runner('fixtures permissions -p read -r test-users@cern.ch --schema --allow test-schema') assert res.exit_code == 0 - assert 'Action user/role already exists.' in res.output + assert 'Already exists' in res.output diff --git a/tests/integration/test_get_records.py b/tests/integration/test_get_records.py index 9339449df0..ad4b33065b 100644 --- a/tests/integration/test_get_records.py +++ b/tests/integration/test_get_records.py @@ -442,7 +442,7 @@ def test_get_record_with_form_json_serializer( }, 'can_update': True, 'is_owner': True, - 'can_review': False, + # 'can_review': False, 'created': rec.created.strftime('%Y-%m-%dT%H:%M:%S.%f+00:00'), 'updated': rec.updated.strftime('%Y-%m-%dT%H:%M:%S.%f+00:00'), 'created_by': {'email': example_user.email, 'profile': {}},