search: fix view when indexed cell has no page (#51251)
gitea-wip/combo/pipeline/head Build started... Details
gitea/combo/pipeline/head Build started... Details

This commit is contained in:
Lauréline Guérin 2021-02-18 14:14:51 +01:00
parent 72f2a0efb0
commit 69d97b1c08
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 10 additions and 2 deletions

View File

@ -143,7 +143,7 @@ def search_site(request, query, pages=None, with_description=None):
'text': hit.title, 'text': hit.title,
'rank': getattr(hit, 'rank', None), 'rank': getattr(hit, 'rank', None),
'url': hit.url, '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 seen[hit.url] = True

View File

@ -5,6 +5,7 @@ import mock
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import AnonymousUser, User, Group from django.contrib.auth.models import AnonymousUser, User, Group
from django.contrib.contenttypes.models import ContentType
from django.db import connection from django.db import connection
from django.test import override_settings from django.test import override_settings
from django.test.client import RequestFactory from django.test.client import RequestFactory
@ -401,13 +402,20 @@ def test_search_api(app):
result = result.replace('\n', '') result = result.replace('\n', '')
assert '<li><a href="/second-page/">second page</a></li>' in result assert '<li><a href="/second-page/">second page</a></li>' 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._search_services['options'] = {'_text': {'with_description': True}}
cell.save() cell.save()
resp = app.get('/ajax/search/%s/_text/?q=other' % cell.id, status=200) resp = app.get('/ajax/search/%s/_text/?q=other' % cell.id, status=200)
assert resp.text.count('<li') == 1 assert resp.text.count('<li') == 2
result = resp.text result = resp.text
result = result.replace(' ', '') result = result.replace(' ', '')
result = result.replace('\n', '') result = result.replace('\n', '')
assert '<li><a href="fake">other</a></li>' in result
assert '<li><a href="/second-page/">second page</a><div>Foo Bar Description</div></li>' in result assert '<li><a href="/second-page/">second page</a><div>Foo Bar Description</div></li>' in result
resp = app.get('/ajax/search/%s/_text/?q=baz' % cell.id, status=200) resp = app.get('/ajax/search/%s/_text/?q=baz' % cell.id, status=200)