From 69d97b1c08e898e842aaf5568d509d32e2be9435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= Date: Thu, 18 Feb 2021 14:14:51 +0100 Subject: [PATCH] search: fix view when indexed cell has no page (#51251) --- combo/apps/search/utils.py | 2 +- tests/test_search.py | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/combo/apps/search/utils.py b/combo/apps/search/utils.py index 4957551a..addb1e5d 100644 --- a/combo/apps/search/utils.py +++ b/combo/apps/search/utils.py @@ -143,7 +143,7 @@ def search_site(request, query, pages=None, with_description=None): 'text': hit.title, 'rank': getattr(hit, 'rank', None), 'url': hit.url, - 'description': hit.page.description if with_description is True else '', + 'description': hit.page.description if (hit.page and with_description is True) else '', } ) seen[hit.url] = True diff --git a/tests/test_search.py b/tests/test_search.py index 273220b2..ce19ca3e 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -5,6 +5,7 @@ import mock from django.conf import settings from django.contrib.auth.models import AnonymousUser, User, Group +from django.contrib.contenttypes.models import ContentType from django.db import connection from django.test import override_settings from django.test.client import RequestFactory @@ -401,13 +402,20 @@ def test_search_api(app): result = result.replace('\n', '') assert '
  • second page
  • ' in result + # indexed cell without page + cell_type = ContentType.objects.get_for_model(cell) + IndexedCell.objects.create( + title='other', cell_type=cell_type, cell_pk=cell.pk, public_access=True, url='fake' + ) + # enable description 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('other' in result assert '
  • second page
    Foo Bar Description
  • ' in result resp = app.get('/ajax/search/%s/_text/?q=baz' % cell.id, status=200)