def put(self, request, pk):
     comment = self.get_object(pk)
     serializer = CommentSerializer(comment, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 2
0
 def post(self,request,format=None):
     serializer = CommentSerializer(data=request.data,context= {'user_id':request.user.id})
    
     if serializer.is_valid():
          serializer_2 = CommentSerializer(serializer.save())
          r.hset("COMMENTS",serializer_2.data["id"],json.dumps(serializer_2.data))
          return Response(json.dumps(serializer.data), status=status.HTTP_201_CREATED) 
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)    
 def post(self, request):
     serializer = CommentSerializer(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)
Ejemplo n.º 4
0
def comment_post_api(request, pk):
    try:
        post = get_object_or_404(NewsPost, id=pk)
    except NewsPost.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)
    if request.method == 'GET':
        comments = Comment.objects.filter(post=post)
        serializer = CommentSerializer(comments, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)
    if request.method == 'POST':
        serializer = CommentSerializer(data=request.data,
                                       context={'request': request})
        if serializer.is_valid():
            serializer.save(post_id=pk, user=request.user)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 5
0
def comment_post_form(request):
    """
    With POST method comment form submit. In this view the recieved data updated and pass to serializer
    Inputs: data from POST => post,text,user.id that get from request.user
    Outputs: Serializer.data [not impostant becuase in the js if request is success alert with notificatin
    """
    if request.method == 'GET':
        pass

    elif request.method == 'POST':
        user = User.objects.get(username=request.user)
        print(request.data)
        param = {
            'user': user.pk,
            'post': int(request.data['post']),
            'text': request.data['text'],
        }

        serializer = CommentSerializer(data=param)

        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)