From c47ef6fd4dd321b4c9bf1a0c1a9947bf1bf50b9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 25 Feb 2019 20:52:49 +0100 Subject: [PATCH] misc: pass configured json cell parameters to action URL templates (#30885) --- combo/data/models.py | 16 +++++++--------- tests/test_public.py | 24 ++++++++++++++---------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/combo/data/models.py b/combo/data/models.py index 1a0e5011..c59cfa4a 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -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): diff --git a/tests/test_public.py b/tests/test_public.py index ad7b7196..92efafb2 100644 --- a/tests/test_public.py +++ b/tests/test_public.py @@ -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('