Skip to content

Commit

Permalink
search: show correctly localized page title in search results
Browse files Browse the repository at this point in the history
fixes #852
  • Loading branch information
goapunk committed Oct 26, 2023
1 parent 0dfe1c6 commit 614c149
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
6 changes: 5 additions & 1 deletion apps/contrib/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 51 additions & 3 deletions apps/contrib/views.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions changelog/852.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- search results show page title in wrong language (#852)
4 changes: 2 additions & 2 deletions digitalstrategie/templates/search_results.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ <h2 class="title">
{% if result.highlight %}
<div class="searchresult-item">
<h3 class="title">
<a href="{% pageurl result %}">{{ result }}</a>
<a href="{% pageurl result %}">{{ result.page_title }}</a>
</h3>
<p class="text">
{{ result.highlight | safe }}
Expand All @@ -68,7 +68,7 @@ <h3 class="title">
aria-hidden="true"
href="{% pageurl result %}"
>
{{ result }}
{{ result.page_title }}
</a>
</p>
</div>
Expand Down

0 comments on commit 614c149

Please sign in to comment.