From b7b571fb0df4245ea84a911015ce8426320956f2 Mon Sep 17 00:00:00 2001 From: Thomas NOEL Date: Fri, 5 May 2017 20:31:57 +0200 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(-) diff --git a/combo/apps/search/models.py b/combo/apps/search/models.py index fb07a93..1b97f2f 100644 --- a/combo/apps/search/models.py +++ b/combo/apps/search/models.py @@ -27,7 +27,7 @@ from django.utils.http import quote from combo.utils import requests from combo.data.models import CellBase from combo.data.library import register_cell_class -from combo.utils import NothingInCacheException +from combo.utils import NothingInCacheException, get_templated_url @register_cell_class class SearchCell(CellBase): @@ -101,7 +101,9 @@ class SearchCell(CellBase): query = request.GET.get('q') if query and cell.search_service.get('url'): - url = cell.search_service.get('url') % {'q': quote(query.encode('utf-8'))} + url = cell.search_service.get('url') + url = get_templated_url(url) + url = url % {'q': quote(query.encode('utf-8'))} if url.startswith('/'): url = request.build_absolute_uri(url) results = requests.get(url, cache_duration=0).json() diff --git a/tests/test_search.py b/tests/test_search.py index 6b19f97..8374535 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -3,7 +3,7 @@ import pytest import mock from django.conf import settings -from django.test import Client +from django.test import Client, override_settings from django.test.client import RequestFactory from django.template import Context from django.core.urlresolvers import reverse @@ -23,9 +23,15 @@ SEARCH_SERVICES = { 'search1': { 'label': 'Search 1', 'url': 'http://www.example.net/search/?q=%(q)s', + }, + 'search_tmpl': { + 'label': 'Search with template', + 'url': '[search_url]?q=%(q)s', } } +TEMPLATE_VARS = {'search_url': 'http://search.example.net/'} + class SearchServices(object): def __init__(self, search_services): @@ -82,6 +88,16 @@ def test_search_cell(app): assert '
  • barbarbar' in resp.content assert 'this is html' in resp.content + with override_settings(TEMPLATE_VARS=TEMPLATE_VARS): + cell._search_service = 'search_tmpl' + cell.save() + with mock.patch('combo.apps.search.models.requests.get') as requests_get: + response = {'err': 0, 'data': []} + mock_json = mock.Mock() + mock_json.json.return_value = response + requests_get.return_value = mock_json + resp = client.get('/ajax/search/%s/?q=foo' % cell.pk, status=200) + assert requests_get.call_args[0][0] == 'http://search.example.net/?q=foo' def test_search_global_context(app): with SearchServices(SEARCH_SERVICES): -- 2.11.0