From 299c3d2dcd42acecbb3141dfe632f152f06b3565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Wed, 12 Sep 2018 21:41:27 +0200 Subject: [PATCH] general: allow POST to a cell to return the raw service response (#26373) --- combo/data/models.py | 8 ++++++++ combo/public/views.py | 8 +++++++- tests/test_public.py | 20 +++++++++++++++++++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/combo/data/models.py b/combo/data/models.py index 9fba00a4..964b0c15 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -1194,6 +1194,14 @@ class JsonCellBase(CellBase): if self.cache_duration: self.get_cell_extra_context(context, invalidate_cache=True) + if self.actions[action].get('response', 'cell') == 'raw': + # response: raw in the config will get the response directly sent + # to the client. + return json_response + + # default behaviour, the cell will render itself. + return None + def render(self, context): if self.force_async and not context.get('synchronous'): raise NothingInCacheException() diff --git a/combo/public/views.py b/combo/public/views.py index 1192c4b5..06ed617a 100644 --- a/combo/public/views.py +++ b/combo/public/views.py @@ -112,16 +112,22 @@ def ajax_page_cell(request, page_pk, cell_reference): raise PermissionDenied() exception = None + action_response = None if request.method == 'POST': if not hasattr(cell, 'post'): raise PermissionDenied() try: - cell.post(request) + action_response = cell.post(request) except PostException as e: exception = e if not request.is_ajax(): messages.error(request, force_text(e) if force_text(e) != 'None' else _('Error sending data.')) + if action_response: + return HttpResponse( + action_response.content, + content_type=action_response.headers['Content-Type']) + if not request.is_ajax(): return HttpResponseRedirect(cell.page.get_online_url()) diff --git a/tests/test_public.py b/tests/test_public.py index 5983ab6d..61ca54fa 100644 --- a/tests/test_public.py +++ b/tests/test_public.py @@ -402,7 +402,12 @@ def test_post_cell(app): 'update': { 'url': 'http://test-post-cell/update/', 'method': 'PATCH', - } + }, + 'search': { + 'url': 'http://test-post-cell/search/', + 'method': 'GET', + 'response': 'raw' + }, } }}, TEMPLATES=templates_settings @@ -473,6 +478,19 @@ def test_post_cell(app): assert requests_patch.call_args[1]['json'] == {'value': 'plop', 'items': ['1']} assert urlparse.urlparse(resp2.location).path == '/' + # check raw result + with mock.patch('combo.utils.requests.request') as requests_search: + resp.form['action'] = 'search' + requests_search.return_value = mock.Mock( + content=json.dumps({'foo': 'bar'}), + status_code=200, + 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 resp2.json == {'foo': 'bar'} + + def test_familyinfos_cell_with_placeholders(app, admin_user): Page.objects.all().delete() page = Page(title='Family', slug='index', template_name='standard') -- 2.19.0