From b1b126a9fc56c653c402fecdb17d2b2ea205d551 Mon Sep 17 00:00:00 2001 From: "Esteban J. G. Gabancho" Date: Mon, 24 Jun 2024 10:27:02 +0200 Subject: [PATCH] config: allow request search configuration * closes #384 --- invenio_requests/config.py | 22 ++++++++++++++++++++ invenio_requests/services/requests/config.py | 16 +++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/invenio_requests/config.py b/invenio_requests/config.py index 93274a5d..88cb6891 100644 --- a/invenio_requests/config.py +++ b/invenio_requests/config.py @@ -36,6 +36,28 @@ } """Invenio requests ui endpoints.""" +REQUESTS_SEARCH = { + "facets": ["type", "status"], + "sort": ["bestmatch", "newest", "oldest"], +} +"""Requests search default configuration.""" + +REQUESTS_SORT_OPTIONS = { + "bestmatch": dict( + title=_("Best match"), + fields=["_score"], # search defaults to desc on `_score` field + ), + "newest": dict( + title=_("Newest"), + fields=["-created"], + ), + "oldest": dict( + title=_("Oldest"), + fields=["created"], + ), +} +"""Definitions of available request sort options.""" + REQUESTS_FACETS = { "type": { "facet": facets.type, diff --git a/invenio_requests/services/requests/config.py b/invenio_requests/services/requests/config.py index 3dfa527e..9e7c9e75 100644 --- a/invenio_requests/services/requests/config.py +++ b/invenio_requests/services/requests/config.py @@ -11,7 +11,12 @@ """Requests service configuration.""" from invenio_records_resources.services import RecordServiceConfig, SearchOptions -from invenio_records_resources.services.base.config import ConfiguratorMixin, FromConfig +from invenio_records_resources.services.base.config import ( + ConfiguratorMixin, + FromConfig, + FromConfigSearchOptions, + SearchOptionsMixin, +) from invenio_records_resources.services.records.links import pagination_links from invenio_requests.services.requests import facets @@ -39,7 +44,7 @@ def _is_action_available(request, context): return RequestActions.can_execute(request, action) and permission.allows(identity) -class RequestSearchOptions(SearchOptions): +class RequestSearchOptions(SearchOptions, SearchOptionsMixin): """Search options.""" params_interpreters_cls = SearchOptions.params_interpreters_cls + [ @@ -66,7 +71,12 @@ class RequestsServiceConfig(RecordServiceConfig, ConfiguratorMixin): ) result_item_cls = RequestItem result_list_cls = RequestList - search = RequestSearchOptions + search = FromConfigSearchOptions( + config_key="REQUESTS_SEARCH", + sort_key="REQUESTS_SORT_OPTIONS", + facet_key="REQUESTS_FACETS", + search_option_cls=RequestSearchOptions, + ) # request-specific configuration record_cls = Request # needed for model queries