def post(self, request):
        try:
            user_id = request.user.id
        except:
            return Response({"message": "Token is not valid."},
                            status=status.HTTP_401_UNAUTHORIZED)
        serializer = CommentSerializer(data=request.data)
        if serializer.is_valid():
            comment = Comment.objects.filter(
                product_id=serializer.validated_data['product'].id,
                customer_id=serializer.validated_data['customer'].user_id)
            if comment:
                return Response({"message": "A comment of you already exists"},
                                status=status.HTTP_400_BAD_REQUEST)
            if serializer.validated_data['customer'].user_id == user_id:
                # update the rating of the product
                product = serializer.validated_data['product']
                rating = request.data['rating']
                rateCounter = len(
                    Comment.objects.filter(product_id=product.id))
                if rateCounter:
                    product.rating = (rating + product.rating *
                                      rateCounter) / (rateCounter + 1)
                else:
                    product.rating = rating
                product.save()

                serializer.save()
                return Response(serializer.data,
                                status=status.HTTP_201_CREATED)
            else:
                return Response({"message": "Token and user id didn't match"},
                                status=status.HTTP_401_UNAUTHORIZED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    def put(self, request, id):
        try:
            user_id = request.user.id
        except:
            return Response({"message": "Token is not valid."},
                            status=status.HTTP_401_UNAUTHORIZED)
        serializer = CommentSerializer(data=request.data)
        if serializer.is_valid():
            if serializer.validated_data['customer'].user_id == user_id:
                oldComment = Comment.objects.get(id=id)
                serializer = CommentSerializer(oldComment, data=request.data)
                if serializer.is_valid():
                    # update the rating of the product
                    product = serializer.validated_data['product']
                    rating = request.data['rating']
                    rateCounter = len(
                        Comment.objects.filter(product_id=product.id))
                    if rateCounter:
                        product.rating = (rating - oldComment.rating +
                                          product.rating *
                                          (rateCounter)) / rateCounter
                    else:
                        product.rating = rating
                    product.save()

                    serializer.save()
                    return Response(serializer.data)
            else:
                return Response({"message": "Token and user id didn't match"},
                                status=status.HTTP_401_UNAUTHORIZED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
 def get(self, request, pid):
     comments = Comment.objects.filter(product_id=pid)
     serializers = []
     for comment in comments:
         if comment.is_anonymous:
             comment.customer = None
             serializers.append(CommentSerializer(comment).data)
         else:
             serializer = {
                 **CommentSerializer(comment).data,
                 **UserSerializer(
                     User.objects.get(id=comment.customer.user_id)).data
             }
             serializers.append(serializer)
     return Response(serializers)
Exemple #4
0
def detail_view(request, slug):
    print(request.headers)
    product = get_object_or_404(Product, slug=slug)
    prod_serializer = ProductSerializerExpand(product)
    comments = []
    for comm in list(product.comment.all()):
        comments.append(CommentSerializer(comm).data)
    return Response({"product_info": prod_serializer.data, "comments": comments})
Exemple #5
0
    def get(self, request, id):

        comments = Comment.objects.filter(customer_id=id)
        serializers = []
        for comment in comments:
            if not comment.is_anonymous:
                serializer = CommentSerializer(comment).data
                serializers.append(serializer)
        return Response(serializers)
 def get(self, request, pid, uid):
     try:
         user_id = request.user.id
     except:
         return Response({"message": "Token is not valid."},
                         status=status.HTTP_401_UNAUTHORIZED)
     if uid == user_id:
         comment = Comment.objects.filter(product_id=pid, customer_id=uid)
         serializer = CommentSerializer(comment, many=True)
         return Response(serializer.data)
     return Response({"message": "Token and user id didn't match"},
                     status=status.HTTP_401_UNAUTHORIZED)
Exemple #7
0
    def get(self, request):
        try:
            user = request.user
        except:
            return Response({"message": "Token is not valid."}, status=status.HTTP_401_UNAUTHORIZED)

        if user.user_type != 3:
            return Response({"message": "You are not an admin."}, status=status.HTTP_401_UNAUTHORIZED)

        comments = Comment.objects.all()
        commentList = []
        for comment in comments:
            commentList.append(CommentSerializer(comment).data)
        return Response(commentList)
Exemple #8
0
 def put(self, request, id):
     try:
         user_id = request.user.id
     except:
         return Response({"message": "Token is not valid."},
                         status=status.HTTP_401_UNAUTHORIZED)
     serializer = CommentSerializer(data=request.data)
     if serializer.is_valid():
         if serializer.validated_data['customer'].user_id == user_id:
             oldComment = Comment.objects.get(id=id)
             serializer = CommentSerializer(oldComment, data=request.data)
             if serializer.is_valid():
                 product = serializer.validated_data['product']
                 serializer.save()
                 avg_rating = calculate_rating(product.id)
                 product.rating = avg_rating
                 product.save()
                 return Response(serializer.data)
         else:
             return Response({"message": "Token and user id didn't match"},
                             status=status.HTTP_401_UNAUTHORIZED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemple #9
0
 def comments(self, request, slug):
     product = self.get_object()
     comments = product.comments.all()
     serializer = CommentSerializer(comments, many=True)
     return Response(serializer.data)
Exemple #10
0
def add_comment(request):
    data = request.data
    comment = Comment(content=data['comment'], author=request.user, product=Product.objects.get(id=data['id']), rating=data['rate'])
    comment.save()
    return Response({'new_comment': CommentSerializer(comment).data})