From 94b384b8c9abaa0ccbf0eaeefba96fb0fc10e22a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sat, 6 Apr 2019 14:26:12 +0200
Subject: [PATCH 1/3] search: don't display "no result found" when there is no
 query (#32038)

---
 combo/apps/search/models.py                            | 10 ++++++++--
 .../search/templates/combo/search-cell-results.html    |  2 ++
 tests/test_search.py                                   |  8 ++++++++
 3 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/combo/apps/search/models.py b/combo/apps/search/models.py
index 147fe5fb..c06147ff 100644
--- a/combo/apps/search/models.py
+++ b/combo/apps/search/models.py
@@ -115,12 +115,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:
@@ -129,7 +136,6 @@ class SearchCell(CellBase):
         else:
             return render_response()
 
-        query = request.GET.get('q')
         if not query:
             return render_response(service)
 
diff --git a/combo/apps/search/templates/combo/search-cell-results.html b/combo/apps/search/templates/combo/search-cell-results.html
index 75e29681..dae4acb7 100644
--- a/combo/apps/search/templates/combo/search-cell-results.html
+++ b/combo/apps/search/templates/combo/search-cell-results.html
@@ -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 %}
diff --git a/tests/test_search.py b/tests/test_search.py
index e6ee8c9a..987cb393 100644
--- a/tests/test_search.py
+++ b/tests/test_search.py
@@ -80,15 +80,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>'}]
@@ -96,6 +99,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()
-- 
2.20.1

