Projet

Général

Profil

0001-allow-substitution-variables-in-search-URLs-16196.patch

Thomas Noël, 05 mai 2017 20:32

Télécharger (3,08 ko)

Voir les différences:

Subject: [PATCH] allow substitution variables in search URLs (#16196)

 combo/apps/search/models.py |  6 ++++--
 tests/test_search.py        | 18 +++++++++++++++++-
 2 files changed, 21 insertions(+), 3 deletions(-)
combo/apps/search/models.py
27 27
from combo.utils import requests
28 28
from combo.data.models import CellBase
29 29
from combo.data.library import register_cell_class
30
from combo.utils import NothingInCacheException
30
from combo.utils import NothingInCacheException, get_templated_url
31 31

  
32 32
@register_cell_class
33 33
class SearchCell(CellBase):
......
101 101

  
102 102
        query = request.GET.get('q')
103 103
        if query and cell.search_service.get('url'):
104
            url = cell.search_service.get('url') % {'q': quote(query.encode('utf-8'))}
104
            url = cell.search_service.get('url')
105
            url = get_templated_url(url)
106
            url = url % {'q': quote(query.encode('utf-8'))}
105 107
            if url.startswith('/'):
106 108
                url = request.build_absolute_uri(url)
107 109
            results = requests.get(url, cache_duration=0).json()
tests/test_search.py
3 3
import mock
4 4

  
5 5
from django.conf import settings
6
from django.test import Client
6
from django.test import Client, override_settings
7 7
from django.test.client import RequestFactory
8 8
from django.template import Context
9 9
from django.core.urlresolvers import reverse
......
23 23
    'search1': {
24 24
        'label': 'Search 1',
25 25
        'url': 'http://www.example.net/search/?q=%(q)s',
26
    },
27
    'search_tmpl': {
28
        'label': 'Search with template',
29
        'url': '[search_url]?q=%(q)s',
26 30
    }
27 31
}
28 32

  
33
TEMPLATE_VARS = {'search_url': 'http://search.example.net/'}
34

  
29 35

  
30 36
class SearchServices(object):
31 37
    def __init__(self, search_services):
......
82 88
            assert '<li><a href="http://test">barbarbar</a>' in resp.content
83 89
            assert 'this is <b>html</b>' in resp.content
84 90

  
91
        with override_settings(TEMPLATE_VARS=TEMPLATE_VARS):
92
            cell._search_service = 'search_tmpl'
93
            cell.save()
94
            with mock.patch('combo.apps.search.models.requests.get') as requests_get:
95
                response = {'err': 0, 'data': []}
96
                mock_json = mock.Mock()
97
                mock_json.json.return_value = response
98
                requests_get.return_value = mock_json
99
                resp = client.get('/ajax/search/%s/?q=foo' % cell.pk, status=200)
100
                assert requests_get.call_args[0][0] == 'http://search.example.net/?q=foo'
85 101

  
86 102
def test_search_global_context(app):
87 103
    with SearchServices(SEARCH_SERVICES):
88
-