From 614c149f1095051a27500319e2a4d706ad5eac4b Mon Sep 17 00:00:00 2001 From: Julian Dehm Date: Thu, 26 Oct 2023 13:58:04 +0200 Subject: [PATCH] search: show correctly localized page title in search results fixes #852 --- apps/contrib/translations.py | 6 ++- apps/contrib/views.py | 54 +++++++++++++++++-- changelog/852.md | 3 ++ .../templates/search_results.html | 4 +- 4 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 changelog/852.md diff --git a/apps/contrib/translations.py b/apps/contrib/translations.py index 8351b4d8..92dfd1bb 100644 --- a/apps/contrib/translations.py +++ b/apps/contrib/translations.py @@ -50,4 +50,8 @@ def get_search_fields() -> List[str]: '*body_', ] lang = translation.get_language() - return [f + lang + '_edgengrams' for f in fields] + localized_fields = [] + for f in fields: + localized_fields.append(f + lang) + localized_fields.append(f + lang + "_edgengrams") + return localized_fields diff --git a/apps/contrib/views.py b/apps/contrib/views.py index 3da040eb..1e5610a6 100644 --- a/apps/contrib/views.py +++ b/apps/contrib/views.py @@ -1,15 +1,63 @@ +from itertools import chain +from typing import Any +from typing import List + from django.views.generic import ListView -from wagtail.models import Page from wagtail.search.backends import get_search_backend from apps.contrib.translations import get_search_fields +from apps.gruenbuch.models import GruenbuchDetailPage +from apps.gruenbuch.models import GruenbuchIndexPage +from apps.gruenbuch.models import GruenbuchOverviewPage +from apps.home.models import DetailPage +from apps.home.models import HomePage +from apps.home.models import MicrositeDetailPage +from apps.home.models import MicrositeOverviewPage +from apps.home.models import OverviewPage +from apps.home.models import SimplePage +from apps.measures.models import MeasuresDetailPage +from apps.measures.models import MeasuresOverviewPage class SearchResultsView(ListView): template_name = 'search_results.html' - def get_queryset(self): + def get_queryset(self) -> List[Any]: + """Return list with all search results from the different pages. + + This is an inefficient workaround for + `sb.autocomplete(query, Page, fields=get_search_fields())` not + returning the title in the correct language as the wagtail.models.Page + doesn't know about it. The proper way would be to subclass Page with + the translated title field and make all custom models inherit from that + subclass. + + Returns: + List of all search results + + """ sb = get_search_backend() query = self.request.GET.get('q') - res = sb.autocomplete(query, Page, fields=get_search_fields()) + # FIXME: Should use one query over a common page model inherited by all + # models below + res = list(chain( + sb.autocomplete(query, HomePage, fields=get_search_fields()), + sb.autocomplete(query, OverviewPage, fields=get_search_fields()), + sb.autocomplete(query, DetailPage, fields=get_search_fields()), + sb.autocomplete(query, MicrositeOverviewPage, + fields=get_search_fields()), + sb.autocomplete(query, MicrositeDetailPage, + fields=get_search_fields()), + sb.autocomplete(query, SimplePage, fields=get_search_fields()), + sb.autocomplete(query, GruenbuchOverviewPage, + fields=get_search_fields()), + sb.autocomplete(query, GruenbuchDetailPage, + fields=get_search_fields()), + sb.autocomplete(query, GruenbuchIndexPage, + fields=get_search_fields()), + sb.autocomplete(query, MeasuresOverviewPage, + fields=get_search_fields()), + sb.autocomplete(query, MeasuresDetailPage, + fields=get_search_fields()) + )) return res diff --git a/changelog/852.md b/changelog/852.md new file mode 100644 index 00000000..5a2c1cd3 --- /dev/null +++ b/changelog/852.md @@ -0,0 +1,3 @@ +### Fixed + +- search results show page title in wrong language (#852) diff --git a/digitalstrategie/templates/search_results.html b/digitalstrategie/templates/search_results.html index 85920451..7a9ce68b 100644 --- a/digitalstrategie/templates/search_results.html +++ b/digitalstrategie/templates/search_results.html @@ -57,7 +57,7 @@

{% if result.highlight %}

- {{ result }} + {{ result.page_title }}

{{ result.highlight | safe }} @@ -68,7 +68,7 @@

aria-hidden="true" href="{% pageurl result %}" > - {{ result }} + {{ result.page_title }}