From 4a2dedf41ae3ae3218d6243cd74b833239340294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= Date: Fri, 12 Feb 2021 14:21:28 +0100 Subject: [PATCH] search: option to display page's description (#51014) --- combo/apps/search/forms.py | 1 + combo/apps/search/manager_views.py | 5 ++-- combo/apps/search/models.py | 6 ++++- combo/apps/search/utils.py | 3 ++- tests/test_search.py | 37 +++++++++++++++++------------- 5 files changed, 32 insertions(+), 20 deletions(-) diff --git a/combo/apps/search/forms.py b/combo/apps/search/forms.py index 4d9ddc78..edb2a030 100644 --- a/combo/apps/search/forms.py +++ b/combo/apps/search/forms.py @@ -54,6 +54,7 @@ class TextEngineSettingsForm(forms.ModelForm): widget=SelectWithDisabled(), ) title = forms.CharField(label=_('Custom Title'), required=False) + with_description = forms.BooleanField(label=_("Display page's description in results"), required=False) class Meta: model = SearchCell diff --git a/combo/apps/search/manager_views.py b/combo/apps/search/manager_views.py index abef9b2c..face7615 100644 --- a/combo/apps/search/manager_views.py +++ b/combo/apps/search/manager_views.py @@ -57,8 +57,9 @@ def page_search_cell_add_engine(request, page_pk, cell_reference, engine_slug): kwargs = { 'title': form.cleaned_data['title'], } - if form.cleaned_data.get('without_user'): - kwargs['without_user'] = True + for key in ['without_user', 'with_description']: + if form.cleaned_data.get(key): + kwargs[key] = True return add_slug( form.get_slug(), **kwargs) diff --git a/combo/apps/search/models.py b/combo/apps/search/models.py index 87c79673..4d2bff83 100644 --- a/combo/apps/search/models.py +++ b/combo/apps/search/models.py @@ -216,7 +216,11 @@ class SearchCell(CellBase): pages = None if service.get('function'): # internal search engine pages = get_root_page_and_children(service_slug) - results = {'data': service['function'](request, query, pages=pages)} + try: + with_description = service['options']['with_description'] + except (KeyError, TypeError): + with_description = None + results = {'data': service['function'](request, query, pages=pages, with_description=with_description)} else: url = get_templated_url(service['url'], context={'request': request, 'q': query, 'search_service': service}) diff --git a/combo/apps/search/utils.py b/combo/apps/search/utils.py index f9dca9af..f404b9b0 100644 --- a/combo/apps/search/utils.py +++ b/combo/apps/search/utils.py @@ -108,7 +108,7 @@ def index_site(): indexed_cell.save() -def search_site(request, query, pages=None): +def search_site(request, query, pages=None, with_description=None): pages = pages or [] if connection.vendor == 'postgresql': @@ -139,6 +139,7 @@ def search_site(request, query, pages=None): 'text': hit.title, 'rank': getattr(hit, 'rank', None), 'url': hit.url, + 'description': hit.page.description if with_description is True else '', }) seen[hit.url] = True if len(hits) == 10: diff --git a/tests/test_search.py b/tests/test_search.py index 4a4e11c2..426c9370 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -370,21 +370,13 @@ def test_search_contents_technical_placeholder(): def test_search_api(app): - page = Page(title='example page', slug='example-page') - page.save() - - cell = TextCell(page=page, placeholder='content', text='

foobar baz

', order=0) - cell.save() - - second_page = Page(title='second page', slug='second-page') - second_page.save() - - cell = TextCell(page=second_page, placeholder='content', text='

other baz

', order=0) - cell.save() + page = Page.objects.create(title='example page', slug='example-page') + TextCell.objects.create(page=page, placeholder='content', text='

foobar baz

', order=0) + second_page = Page.objects.create(title='second page', slug='second-page', description='Foo Bar Description') + TextCell.objects.create(page=second_page, placeholder='content', text='

other baz

', order=0) index_site() - cell = SearchCell(page=page, placeholder='content', _search_services={'data': ['_text']}, order=0) - cell.save() + cell = SearchCell.objects.create(page=page, placeholder='content', _search_services={'data': ['_text']}, order=0) resp = app.get('/ajax/search/%s/_text/?q=foobar' % cell.id, status=200) assert resp.text.count('second page' in result + + cell._search_services['options'] = {'_text': {'with_description': True}} + cell.save() + resp = app.get('/ajax/search/%s/_text/?q=other' % cell.id, status=200) + assert resp.text.count('second page
Foo Bar Description
' in result resp = app.get('/ajax/search/%s/_text/?q=baz' % cell.id, status=200) assert resp.text.count('