Skip to content

Commit

Permalink
Merge pull request #61 from oarepo/miroslavsimek/be-462-add-links-to-…
Browse files Browse the repository at this point in the history
…topic-receiver-creator-directly-on-request

Request links to topic, receiver & creator
  • Loading branch information
Alzpeta authored Sep 27, 2024
2 parents d217af7 + 125f1fd commit 798e872
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
5 changes: 5 additions & 0 deletions oarepo_requests/invenio_patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
OARepoRequestEventsUIJSONSerializer,
OARepoRequestsUIJSONSerializer,
)
from oarepo_requests.services.oarepo.config import OARepoRequestsServiceConfig


class RequestOwnerFilterParam(FilterParam):
Expand Down Expand Up @@ -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):
Expand Down
42 changes: 42 additions & 0 deletions oarepo_requests/services/oarepo/config.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"),
}
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = oarepo-requests
version = 2.1.3
version = 2.1.4
description =
authors = Ronald Krist <[email protected]>
readme = README.md
Expand Down
4 changes: 4 additions & 0 deletions tests/test_requests/test_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down

0 comments on commit 798e872

Please sign in to comment.