search: option to display page's description (#51014)
This commit is contained in:
parent
1b66f4106c
commit
4a2dedf41a
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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})
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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='<p>foobar baz</p>', order=0)
|
||||
cell.save()
|
||||
|
||||
second_page = Page(title='second page', slug='second-page')
|
||||
second_page.save()
|
||||
|
||||
cell = TextCell(page=second_page, placeholder='content', text='<p>other baz</p>', order=0)
|
||||
cell.save()
|
||||
page = Page.objects.create(title='example page', slug='example-page')
|
||||
TextCell.objects.create(page=page, placeholder='content', text='<p>foobar baz</p>', 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='<p>other baz</p>', 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('<li') == 1
|
||||
|
@ -392,7 +384,19 @@ def test_search_api(app):
|
|||
|
||||
resp = app.get('/ajax/search/%s/_text/?q=other' % cell.id, status=200)
|
||||
assert resp.text.count('<li') == 1
|
||||
assert 'second page' in resp.text
|
||||
result = resp.text
|
||||
result = result.replace(' ', '')
|
||||
result = result.replace('\n', '')
|
||||
assert '<li><a href="/second-page/">second page</a></li>' 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('<li') == 1
|
||||
result = resp.text
|
||||
result = result.replace(' ', '')
|
||||
result = result.replace('\n', '')
|
||||
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)
|
||||
assert resp.text.count('<li') == 2
|
||||
|
@ -594,17 +598,18 @@ def test_manager_search_cell(settings, app, admin_user):
|
|||
assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.pk, cell.get_reference()))
|
||||
cell.refresh_from_db()
|
||||
|
||||
# add engine on page and sub pages, with a custom title
|
||||
# add engine on page and sub pages, with a custom title and description
|
||||
resp = app.get('/manage/pages/%s/' % page.pk)
|
||||
resp = resp.click(href='.*/search_searchcell-%s/engine/_text/add/' % cell.pk)
|
||||
resp.form['selected_page'] = page.pk
|
||||
resp.form['title'] = 'Custom Title'
|
||||
resp.form['with_description'] = True
|
||||
resp = resp.form.submit('submit')
|
||||
assert resp.status_int == 302
|
||||
assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.pk, cell.get_reference()))
|
||||
cell.refresh_from_db()
|
||||
assert cell._search_services['data'] == ['search1', 'search_tmpl', '_text', '_text_page_one']
|
||||
assert cell._search_services['options']['_text_page_one'] == {'title': 'Custom Title'}
|
||||
assert cell._search_services['options']['_text_page_one'] == {'title': 'Custom Title', 'with_description': True}
|
||||
|
||||
|
||||
def test_manager_search_cell_order(settings, app, admin_user):
|
||||
|
|
Loading…
Reference in New Issue