Ejemplo n.º 1
0
def comment_detail(request, pk):
    """
    Retrieve, update or delete a code comment.
    """
    comment = get_object_or_404(Comment, pk=pk)

    if request.method == 'GET':
        serializer = CommentSerializer(comment)
        return JSONResponse(serializer.data)

    elif request.method == 'PUT':
        if not request.user.is_authenticated():
            return redirect('index')
        data = JSONParser().parse(request)
        serializer = CommentSerializer(comment, data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)
        else:
            return JSONResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        if not request.user.is_authenticated():
            return redirect('index')
        comment.delete()
        return HttpResponse(status=204)
Ejemplo n.º 2
0
def comment_detail(request, pk):
    """
    Retrieve, update or delete a code comment.
    """
    comment = get_object_or_404(Comment, pk=pk)

    if request.method == 'GET':
        serializer = CommentSerializer(comment)
        return JSONResponse(serializer.data)

    elif request.method == 'PUT':
        if not request.user.is_authenticated():
            return redirect('index')
        data = JSONParser().parse(request)
        serializer = CommentSerializer(comment, data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)
        else:
            return JSONResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        if not request.user.is_authenticated():
            return redirect('index')
        comment.delete()
        return HttpResponse(status=204)
Ejemplo n.º 3
0
def comment_list(request):
    if request.method == 'GET':
        if request.GET == {}:
            comments = Comment.objects.all()
        else:
            comments = Comment.objects.filter(picture=request.GET['pic_id'])
        serializer = CommentSerializer(comments, many=True)
        return JSONResponse(serializer.data)
    elif request.method == 'POST':
        print(request.raw_post_data)
        data = JSONParser().parse(request)
        serializer = CommentAddSerializer(data=data)
        print(serializer.is_valid())
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
        else:
            return JSONResponse(serializer.errors, status=400)
Ejemplo n.º 4
0
def comment_list(request):
    if request.method == 'GET':
        if request.GET == {}:
            comments = Comment.objects.all()
        else:
            comments = Comment.objects.filter(picture=request.GET['pic_id'])
        serializer = CommentSerializer(comments, many=True)
        return JSONResponse(serializer.data)
    elif request.method == 'POST':
        print(request.raw_post_data)
        data = JSONParser().parse(request)
        serializer = CommentAddSerializer(data=data)
        print(serializer.is_valid())
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
        else:
            return JSONResponse(serializer.errors, status=400)