Esempio n. 1
0
 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
Esempio n. 2
0
 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
             }})
Esempio n. 3
0
 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
Esempio n. 4
0
 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
Esempio n. 5
0
 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
Esempio n. 6
0
 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
Esempio n. 7
0
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))
Esempio n. 8
0
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}))
Esempio n. 9
0
 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
Esempio n. 10
0
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))