search: don't display "no result found" when there is no query (#32038)

This commit is contained in:
Frédéric Péters 2019-04-06 14:26:12 +02:00
parent 77b5977c1f
commit 8d9a03f57c
3 changed files with 18 additions and 2 deletions

View File

@ -111,12 +111,19 @@ class SearchCell(CellBase):
if not cell.is_visible(request.user) or not cell.page.is_visible(request.user):
raise PermissionDenied
query = request.GET.get('q')
def render_response(service={}, results={'err': 0, 'data': []}):
template_names = ['combo/search-cell-results.html']
if cell.slug:
template_names.insert(0, 'combo/cells/%s/search-cell-results.html' % cell.slug)
tmpl = template.loader.select_template(template_names)
context = {'cell': cell, 'results': results, 'search_service': service}
context = {
'cell': cell,
'results': results,
'search_service': service,
'query': query
}
return HttpResponse(tmpl.render(context, request), content_type='text/html')
for service in cell.search_services:
@ -125,7 +132,6 @@ class SearchCell(CellBase):
else:
return render_response()
query = request.GET.get('q')
if not query:
return render_response(service)

View File

@ -1,4 +1,5 @@
{% load i18n %}
{% if query %}
{% if cell.has_multiple_search_services %}<p class="search-service-label">{{ search_service.label }}</p>{% endif %}
{% if results.data %}
<div class="links-list">
@ -15,3 +16,4 @@
{% trans "…no result found." %}
</div>
{% endif %}
{% endif %}

View File

@ -79,15 +79,18 @@ def test_search_cell(app):
resp = app.get('/ajax/search/%s/search1/?q=foo' % cell.pk, status=200)
assert requests_get.call_args[0][0] == 'http://www.example.net/search/?q=foo'
assert '<li>' not in resp.text
assert 'no result found' in resp.text
resp = app.get('/ajax/search/%s/search1/?q=foo%%23bar' % cell.pk, status=200)
assert requests_get.call_args[0][0] == 'http://www.example.net/search/?q=foo%23bar'
assert '<li>' not in resp.text
assert 'no result found' in resp.text
response['data'] = [{'url': 'http://test', 'text': 'barbarbar'}]
resp = app.get('/ajax/search/%s/search1/?q=foo' % cell.pk, status=200)
assert resp.text.count('<li>') == 1
assert '<li><a href="http://test">barbarbar</a>' in resp.text
assert 'no result found' not in resp.text
response['data'] = [{'url': 'http://test', 'text': 'barbarbar',
'description': 'this is <b>html</b>'}]
@ -95,6 +98,11 @@ def test_search_cell(app):
assert resp.text.count('<li>') == 1
assert '<li><a href="http://test">barbarbar</a>' in resp.text
assert 'this is <b>html</b>' in resp.text
assert 'no result found' not in resp.text
resp = app.get('/ajax/search/%s/search1/?q=' % cell.pk, status=200)
assert '<li>' not in resp.text
assert 'no result found' not in resp.text
cell._search_services = {'data': ['search_alternate_key']}
cell.save()