general: allow POST to a cell to return the raw service response (#26373)

This commit is contained in:
Frédéric Péters 2018-09-12 21:41:27 +02:00
parent 23b3d15687
commit 299c3d2dcd
3 changed files with 34 additions and 2 deletions

View File

@ -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()

View File

@ -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())

View File

@ -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')