From d7fae4cdd790b5a2dd57494cc9b157051d38f4e8 Mon Sep 17 00:00:00 2001 From: Saksham Date: Wed, 14 Aug 2024 11:34:19 +0200 Subject: [PATCH] services: Add locale to search params via args --- invenio_records_resources/resources/records/args.py | 8 ++++++++ .../services/records/params/querystr.py | 2 +- .../services/records/queryparser/query.py | 7 ++++++- .../services/records/queryparser/suggest.py | 6 +++++- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/invenio_records_resources/resources/records/args.py b/invenio_records_resources/resources/records/args.py index 7f62174d..6dc1c02b 100644 --- a/invenio_records_resources/resources/records/args.py +++ b/invenio_records_resources/resources/records/args.py @@ -9,6 +9,8 @@ """Schemas for parameter parsing.""" +from invenio_i18n import get_locale + from flask_resources.parsers import MultiDictSchema from marshmallow import fields, post_load, validate @@ -29,3 +31,9 @@ def facets(self, data, original_data=None, **kwargs): for k in set(original_data.keys()) - set(data.keys()): data["facets"][k] = original_data.getlist(k) return data + + @post_load + def inject_locale(self, data, **kwargs): + """Inject locale from request context into the args.""" + data["locale"] = str(get_locale()) + return data diff --git a/invenio_records_resources/services/records/params/querystr.py b/invenio_records_resources/services/records/params/querystr.py index b67833ab..4668911c 100644 --- a/invenio_records_resources/services/records/params/querystr.py +++ b/invenio_records_resources/services/records/params/querystr.py @@ -38,7 +38,7 @@ def apply(self, identity, search, params): raise QuerystringValidationError("Invalid 'suggest' parameter.") if query_str: - query = parser_cls(identity).parse(query_str) + query = parser_cls(identity).parse(query_str, locale=params.get("locale")) search = search.query(query) return search diff --git a/invenio_records_resources/services/records/queryparser/query.py b/invenio_records_resources/services/records/queryparser/query.py index 9c685f59..3b9b60b5 100644 --- a/invenio_records_resources/services/records/queryparser/query.py +++ b/invenio_records_resources/services/records/queryparser/query.py @@ -80,6 +80,7 @@ def __init__(self, identity=None, extra_params=None, tree_transformer_cls=None): # fields is not removed from extra params since if given it must be # used in both querystring and multi match self._fields = self.extra_params.get("fields") or [] + self._dynamic_fields = self.extra_params.pop("dynamic_fields", None) or [] @property def allow_list(self): @@ -117,8 +118,12 @@ def factory(cls, tree_transformer_cls=None, **extra_params): tree_transformer_cls=tree_transformer_cls, ) - def parse(self, query_str): + def parse(self, query_str, locale=None): """Parse the query.""" + if locale and self.extra_params.get("fields", None): + self.extra_params["fields"] += [ + field.format(locale=locale) for field in self._dynamic_fields + ] try: # We parse the Lucene query syntax in Python, so we know upfront # if the syntax is correct before executing it in the search engine diff --git a/invenio_records_resources/services/records/queryparser/suggest.py b/invenio_records_resources/services/records/queryparser/suggest.py index 6510832d..aeac39af 100644 --- a/invenio_records_resources/services/records/queryparser/suggest.py +++ b/invenio_records_resources/services/records/queryparser/suggest.py @@ -53,6 +53,10 @@ def __init__(self, identity=None, extra_params=None, **kwargs): super().__init__(identity=identity, extra_params=extra_params) self.extra_params.setdefault("type", "bool_prefix") - def parse(self, query_str): + def parse(self, query_str, locale=None): """Parse the query.""" + if locale and self.extra_params.get("fields", None): + self.extra_params["fields"] += [ + field.format(locale=locale) for field in self._dynamic_fields + ] return dsl.Q("multi_match", query=query_str, **self.extra_params)