dashboard: raise on missing parameters in auto-tile (#45053)

This commit is contained in:
Frédéric Péters 2020-07-11 22:50:21 +02:00
parent cccbc87b61
commit 3e97633723
2 changed files with 11 additions and 3 deletions

View File

@ -21,7 +21,7 @@ from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import PermissionDenied
from django.urls import reverse
from django.db.models import Max, Min
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.http import Http404, HttpResponse, HttpResponseBadRequest, HttpResponseRedirect
from django.utils.encoding import force_text
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
@ -116,11 +116,13 @@ def dashboard_auto_tile(request, *args, **kwargs):
page_id=dashboard.page_id, placeholder='_auto_tile')
# only keep parameters that are actually defined for this cell type.
cell_form_keys = [x['varname'] for x in settings.JSON_CELL_TYPES[cell.key].get('form') or {}]
cell.parameters = {}
request_body = json.loads(force_text(request.body))
for key in cell_form_keys:
for field in settings.JSON_CELL_TYPES[cell.key].get('form') or []:
key = field['varname']
cell.parameters[key] = request_body.get(key)
if cell.parameters[key] is None and field.get('required', True):
return HttpResponseBadRequest('missing key: %s' % key)
# save cell so it can be reused later, for example to be added to
# dashboard, or to be used as reference in another page, etc.

View File

@ -209,6 +209,12 @@ def test_auto_tile(app, site):
content_type='application/json')
assert resp.text.strip() == '/var1=one/var2=/'
# with missing data
resp = app.post(reverse('combo-dashboard-auto-tile', kwargs={'key': 'test-config-json-cell'}),
params=json.dumps({'var2': 'two'}),
content_type='application/json', status=400)
def test_clean_autotiles(app, site):
appconfig = apps.get_app_config('dashboard')
appconfig.clean_autotiles()