general: extend json cell actions to support submitting arrays (#22128)

This commit is contained in:
Frédéric Péters 2018-02-27 10:07:52 +01:00
parent 1e33fe9910
commit 77c053945c
3 changed files with 10 additions and 3 deletions

View File

@ -1003,7 +1003,10 @@ class JsonCellBase(CellBase):
for key, value in request.POST.items():
if key == 'action':
continue
content[key] = value
if key.endswith('[]'): # array
content[key[:-2]] = request.POST.getlist(key)
else:
content[key] = value
context = copy.copy(content)
context['request'] = request

View File

@ -1,5 +1,8 @@
<form method="post" action="{% url 'combo-public-ajax-page-cell' page_pk=cell.page.id cell_reference=cell.get_reference %}">
<input type="hidden" name="action" value="create"/>
<input name="value">
<input name="items[]" value="1" type="checkbox"/>
<input name="items[]" value="2" type="checkbox"/>
<input name="items[]" value="3" type="checkbox"/>
<button>submit</button>
</form>

View File

@ -395,12 +395,13 @@ def test_post_cell(app):
resp = app.get('/', status=200)
resp.form['value'] = 'plop'
resp.form.fields['items[]'][0].checked = True
with mock.patch('combo.utils.requests.post') as requests_post:
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] == 'http://test-post-cell/create/'
assert requests_post.call_args[1]['json'] == {'value': 'plop'}
assert requests_post.call_args[1]['json'] == {'value': 'plop', 'items': ['1']}
assert urlparse(resp2.location).path == '/'
# check ajax call
@ -408,7 +409,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] == 'http://test-post-cell/create/'
assert requests_post.call_args[1]['json'] == {'value': 'plop'}
assert requests_post.call_args[1]['json'] == {'value': 'plop', 'items': ['1']}
assert resp2.content.startswith('<form')
# check error on POST