Beispiel #1
0
 def put(self, request, pk):
     todo = get_todo(pk)
     serializer = TodoSerializer(todo, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #2
0
 def put(self, request, pk):
     todo = get_todo(pk)
     serializer = TodoSerializer(todo, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #3
0
    def post(self, request, format=None):
            serializer = TodoSerializer(data=request.data)
            if serializer.is_valid():
                serializer.save()
                return Response(serializer.data, status=status.HTTP_201_CREATED)

            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #4
0
 def put(self, request, pk, format=None):
     todo = get_object_or_404(Todo, pk=pk)
     serializer = TodoSerializer(todo, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #5
0
def create_todo(request):
    if request.method == "GET":
        return JsonResponse({'error': "GET is not allowed"})
    elif request.method == "POST":
        serializer = TodoSerializer(data=request.POST)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, safe=False)
        else:
            return JsonResponse(serializer.errors, safe=False)
Beispiel #6
0
 def post(self, request):
     data = request.data
     data['is_done'] = False
     deserialized_data = TodoSerializer(data=data)
     if deserialized_data.is_valid():
         todo = deserialized_data.save()
         serialized_data = TodoSerializer(todo)
         return Response(serialized_data.data,
                         status=status.HTTP_201_CREATED)
     return Response({'error': 'Data tidak valid'},
                     status=status.HTTP_400_BAD_REQUEST)
Beispiel #7
0
def todo_list(request):
    if (request.method == "GET"):
        todos = Todo.objects.all()
        serializer = TodoSerializer(todos, many=True)
        return JsonResponse(serializer.data, safe=False)
    elif (request.method == "POST"):
        todoToAdd = JSONParser().parse(request)
        serializer = TodoSerializer(data=todoToAdd)
        if (serializer.is_valid()):
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)
Beispiel #8
0
def todo_list(request):
    if request.method == 'GET':
        todos = Todo.objects.all()
        serializer = TodoSerializer(todos, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = TodoSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)
Beispiel #9
0
def get_todos(request):
    if request.method == 'POST':
        serializer = TodoSerializer(data=json.loads(request.body))
        if serializer.is_valid():
            serializer.save()

        return JsonResponse({"status": "CREATED"})

    else:
        request.GET.get('name')
        result_set = Todo.objects.all()
        serializer = TodoSerializer(result_set, many=True)

        return JsonResponse(serializer.data, safe=False)
Beispiel #10
0
def list_create_todo(request):

    if request.method == "GET":
        todos = Todo.objects.all()
        serializer = TodoSerializer(todos, many=True)

        return Response(serializer.data)

    elif request.method == "POST":
        serializer = TodoSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)

        return Response(serializer.data, status.HTTP_400_BAD_REQUEST)
Beispiel #11
0
def todo_detail(request, todo_id):
    try:
        todo = Todo.objects.get(pk=todo_id)
    except Exception as e:
        return JsonResponse({"error": str(e)}, status=404)
    if (request.method == "GET"):
        serializer = TodoSerializer(todo)
        return JsonResponse(serializer.data)
    elif (request.method == "PUT"):
        curTodo = JSONParser().parse(request)
        serializer = TodoSerializer(todo, curTodo)
        if (serializer.is_valid()):
            serializer.save()
            return JsonResponse(serializer.data)
    elif (request.method == "DELETE"):
        todo.delete()
        serializer = TodoSerializer(todo)
        return JsonResponse(serializer.data)
Beispiel #12
0
def todo_detail(request, id):
    try:
        todo = Todo.objects.get(id=id)
    except Todo.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = TodoSerializer(todo)
        return JsonResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = TodoSerializer(todo, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        serializer = TodoSerializer(todo)
        todo.delete()
        return JsonResponse({'id': id})
Beispiel #13
0
 def post(self, request):
     serializer = TodoSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)