Skip to content

Commit

Permalink
communities: add resource and service handlers for setting default co…
Browse files Browse the repository at this point in the history
…mmunity

* closes zenodo/rdm-project#188
  • Loading branch information
anikachurilova authored and kpsherva committed Oct 4, 2023
1 parent a12e3bb commit f69ae38
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
13 changes: 13 additions & 0 deletions invenio_rdm_records/resources/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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."""
Expand Down
29 changes: 29 additions & 0 deletions invenio_rdm_records/services/communities/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions invenio_rdm_records/services/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -183,6 +184,7 @@ class RDMRecordCommunitiesConfig(ServiceConfig, ConfiguratorMixin):
)

schema = RecordCommunitiesSchema
communities_schema = CommunitiesSchema

indexer_cls = RecordIndexer
indexer_queue_name = service_id
Expand Down

0 comments on commit f69ae38

Please sign in to comment.