From f69ae387d59c0437e395983e948459212d5d16e9 Mon Sep 17 00:00:00 2001 From: anikachurilova Date: Tue, 26 Sep 2023 16:39:06 +0200 Subject: [PATCH] communities: add resource and service handlers for setting default community * closes https://github.com/zenodo/rdm-project/issues/188 --- invenio_rdm_records/resources/resources.py | 13 +++++++++ .../services/communities/service.py | 29 +++++++++++++++++++ invenio_rdm_records/services/config.py | 2 ++ 3 files changed, 44 insertions(+) diff --git a/invenio_rdm_records/resources/resources.py b/invenio_rdm_records/resources/resources.py index 775c419c0..d34f70cea 100644 --- a/invenio_rdm_records/resources/resources.py +++ b/invenio_rdm_records/resources/resources.py @@ -296,6 +296,7 @@ def create_url_rules(self): route("POST", routes["list"], self.add), route("DELETE", routes["list"], self.remove), route("GET", routes["suggestions"], self.get_suggestions), + route("PUT", routes["list"], self.set_default), ] return url_rules @@ -368,6 +369,18 @@ def get_suggestions(self): ) return items.to_dict(), 200 + @request_view_args + @request_data + def set_default(self): + """Set default community.""" + item = self.service.set_default( + id_=resource_requestctx.view_args["pid_value"], + identity=g.identity, + data=resource_requestctx.data, + ) + + return item, 200 + class RDMRecordRequestsResource(ErrorHandlersMixin, Resource): """Record requests resource.""" diff --git a/invenio_rdm_records/services/communities/service.py b/invenio_rdm_records/services/communities/service.py index 1d40a7465..cc67b18d4 100644 --- a/invenio_rdm_records/services/communities/service.py +++ b/invenio_rdm_records/services/communities/service.py @@ -50,6 +50,11 @@ def schema(self): """Returns the data schema instance.""" return ServiceSchemaWrapper(self, schema=self.config.schema) + @property + def communities_schema(self): + """Returns the communities schema instance.""" + return ServiceSchemaWrapper(self, schema=self.config.communities_schema) + @property def record_cls(self): """Factory for creating a record class.""" @@ -325,3 +330,27 @@ def search_suggested_communities( extra_filter=communities_filter, **kwargs, ) + + @unit_of_work() + def set_default(self, identity, id_, data, uow): + """Set default community.""" + valid_data, _ = self.communities_schema.load( + data, + context={ + "identity": identity, + }, + raise_errors=True, + ) + + record = self.record_cls.pid.resolve(id_) + self.require_permission(identity, "manage", record=record) + record.parent.communities.default = valid_data["default"] + + uow.register( + ParentRecordCommitOp( + record.parent, + indexer_context=dict(service=current_rdm_records_service), + ) + ) + + return record.parent diff --git a/invenio_rdm_records/services/config.py b/invenio_rdm_records/services/config.py index 7ae2ed2ac..beb884172 100644 --- a/invenio_rdm_records/services/config.py +++ b/invenio_rdm_records/services/config.py @@ -71,6 +71,7 @@ from .schemas.parent.access import Grant as GrantSchema from .schemas.parent.access import RequestAccessSchema from .schemas.parent.access import SecretLink as SecretLinkSchema +from .schemas.parent.communities import CommunitiesSchema from .schemas.quota import QuotaSchema from .schemas.record_communities import RecordCommunitiesSchema from .schemas.tombstone import TombstoneSchema @@ -183,6 +184,7 @@ class RDMRecordCommunitiesConfig(ServiceConfig, ConfiguratorMixin): ) schema = RecordCommunitiesSchema + communities_schema = CommunitiesSchema indexer_cls = RecordIndexer indexer_queue_name = service_id