Skip to content

Commit

Permalink
Merge pull request #25 from oarepo/miroslavsimek/be-324-ui-serializat…
Browse files Browse the repository at this point in the history
…ion-of-request-events-timeline

Miroslavsimek/be 324 UI serialization of request events timeline
  • Loading branch information
mesemus authored Jun 12, 2024
2 parents 4e3f34c + 15a280a commit 25e9605
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 8 deletions.
4 changes: 2 additions & 2 deletions oarepo_requests/resources/events/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from invenio_records_resources.services.base.config import ConfiguratorMixin
from invenio_requests.resources.events.config import RequestCommentsResourceConfig

from oarepo_requests.resources.ui import OARepoRequestsUIJSONSerializer
from oarepo_requests.resources.ui import OARepoRequestEventsUIJSONSerializer


class OARepoRequestsCommentsResourceConfig(
Expand All @@ -22,7 +22,7 @@ class OARepoRequestsCommentsResourceConfig(
def response_handlers(self):
return {
"application/vnd.inveniordm.v1+json": ResponseHandler(
OARepoRequestsUIJSONSerializer()
OARepoRequestEventsUIJSONSerializer()
),
**super().response_handlers,
}
13 changes: 12 additions & 1 deletion oarepo_requests/resources/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ..proxies import current_oarepo_requests
from ..resolvers.ui import resolve
from ..services.ui_schema import UIBaseRequestSchema
from ..services.ui_schema import UIBaseRequestEventSchema, UIBaseRequestSchema
from ..utils import reference_to_tuple


Expand Down Expand Up @@ -86,3 +86,14 @@ def dump_list(self, obj_list, *args, **kwargs):
)
}
return super().dump_list(obj_list, *args, extra_context=extra_context, **kwargs)


class OARepoRequestEventsUIJSONSerializer(LocalizedUIJSONSerializer):
def __init__(self):
"""Initialise Serializer."""
super().__init__(
format_serializer_cls=JSONSerializer,
object_schema_cls=UIBaseRequestEventSchema,
list_schema_cls=BaseListSchema,
schema_context={"object_key": "ui", "identity": g.identity},
)
34 changes: 33 additions & 1 deletion oarepo_requests/services/ui_schema.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import marshmallow as ma
from invenio_pidstore.errors import PIDDeletedError
from invenio_requests.proxies import current_request_type_registry
from invenio_requests.proxies import current_request_type_registry, current_requests
from invenio_requests.services.schemas import (
CommentEventType,
EventTypeMarshmallowField,
)
from marshmallow import validate
from oarepo_runtime.i18n import lazy_gettext as _
from oarepo_runtime.services.schema.marshmallow import BaseRecordSchema
from oarepo_runtime.services.schema.ui import LocalizedDateTime

from oarepo_requests.resolvers.ui import resolve
Expand Down Expand Up @@ -96,3 +101,30 @@ def add_type_details(self, data, **kwargs):
class UIRequestsSerializationMixin(RequestsSchemaMixin):
requests = ma.fields.List(ma.fields.Nested(UIBaseRequestSchema))
request_types = ma.fields.List(ma.fields.Nested(UIRequestTypeSchema))


class UIBaseRequestEventSchema(BaseRecordSchema):
created = LocalizedDateTime(dump_only=True)
updated = LocalizedDateTime(dump_only=True)

type = EventTypeMarshmallowField(dump_only=True)
created_by = ma.fields.Nested(UIReferenceSchema)
permissions = ma.fields.Method("get_permissions", dump_only=True)
payload = ma.fields.Raw()

def get_permissions(self, obj):
"""Return permissions to act on comments or empty dict."""
type = self.get_attribute(obj, "type", None)
is_comment = type == CommentEventType
if is_comment:
service = current_requests.request_events_service
return {
"can_update_comment": service.check_permission(
self.context["identity"], "update_comment", event=obj
),
"can_delete_comment": service.check_permission(
self.context["identity"], "delete_comment", event=obj
),
}
else:
return {}
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 = 1.1.19
version = 1.1.18
description =
authors = Ronald Krist <[email protected]>
readme = README.md
Expand Down
66 changes: 66 additions & 0 deletions tests/test_requests/test_timeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from invenio_requests.records.api import RequestEvent

from tests.test_requests.test_create_inmodel import pick_request_type
from tests.test_requests.utils import link_api2testclient


def test_timeline(
vocab_cf,
logged_client,
users,
urls,
publish_request_data_function,
search_clear,
):
creator = users[0]
creator_client = logged_client(creator)

draft1 = creator_client.post(urls["BASE_URL"], json={})
link = link_api2testclient(
pick_request_type(draft1.json["request_types"], "thesis_publish_draft")[
"links"
]["actions"]["create"]
)

publish_request_resp = creator_client.post(link)
assert publish_request_resp.status_code == 201

publish_request_submit_resp = creator_client.post(
link_api2testclient(publish_request_resp.json["links"]["actions"]["submit"]),
)
assert publish_request_submit_resp.status_code == 200

comment_resp = creator_client.post(
link_api2testclient(publish_request_resp.json["links"]["comments"]),
json={"payload": {"content": "test"}},
)
assert comment_resp.status_code == 201
RequestEvent.index.refresh()

timeline_resp = creator_client.get(
link_api2testclient(publish_request_resp.json["links"]["timeline"]),
)
assert timeline_resp.status_code == 200
assert len(timeline_resp.json["hits"]["hits"]) == 1

# vnd serialization
timeline_resp = creator_client.get(
link_api2testclient(publish_request_resp.json["links"]["timeline"]),
headers={"Accept": "application/vnd.inveniordm.v1+json"},
)
assert timeline_resp.status_code == 200
assert len(timeline_resp.json["hits"]["hits"]) == 1
comment = timeline_resp.json["hits"]["hits"][0]
assert (
comment.items()
>= {
"created_by": {
"reference": {"user": "1"},
"type": "user",
"label": "[email protected]",
"links": {"self": "https://127.0.0.1:5000/api/users/1"},
},
"permissions": {},
"payload": {"content": "test", "format": "html"},
}.items()
)
15 changes: 12 additions & 3 deletions tests/test_ui/test_ui_resource.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json

from invenio_requests.proxies import current_requests_service

from thesis.records.requests.edit_record.types import EditRecordRequestType

allowed_actions = ["submit", "delete"]
Expand Down Expand Up @@ -66,9 +68,16 @@ def test_record_delete_unauthorized(
data = json.loads(c.text)
assert "delete_record" not in data["creatable_request_types"]


def test_request_detail_page(
app, logged_client, record_ui_resource, example_topic, client,
fake_manifest, users, urls
app,
logged_client,
record_ui_resource,
example_topic,
client,
fake_manifest,
users,
urls,
):
creator_client = logged_client(users[0])
creator_identity = users[0].identity
Expand All @@ -78,7 +87,7 @@ def test_request_detail_page(
EditRecordRequestType,
topic=example_topic,
receiver=users[1].user,
creator=users[0].user
creator=users[0].user,
)
# resp_request_submit = creator_client.post(
# link_api2testclient(resp_request_create.json["links"]["actions"]["submit"]),
Expand Down

0 comments on commit 25e9605

Please sign in to comment.