summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrédéric Péters <fpeters@entrouvert.com>2018-12-04 18:08:57 (GMT)
committerFrédéric Péters <fpeters@entrouvert.com>2018-12-05 10:00:27 (GMT)
commitf598929c341442ceb80bbcc69ee802108e70d934 (patch)
tree79423c55aaf0f7389904658e2217435f4a973111
parentd35ffa3b599491f0b24397daaa8ee149599990d9 (diff)
downloadcombo-f598929c341442ceb80bbcc69ee802108e70d934.zip
combo-f598929c341442ceb80bbcc69ee802108e70d934.tar.gz
combo-f598929c341442ceb80bbcc69ee802108e70d934.tar.bz2
search: add support for API returning results in a different key (#28606)v1.77
-rw-r--r--combo/apps/search/models.py2
-rw-r--r--tests/test_search.py18
2 files changed, 18 insertions, 2 deletions
diff --git a/combo/apps/search/models.py b/combo/apps/search/models.py
index fc8e56d..87a1fba 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('data_key'):
+ results['data'] = results.get(service['data_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 909cb5a..b841e9c 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',
+ 'data_key': 'results',
+ },
}
TEMPLATE_VARS = {'search_url': 'http://search.example.net/'}
@@ -90,6 +95,14 @@ def test_search_cell(app):
assert '<li><a href="http://test">barbarbar</a>' in resp.text
assert 'this is <b>html</b>' 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('<li>') == 1
+ assert '<li><a href="http://test">barbarbar</a>' 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']