handle context vars in jsoncell url (#15152)

This commit is contained in:
Thomas NOËL 2017-02-25 11:23:19 +01:00
parent 571cc6d191
commit 9606dc85fc
2 changed files with 38 additions and 8 deletions

View File

@ -796,12 +796,14 @@ class JsonCell(CellBase):
return bool(self.url) and super(JsonCell, self).is_visible(user=user)
def get_cell_extra_context(self, context):
context = super(JsonCell, self).get_cell_extra_context(context)
url = utils.get_templated_url(self.url)
extra_context = super(JsonCell, self).get_cell_extra_context(context)
self._json_content = None
try:
url = utils.get_templated_url(self.url, context)
except utils.UnknownTemplateVariableError:
logger = logging.getLogger(__name__)
logger.error('unknown variable in template URL (%s)', self.url)
return extra_context
json_response = utils.requests.get(url,
headers={'Accept': 'application/json'},
remote_service='auto',
@ -812,9 +814,8 @@ class JsonCell(CellBase):
except ValueError:
logger = logging.getLogger(__name__)
logger.error('invalid json content')
context['json'] = self._json_content
return context
extra_context['json'] = self._json_content
return extra_context
@property
def template_name(self):

View File

@ -8,6 +8,8 @@ from combo.data.models import Page, CellBase, TextCell, LinkCell, JsonCell
from django.forms.widgets import Media
from django.template import Context
from django.test import override_settings
from django.test.client import RequestFactory
from django.contrib.auth.models import User
from combo.utils import NothingInCacheException
@ -181,3 +183,30 @@ def test_json_cell():
cell.url = 'http://test5'
result = cell.render(Context({'synchronous': True}))
assert result == ''
# URL is a template, using [variables]
cell.cache_duration = 10
data = {'data': [{'url': 'xxx', 'text': 'xxx'}]}
cell.url = 'http://testuser?[foobar]'
with mock.patch('combo.utils.requests.get') as requests_get:
requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200)
context = cell.get_cell_extra_context(Context({'foobar': 'barfoo'}))
assert context['json'] == data
assert requests_get.call_args[0][0] == 'http://testuser?barfoo'
assert requests_get.call_args[1]['cache_duration'] == 10
assert requests_get.call_count == 1
with mock.patch('combo.utils.requests.get') as requests_get:
context = cell.get_cell_extra_context(Context({}))
# can't get URL, 'foobar' variable is missing
assert 'json' not in context
assert requests_get.call_count == 0
request = RequestFactory().get('/')
request.user = User(username='foo', email='foo@example.net')
cell.url = 'http://testuser?email=[user_email]'
with mock.patch('combo.utils.requests.get') as requests_get:
requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200)
context = cell.get_cell_extra_context(Context({'request': request}))
assert context['json'] == data
assert requests_get.call_count == 1
assert requests_get.call_args[0][0] == 'http://testuser?email=foo%40example.net'
assert requests_get.call_args[1]['cache_duration'] == 10