Projet

Général

Profil

0001-search-add-support-for-API-returning-results-in-a-di.patch

Frédéric Péters, 04 décembre 2018 19:11

Télécharger (3,14 ko)

Voir les différences:

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(-)
combo/apps/search/models.py
146 146
        # {{user_nameid}} or {{user_email}}).
147 147
        kwargs['without_user'] = True
148 148
        results = requests.get(url, **kwargs).json()
149
        if service.get('list_key'):
150
            results['data'] = results.get(service['list_key']) or []
149 151
        hit_templates = {}
150 152
        if service.get('hit_url_template'):
151 153
            hit_templates['url'] = Template(service['hit_url_template'])
tests/test_search.py
30 30
    'search_tmpl': {
31 31
        'label': 'Search with template',
32 32
        'url': '[search_url]?q=%(q)s',
33
    }
33
    },
34
    'search_alternate_key': {
35
        'label': 'Search with alternate key',
36
        'url': 'http://www.example.net/search/?q=%(q)s',
37
        'list_key': 'results',
38
    },
34 39
}
35 40

  
36 41
TEMPLATE_VARS = {'search_url': 'http://search.example.net/'}
......
90 95
            assert '<li><a href="http://test">barbarbar</a>' in resp.text
91 96
            assert 'this is <b>html</b>' in resp.text
92 97

  
98
            cell._search_services = {'data': ['search_alternate_key']}
99
            cell.save()
100
            response = {'results': [{'url': 'http://test', 'text': 'barbarbar'}]}
101
            mock_json.json.return_value = response
102
            resp = app.get('/ajax/search/%s/search_alternate_key/?q=foo' % cell.pk, status=200)
103
            assert resp.text.count('<li>') == 1
104
            assert '<li><a href="http://test">barbarbar</a>' in resp.text
105

  
93 106
        with override_settings(TEMPLATE_VARS=TEMPLATE_VARS):
94 107
            cell._search_services = {'data': ['search_tmpl']}
95 108
            cell.save()
......
331 344

  
332 345
    with SearchServices(SEARCH_SERVICES):
333 346
        resp = app.get('/manage/pages/%s/' % page.id)
334
        assert len(resp.form['c%s-_search_services' % cells[0].get_reference()].options) == 4
347
        assert len(resp.form['c%s-_search_services' % cells[0].get_reference()].options) == 5
335 348
        # simulate reordering of options
336 349
        resp.form['c%s-_search_services' % cells[0].get_reference()].options = [
337 350
                (u'search_tmpl', False, u'Search with template'),
351
                (u'search_alternate_key', False, u'Search with alternate key'),
338 352
                (u'_text', False, u'Page Contents'),
339 353
                (u'search1', False, u'Search 1')]
340 354
        resp.form['c%s-_search_services' % cells[0].get_reference()].value = ['search_tmpl', '_text']
341
-