def task_detail(request, pk): """ Get, udpate, or delete a specific task """ try: task = Task.objects.get(pk=pk) except Task.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = TaskSerializer(task) return Response(serializer.data) elif request.method == 'PUT': serializer = TaskSerializer(task, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) else: return Response(serilizer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': task.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def task_list(request): """ List all code snippets, or create a new snippet. """ if request.method == 'GET': tasks = Task.objects.all() serializer = TaskSerializer(tasks, many=True) return JSONResponse(serializer.data) elif request.method == 'POST': data = JSONParser().parse(request) serializer = TaskSerializer(data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status=201) return JSONResponse(serializer.errors, status=400)
def tasks(request): if request.method == 'GET': tasks = Task.objects.all() serializer = TaskSerializer(tasks, many=True) return JSONResponse(serializer.data, status=200) elif request.method == 'POST': data = JSONParser().parse(request) serializer = TaskSerializer(data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status=201) else: return JSONResponse(serializer.errors, status=400) return HttpResponse(status=400)
def task_list(request): """ List all tasks, or create a new task. """ if request.method == 'GET': tasks = Task.objects.all() serializer = TaskSerializer(tasks, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = TaskSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def task_detail(request, pk): """ Retrieve, update or delete a code snippet. """ try: task = Task.objects.get(pk=pk) except Task.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = TaskSerializer(task) return JSONResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = TaskSerializer(task, data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data) return JSONResponse(serializer.errors, status=400) elif request.method == 'DELETE': task.delete() return HttpResponse(status=204)