Skip to content

Commit

Permalink
Merge pull request #12 from oarepo/miroslavsimek/be-256-ui-serializer…
Browse files Browse the repository at this point in the history
…-for-apirequests

Adding UI serializer to invenio requests service
  • Loading branch information
mesemus authored Mar 28, 2024
2 parents 1549275 + acd1c77 commit 31c53bb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from functools import cached_property

from flask_resources import ResponseHandler, JSONSerializer
from invenio_records_resources.resources.records.headers import etag_headers
from invenio_records_resources.services.records.params import FilterParam
from invenio_requests.resources.requests.config import RequestSearchRequestArgsSchema, RequestsResourceConfig
from invenio_requests.services.requests.config import RequestSearchOptions, RequestsServiceConfig
from marshmallow import fields
from opensearch_dsl.query import Bool, Term

from oarepo_requests.resources.ui import OARepoRequestsUIJSONSerializer


class RequestOwnerFilterParam(FilterParam):
def apply(self, identity, search, params):
Expand Down Expand Up @@ -44,7 +50,28 @@ class ExtendedRequestSearchRequestArgsSchema(RequestSearchRequestArgsSchema):
assigned = fields.Boolean()


def override_invenio_request_search_options(*args, **kwargs):
# this monkey patch should be done better
def override_invenio_requests_config(*args, **kwargs):
# this monkey patch should be done better (support from invenio)
RequestsServiceConfig.search = EnhancedRequestSearchOptions
RequestsResourceConfig.request_search_args = ExtendedRequestSearchRequestArgsSchema
RequestsResourceConfig.request_search_args = ExtendedRequestSearchRequestArgsSchema

class LazySerializer:
@cached_property
def __instance(self):
return OARepoRequestsUIJSONSerializer()

@property
def serialize_object_list(self):
return self.__instance.serialize_object_list

@property
def serialize_object(self):
return self.__instance.serialize_object


RequestsResourceConfig.response_handlers = {
"application/json": ResponseHandler(JSONSerializer(), headers=etag_headers),
"application/vnd.inveniordm.v1+json": ResponseHandler(
LazySerializer()
)
}
23 changes: 14 additions & 9 deletions oarepo_requests/services/ui_schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import marshmallow as ma
from invenio_pidstore.errors import PIDDeletedError
from invenio_requests.proxies import current_request_type_registry
from marshmallow import validate
from oarepo_runtime.i18n import lazy_gettext as _
Expand Down Expand Up @@ -28,15 +29,19 @@ def create_reference(self, data, **kwargs):

@ma.post_dump
def dereference(self, data, **kwargs):
reference_type = list(data["reference"].keys())[0]
entity_resolvers = current_oarepo_requests.entity_reference_ui_resolvers
if reference_type in entity_resolvers:
return entity_resolvers[reference_type](self.context["identity"], data)
else:
# TODO log warning
return fallback_entity_reference_ui_resolver(self.context["identity"], data)
# raise ValidationError(f"no entity reference handler for {reference_type}")

try:
reference_type = list(data["reference"].keys())[0]
entity_resolvers = current_oarepo_requests.entity_reference_ui_resolvers
if reference_type in entity_resolvers:
return entity_resolvers[reference_type](self.context["identity"], data)
else:
# TODO log warning
return fallback_entity_reference_ui_resolver(self.context["identity"], data)
except PIDDeletedError:
return {
**data,
"status": "removed"
}

class UIRequestSchemaMixin:
created = LocalizedDateTime(dump_only=True)
Expand Down
4 changes: 2 additions & 2 deletions oarepo_requests/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ def create_oarepo_requests(app):
ext = app.extensions["oarepo-requests"]
blueprint = ext.requests_resource.as_blueprint()

from oarepo_requests.services.search_options import override_invenio_request_search_options
blueprint.record_once(override_invenio_request_search_options)
from oarepo_requests.invenio_patches import override_invenio_requests_config
blueprint.record_once(override_invenio_requests_config)

return blueprint

Expand Down
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.9
version = 1.1.10
description =
authors = Ronald Krist <[email protected]>
readme = README.md
Expand Down

0 comments on commit 31c53bb

Please sign in to comment.