misc: pass configured json cell parameters to action URL templates (#30885)
This commit is contained in:
parent
fb718c6c47
commit
c47ef6fd4d
|
@ -1060,6 +1060,9 @@ class JsonCellBase(CellBase):
|
|||
def is_visible(self, user=None):
|
||||
return bool(self.url) and super(JsonCellBase, self).is_visible(user=user)
|
||||
|
||||
def get_cell_parameters_context(self):
|
||||
return {}
|
||||
|
||||
def get_cell_extra_context(self, context, invalidate_cache=False):
|
||||
extra_context = super(JsonCellBase, self).get_cell_extra_context(context)
|
||||
if context.get('placeholder_search_mode'):
|
||||
|
@ -1071,6 +1074,7 @@ class JsonCellBase(CellBase):
|
|||
context[varname] = context['request'].GET[varname]
|
||||
self._json_content = None
|
||||
|
||||
context.update(self.get_cell_parameters_context())
|
||||
context['concerned_user'] = self.get_concerned_user(context)
|
||||
|
||||
data_urls = [{'key': self.first_data_key, 'url': self.url, 'cache_duration': self.cache_duration,
|
||||
|
@ -1177,6 +1181,7 @@ class JsonCellBase(CellBase):
|
|||
content[key] = value
|
||||
|
||||
context = copy.copy(content)
|
||||
context.update(self.get_cell_parameters_context())
|
||||
context['request'] = request
|
||||
context['synchronous'] = True
|
||||
|
||||
|
@ -1338,15 +1343,8 @@ class ConfigJsonCell(JsonCellBase):
|
|||
(ConfigJsonForm,), {'formdef': formdef})
|
||||
return config_form_class
|
||||
|
||||
def get_cell_extra_context(self, context, **kwargs):
|
||||
# create a new context with parameters, for templated URLs
|
||||
ctx = copy.copy(context)
|
||||
ctx['parameters'] = 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
|
||||
def get_cell_parameters_context(self):
|
||||
return self.parameters or {}
|
||||
|
||||
|
||||
class ExternalLinkSearchItem(models.Model):
|
||||
|
|
|
@ -490,17 +490,20 @@ def test_post_cell(app):
|
|||
with override_settings(JSON_CELL_TYPES={
|
||||
'test-post-cell': {
|
||||
'name': 'Foobar',
|
||||
'url': 'http://test-post-cell/',
|
||||
'url': 'http://test-post-cell/{{slug}}/',
|
||||
'form': [
|
||||
{'label': 'slug', 'varname': 'slug', 'required': True},
|
||||
],
|
||||
'actions': {
|
||||
'create': {
|
||||
'url': 'http://test-post-cell/create/',
|
||||
'url': 'http://test-post-cell/{{slug}}/create/',
|
||||
},
|
||||
'update': {
|
||||
'url': 'http://test-post-cell/update/',
|
||||
'url': 'http://test-post-cell/{{slug}}/update/',
|
||||
'method': 'PATCH',
|
||||
},
|
||||
'search': {
|
||||
'url': 'http://test-post-cell/search/',
|
||||
'url': 'http://test-post-cell/{{slug}}/search/',
|
||||
'method': 'GET',
|
||||
'response': 'raw'
|
||||
},
|
||||
|
@ -510,6 +513,7 @@ def test_post_cell(app):
|
|||
):
|
||||
cell = ConfigJsonCell(page=page, placeholder='content', order=0)
|
||||
cell.key = 'test-post-cell'
|
||||
cell.parameters = {'slug': 'slug'}
|
||||
cell.save()
|
||||
|
||||
with mock.patch('combo.utils.requests.get') as requests_get:
|
||||
|
@ -523,7 +527,7 @@ def test_post_cell(app):
|
|||
requests_post.return_value = mock.Mock(content=json.dumps({'err': 0}), status_code=200)
|
||||
resp2 = resp.form.submit()
|
||||
assert requests_post.call_args[0][0] == 'POST'
|
||||
assert requests_post.call_args[0][1] == 'http://test-post-cell/create/'
|
||||
assert requests_post.call_args[0][1] == 'http://test-post-cell/slug/create/'
|
||||
assert requests_post.call_args[1]['json'] == {'value': 'plop', 'items': ['1']}
|
||||
assert urlparse.urlparse(resp2.location).path == '/'
|
||||
|
||||
|
@ -532,7 +536,7 @@ def test_post_cell(app):
|
|||
requests_post.return_value = mock.Mock(content=json.dumps({'err': 0}), status_code=200)
|
||||
resp2 = resp.form.submit(headers={'x-requested-with': 'XMLHttpRequest'})
|
||||
assert requests_post.call_args[0][0] == 'POST'
|
||||
assert requests_post.call_args[0][1] == 'http://test-post-cell/create/'
|
||||
assert requests_post.call_args[0][1] == 'http://test-post-cell/slug/create/'
|
||||
assert requests_post.call_args[1]['json'] == {'value': 'plop', 'items': ['1']}
|
||||
assert resp2.text.startswith('<form')
|
||||
|
||||
|
@ -559,10 +563,10 @@ def test_post_cell(app):
|
|||
# check variable substitution in URL
|
||||
with mock.patch('combo.utils.requests.request') as requests_post:
|
||||
settings.JSON_CELL_TYPES['test-post-cell']['actions']['create'] = {
|
||||
'url': 'http://test-post-cell/[value]/delete'}
|
||||
'url': 'http://test-post-cell/{{slug}}/{{value}}/delete'}
|
||||
resp2 = resp.form.submit()
|
||||
assert requests_post.call_args[0][0] == 'POST'
|
||||
assert requests_post.call_args[0][1] == 'http://test-post-cell/plop/delete'
|
||||
assert requests_post.call_args[0][1] == 'http://test-post-cell/slug/plop/delete'
|
||||
|
||||
# check patch method
|
||||
with mock.patch('combo.utils.requests.request') as requests_patch:
|
||||
|
@ -570,7 +574,7 @@ def test_post_cell(app):
|
|||
requests_patch.return_value = mock.Mock(content=json.dumps({'err': 0}), status_code=200)
|
||||
resp2 = resp.form.submit()
|
||||
assert requests_patch.call_args[0][0] == 'PATCH'
|
||||
assert requests_patch.call_args[0][1] == 'http://test-post-cell/update/'
|
||||
assert requests_patch.call_args[0][1] == 'http://test-post-cell/slug/update/'
|
||||
assert requests_patch.call_args[1]['json'] == {'value': 'plop', 'items': ['1']}
|
||||
assert urlparse.urlparse(resp2.location).path == '/'
|
||||
|
||||
|
@ -583,7 +587,7 @@ def test_post_cell(app):
|
|||
headers={'Content-Type': 'application/json'})
|
||||
resp2 = resp.form.submit()
|
||||
assert requests_search.call_args[0][0] == 'GET'
|
||||
assert requests_search.call_args[0][1] == 'http://test-post-cell/search/'
|
||||
assert requests_search.call_args[0][1] == 'http://test-post-cell/slug/search/'
|
||||
assert resp2.json == {'foo': 'bar'}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue