Пример #1
0
def comment_list(request):
    #Lista los comentarios o crea uno nuevo --> equivale al CommentViewSet
    if request.method == 'GET':
        comments = Comment.objects.all()
        serializer = CommentSerializer(comments, many=True)
        return JSONResponse(serializer.data)
    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = CommentSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data,
                                status=201)  #Codigo para created
        return JSONResponse(serializer.errors, status=400)  #Bad request
Пример #2
0
def comment_detail(request, pk):
    #Crear, borrar, o actualizar un comentario
    try:
        comment = Comment.objects.get(pk=pk)
    except Comment.DoesNotExist:
        return HttpResponse(status=404)  #Not found :D
    if request.method == 'GET':
        serializer = CommentSerializer(comment)
        return JSONResponse(json.dumps(serializer.data))
    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = CommentSerializer(comment, data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)
        return JSONResponse(serializer.errors, status=400)  #Bad request
    elif request.method == 'DELETE':
        comment.delete()
        return HttpResponse(status=204)  #No content
Пример #3
0
    def post(self, request, pk=None):
        print("POSTED")
        
        serializer_context = {
            'request': request,
        }

        serializer = CommentSerializer(data=request.data, context=serializer_context)
        print("P",serializer.is_valid())
        if serializer.is_valid() == True:
            print("sucessful backend")
            instance = serializer.save()
            messages.success(request._request, 'Success')
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #4
0
    def post(self, request, pk=None):
    
        serializer_context = {
            'request': request,
        }

        serializer = CommentSerializer(data=request.data, context=serializer_context)
        print("Comment serializer - validity: ",serializer.is_valid())
        if serializer.is_valid() == True:
            print("sucessful backend")

            # Compare the userID from request with userID from provided token
            userIDFromRequest = serializer.validated_data['userID']
            userIDFromToken = request.user

            if(userIDFromRequest == userIDFromToken):
                instance = serializer.save()
                messages.success(request._request, 'Success')
                return Response(serializer.data, status=status.HTTP_201_CREATED)
            else:
                raise ValidationError("Sorry, you are trying to comment as another user which is forbidden..")
        
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)