def import_dashboard(request): """Receive an uploaded dashboard file and store in database""" if not can_modify_navlet(request.account, request): return HttpResponseForbidden() response = {} if 'file' in request.FILES: try: # Ensure file is interpreted as utf-8 regardless of locale blob = request.FILES['file'].read() data = json.loads(blob.decode("utf-8")) if not isinstance(data, dict): raise ValueError() for field, dtype in dashboard_fields.items(): if field not in data: raise ValueError() if not isinstance(data[field], dtype): raise ValueError() dashboard = AccountDashboard(account=request.account, name=data['name']) dashboard.num_columns = data['num_columns'] widgets = [] for widget in data['widgets']: if not isinstance(widget, dict): raise ValueError() for field, dtype in widget_fields.items(): if field not in widget: raise ValueError() if not isinstance(widget[field], dtype): raise ValueError() if widget['column'] > dashboard.num_columns: raise ValueError() widget = { k: v for k, v in widget.items() if k in widget_fields } widgets.append(widget) dashboard.save() for widget in widgets: dashboard.widgets.create(account=request.account, **widget) dashboard.save() response['location'] = reverse('dashboard-index-id', args=(dashboard.id, )) except ValueError: _logger.exception('Failed to parse dashboard file for import') return JsonResponse( { 'error': "File is not a valid dashboard file", }, status=400, ) else: return JsonResponse( { 'error': "You need to provide a file", }, status=400, ) return JsonResponse(response)
def add_dashboard(request): """Add a new dashboard to this user""" name = request.POST.get('dashboard-name', 'New dashboard') dashboard = AccountDashboard(account=request.account, name=name) dashboard.save() return JsonResponse({'dashboard_id': dashboard.pk})