From 68df6aa3ebb2cee5961fb94964921992e07fcb94 Mon Sep 17 00:00:00 2001 From: Mirek Simek Date: Fri, 27 Sep 2024 08:03:06 +0200 Subject: [PATCH 1/2] Request links --- oarepo_requests/invenio_patches.py | 5 +++ oarepo_requests/services/oarepo/config.py | 42 +++++++++++++++++++++++ tests/test_requests/test_publish.py | 4 +++ 3 files changed, 51 insertions(+) diff --git a/oarepo_requests/invenio_patches.py b/oarepo_requests/invenio_patches.py index 15bbfbab..c48bcc97 100644 --- a/oarepo_requests/invenio_patches.py +++ b/oarepo_requests/invenio_patches.py @@ -19,6 +19,7 @@ OARepoRequestEventsUIJSONSerializer, OARepoRequestsUIJSONSerializer, ) +from oarepo_requests.services.oarepo.config import OARepoRequestsServiceConfig class RequestOwnerFilterParam(FilterParam): @@ -71,6 +72,10 @@ def override_invenio_requests_config(blueprint, *args, **kwargs): RequestsResourceConfig.request_search_args = ( ExtendedRequestSearchRequestArgsSchema ) + # add extra links to the requests + for k, v in OARepoRequestsServiceConfig.links_item.items(): + if k not in RequestsServiceConfig.links_item: + RequestsServiceConfig.links_item[k] = v class LazySerializer: def __init__(self, serializer_cls): diff --git a/oarepo_requests/services/oarepo/config.py b/oarepo_requests/services/oarepo/config.py index cae82c5a..e096995d 100644 --- a/oarepo_requests/services/oarepo/config.py +++ b/oarepo_requests/services/oarepo/config.py @@ -1,6 +1,42 @@ +from invenio_requests.records.api import Request from invenio_requests.services import RequestsServiceConfig from invenio_requests.services.requests import RequestLink +from oarepo_requests.resolvers.ui import resolve + + +class RequestEntityLink(RequestLink): + def __init__(self, uritemplate, when=None, vars=None, entity="topic"): + super().__init__(uritemplate, when, vars) + self.entity = entity + + def vars(self, record: Request, vars): + super().vars(record, vars) + entity = self._resolve(record, vars) + self._expand_entity(entity, vars) + return vars + + def should_render(self, obj, ctx): + if not super().should_render(obj, ctx): + return False + if self.expand(obj, ctx): + return True + + def _resolve(self, obj, ctx): + reference_dict = getattr(obj, self.entity).reference_dict + key = "entity:" + ":".join(f"{x[0]}:{x[1]}" for x in sorted(reference_dict.items())) + if key in ctx: + return ctx[key] + try: + entity = resolve(ctx["identity"], reference_dict) + except Exception: # noqa + entity = {} + ctx[key] = entity + return entity + + def _expand_entity(self, entity, vars): + vars.update({f"entity_{k}" : v for k, v in entity.get("links", {}).items()}) + class OARepoRequestsServiceConfig(RequestsServiceConfig): service_id = "oarepo_requests" @@ -9,4 +45,10 @@ class OARepoRequestsServiceConfig(RequestsServiceConfig): "self": RequestLink("{+api}/requests/extended/{id}"), "comments": RequestLink("{+api}/requests/extended/{id}/comments"), "timeline": RequestLink("{+api}/requests/extended/{id}/timeline"), + "topic": RequestEntityLink("{+entity_self}"), + "topic_html": RequestEntityLink("{+entity_self_html}"), + "created_by": RequestEntityLink("{+entity_self}", entity="created_by"), + "created_by_html": RequestEntityLink("{+entity_self_html}", entity="created_by"), + "receiver": RequestEntityLink("{+entity_self}", entity="receiver"), + "receiver_html": RequestEntityLink("{+entity_self_html}", entity="receiver"), } diff --git a/tests/test_requests/test_publish.py b/tests/test_requests/test_publish.py index 958d0f59..ae892b61 100644 --- a/tests/test_requests/test_publish.py +++ b/tests/test_requests/test_publish.py @@ -24,6 +24,10 @@ def test_publish_service(users, record_service, default_workflow_json, search_cl submit_result = current_invenio_requests_service.execute_action( creator.identity, request.id, "submit" ) + assert "created_by" in submit_result.links + assert "topic" in submit_result.links + assert "topic_html" in submit_result.links + accept_result = current_invenio_requests_service.execute_action( receiver.identity, request.id, "accept" ) From 125f1fd935cccc65acde78ef29197d64e949b777 Mon Sep 17 00:00:00 2001 From: Mirek Simek Date: Fri, 27 Sep 2024 08:13:44 +0200 Subject: [PATCH 2/2] Version bump --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index adc96590..51a60fcf 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = oarepo-requests -version = 2.1.3 +version = 2.1.4 description = authors = Ronald Krist readme = README.md