def wrapper(request, *args, **kwargs): response = func(request, *args, **kwargs) if isinstance(response, dict): resp = JsonResponse(response) if 'error' in response: resp.status_code = response['error'].get('type', 500) return resp return response
def process_exception(self, request, exception): if not isinstance(exception, AjaxException): return None if isinstance(exception, AjaxDataException): return JsonResponse(exception.data) if isinstance(exception, Ajax404): return JsonResponse( {'error': { 'type': 404, 'message': exception.message }})
def wrapper(request, *args, **kwargs): if request.method == 'POST': response = func(request, *args, **kwargs) else: response = HttpResponseNotFound() if isinstance(response, dict): resp = JsonResponse(response) if 'error' in response: resp.status_code = response['error'].get('type', 500) return resp else: return response
def wrapper(request, *args, **kwargs): if request.method == 'POST': response = func(request, *args, **kwargs) else: response = {'error': {'type': 405, 'message': 'Accepts only POST request'}} if isinstance(response, dict): resp = JsonResponse(response) if 'error' in response: resp.status_code = response['error'].get('type', 500) return resp return response
def wrapper(request, *args, **kwargs): if request.method == 'POST': response = func(request, *args, **kwargs) else: response = { 'error': { 'type': 405, 'message': 'Accepts only POST request' } } if isinstance(response, dict): resp = JsonResponse(response) if 'error' in response: resp.status_code = response['error'].get('type', 500) return resp return response
def add_todo(request): form = TodoForm(request.POST, user=request.user) if form.is_valid(): todo = form.save() response = {'id': todo.id, 'title': todo.title} else: response = form.errors_as_json() return JsonResponse(jsonify(response))
def complete_todo(request): todo = Todo.objects.get(pk=request.POST.get('id'), user=request.user) if todo.completed_on: todo.completed_on = None completed = False else: todo.completed_on = datetime.now() completed = True todo.save() return JsonResponse(jsonify({'completed': completed}))
def wrapper(request, *args, **kwargs): if request.method == 'POST' or request.method == 'GET': #if request.method == 'POST': response = func(request, *args, **kwargs) else: response = { 'error': { 'type': 403, 'message': 'Accepts only POST request' } } if isinstance(response, dict): return JsonResponse(response) else: return response
def clear_completed(request): completed_todos = Todo.completed.filter(user=request.user) response = {'ids': [c.pk for c in completed_todos]} completed_todos.delete() return JsonResponse(jsonify(response))