From 890003d57e5d15a96c0377f03d2f7ebdd5280423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Tue, 4 Dec 2018 19:08:57 +0100 Subject: [PATCH] search: add support for API returning results in a different key (#28606) --- combo/apps/search/models.py | 2 ++ tests/test_search.py | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/combo/apps/search/models.py b/combo/apps/search/models.py index fc8e56d7..64741c00 100644 --- a/combo/apps/search/models.py +++ b/combo/apps/search/models.py @@ -146,6 +146,8 @@ class SearchCell(CellBase): # {{user_nameid}} or {{user_email}}). kwargs['without_user'] = True results = requests.get(url, **kwargs).json() + if service.get('list_key'): + results['data'] = results.get(service['list_key']) or [] hit_templates = {} if service.get('hit_url_template'): hit_templates['url'] = Template(service['hit_url_template']) diff --git a/tests/test_search.py b/tests/test_search.py index 909cb5af..7aaffaf8 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -30,7 +30,12 @@ SEARCH_SERVICES = { 'search_tmpl': { 'label': 'Search with template', 'url': '[search_url]?q=%(q)s', - } + }, + 'search_alternate_key': { + 'label': 'Search with alternate key', + 'url': 'http://www.example.net/search/?q=%(q)s', + 'list_key': 'results', + }, } TEMPLATE_VARS = {'search_url': 'http://search.example.net/'} @@ -90,6 +95,14 @@ def test_search_cell(app): assert '
  • barbarbar' in resp.text assert 'this is html' in resp.text + cell._search_services = {'data': ['search_alternate_key']} + cell.save() + response = {'results': [{'url': 'http://test', 'text': 'barbarbar'}]} + mock_json.json.return_value = response + resp = app.get('/ajax/search/%s/search_alternate_key/?q=foo' % cell.pk, status=200) + assert resp.text.count('
  • ') == 1 + assert '
  • barbarbar' in resp.text + with override_settings(TEMPLATE_VARS=TEMPLATE_VARS): cell._search_services = {'data': ['search_tmpl']} cell.save() @@ -331,10 +344,11 @@ def test_manager_search_cell(app, admin_user): with SearchServices(SEARCH_SERVICES): resp = app.get('/manage/pages/%s/' % page.id) - assert len(resp.form['c%s-_search_services' % cells[0].get_reference()].options) == 4 + assert len(resp.form['c%s-_search_services' % cells[0].get_reference()].options) == 5 # simulate reordering of options resp.form['c%s-_search_services' % cells[0].get_reference()].options = [ (u'search_tmpl', False, u'Search with template'), + (u'search_alternate_key', False, u'Search with alternate key'), (u'_text', False, u'Page Contents'), (u'search1', False, u'Search 1')] resp.form['c%s-_search_services' % cells[0].get_reference()].value = ['search_tmpl', '_text'] -- 2.20.0.rc2