json cell: prioritise varnames in context (#17904)

This commit is contained in:
Thomas NOËL 2017-08-07 15:02:01 +02:00 committed by Frédéric Péters
parent 5758be6863
commit 04f1fd1d82
2 changed files with 21 additions and 4 deletions

View File

@ -854,7 +854,7 @@ class JsonCellBase(CellBase):
extra_context = super(JsonCellBase, self).get_cell_extra_context(context)
if self.varnames and context.get('request'):
for varname in self.varnames:
if varname in context['request'].GET and varname not in context:
if varname in context['request'].GET:
context[varname] = context['request'].GET[varname]
self._json_content = None
@ -1069,8 +1069,11 @@ class ConfigJsonCell(JsonCellBase):
return config_form_class
def get_cell_extra_context(self, context, **kwargs):
context.update(copy.copy(self.parameters)) # early push for templated URLs
ctx = super(ConfigJsonCell, self).get_cell_extra_context(context, **kwargs)
# create a new context with parameters, for templated URLs
ctx = copy.copy(context)
ctx['parameters'] = self.parameters
ctx.update(self.parameters)
ctx.update(copy.copy(self.parameters))
# add varnames, get requests results
extra = super(ConfigJsonCell, self).get_cell_extra_context(ctx, **kwargs)
ctx.update(extra)
return ctx

View File

@ -333,6 +333,20 @@ def test_config_json_cell_with_varnames(app):
assert '/var1=foo/' in resp.body
assert '/var2=bar/' in resp.body
resp = app.get(url + '?var2=plop') # use var1 default value
assert requests_get.call_count == 1 # no request, var1 is missing
assert '/var1=/' in resp.body
assert '/var2=plop/' in resp.body
cell.parameters = {'var1': 'defaultvalue1'}
cell.save()
resp = app.get(url + '?var2=plop') # use var1 default value
assert requests_get.call_count == 2
assert requests_get.call_args[0][0] == 'http://foo?varone=defaultvalue1&vartwo=plop'
assert '/var1=defaultvalue1/' in resp.body
assert '/var2=plop/' in resp.body
def test_config_json_cell_with_param_in_url(app):
page = Page(title='example page', slug='index')
page.save()