def get(self, request):
        token = request.headers.get('Authorization', None)
        if token is None or token == "":
            return Response({"message": "Authorization credentials missing"},
                            status=status.HTTP_403_FORBIDDEN)

        user = get_user(token)
        if user is None:
            return Response(
                {"message": "You need to login to perform this action !"},
                status=status.HTTP_403_FORBIDDEN)

        comments = Comment.objects.filter(
            user_id=user.id).order_by('-date_time')
        serializer = CommentSerializer(comments, many=True)
        serializer = serializer.data
        if len(serializer) == 0:
            return Response({"message": "No Comments found"},
                            status=status.HTTP_204_NO_CONTENT)

        for comment in serializer:
            comment['idea_title'] = Idea.objects.get(
                id=comment['idea_id']).project_title

        return Response({
            "message": "Comments Found",
            "comments": serializer
        },
                        status=status.HTTP_200_OK)
Ejemplo n.º 2
0
 def put(self, request, comment_id):
     comment = self.get_object(comment_id)
     serializer = CommentSerializer(instance=comment, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response({'error': serializer.errors})
Ejemplo n.º 3
0
 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({'error': serializer.errors},
                     status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Ejemplo n.º 4
0
def post_comment(request):
    data = request.data
    client = get_object_or_404(Client, user=request.user)
    data['client'] = client.id

    # check permissions to create a comment on this post
    post = get_object_or_404(Post, id=data['post'])
    blog = Blog.objects.get(id=post.blog.id)

    if not blog.isPublic and not client in blog.subs.all():
        return Response({'error': 'not enough permissions'},
                        status=HTTP_401_UNAUTHORIZED)
    else:
        com_serializer = CommentSerializer(data=data)

        if com_serializer.is_valid():
            com_serializer.save()
            data = {
                'success': 'successfully added a comment to post',
                'comment': com_serializer.data
            }
        else:
            data = com_serializer.errors

    return Response(data)
Ejemplo n.º 5
0
def CommentList(request, postId):
    if request.method == 'GET':
        comments = Comment.objects.select_related().filter(
            post__id=postId).extra(select={
                'username': '******',
                'postId': 'app_post.id'
            }).order_by('-timestamp')
        serializer = CommentSerializer(comments, many=True)
        return Response(serializer.data)
Ejemplo n.º 6
0
def comments_by_product(request, product_id, category_id):
    try:
        categories = Category.objects.get(id=category_id)
        try:
            products = Product.objects.get(id=product_id)
            serializer = ProductSerializer(products, many=True)
        except Category.DoesNotExist as e:
            return Response("error", str(e))
    except Product.DoesNotExist as e:
        return Response("error:", str(e))

    if request.method == 'GET':
        comments = products.comment_set.all()
        serializer = CommentSerializer(comments, many=True)
        return Response(serializer.data)
Ejemplo n.º 7
0
 def product(self,request,product_id):
     user=request.user
     context=getContext(request)
     brands=BrandRepo(user=user).list()
     context['brands']=brands        
     # if request.user.is_authenticated:
     #     context['new_definition_form']=NewDefinitionForm()
     shops=ShopRepo(user=request.user).list(product_id=product_id)
     context['shops']=shops
     context['shops_s']='[]'
     active_customer=CustomerRepo(user=user).me
     active_supplier=SupplierRepo(user=user).me
     if active_customer is not None:            
         add_to_cart_form=AddToCartForm()
         if len(shops)>0:
             context['add_to_cart_form']=add_to_cart_form
         shop_s=ShopSerializer(shops,many=True).data
         context['shops_s']=json.dumps(shop_s)
     if active_supplier is not None:
         context['add_shop_form']=AddShopForm()  
         # context['add_shop_form']=True       
         shop_s=ShopSerializer(shops,many=True).data
         context['shops_s']=json.dumps(shop_s)
     unit_names=ProductUnitRepo(user=user).list_for_product(product_id)
     unit_names_s=ProductUnitSerializer(unit_names,many=True).data
     context['unit_names_s']=json.dumps(unit_names_s)
     product=ProductRepo(user=request.user).get(product_id)
     context['product']=product
     context['metadatas']=product.metadatas.all()
     product_relateds=ProductRepo(user=user).related(product_id=product_id)
     context['product_relateds']=product_relateds
     context['product_s']=json.dumps(ProductSerializer(product).data)
     product_repo=ProductRepo(user=user)
     product_comments=product_repo.comments(product_id=product_id)
     product_comments_s=ProductCommentSerializer(product_comments,many=True).data
     context['product_comments_s']=json.dumps(product_comments_s)
     context['comments_s']=json.dumps(CommentSerializer(product.comments.all(),many=True).data)
     context['product_comments']=product_comments
     if user is not None and user.is_authenticated:
         context['add_product_comment_form']=AddProductCommentForm()
         context['add_product_like_form']=AddProductLikeForm()
         context['is_liked']=product_repo.is_my_favorite(product_id=product_id)
     context['breadcrumb']=product.category.get_breadcrumb()
     #return render(request,TEMPLATE_ROOT+'product.html',context=context)
     return render(request=request,template_name='one-tech/product.html',context=context)
Ejemplo n.º 8
0
 def blog(self, request, blog_id, *args, **kwargs):
     context = getContext(request=request)
     page = BlogRepo(user=request.user).blog(blog_id=blog_id)
     context['page'] = page
     page_id = blog_id
     if page is None:
         raise Http404
     tags = TagRepo(user=request.user).list_top()
     context['tags'] = tags
     context['add_like_form'] = AddLikeForm()
     context['get_edit_url'] = page.get_edit_url()
     context['add_comment_form'] = AddCommentForm()
     context['delete_comment_form'] = DeleteCommentForm()
     comments_s = json.dumps(
         CommentSerializer(page.comments, many=True).data)
     context['comments_s'] = comments_s
     context['my_like'] = LikeRepo(
         user=request.user, object_type='Page').my_like(object_id=page_id)
     return render(request, TEMPLATE_ROOT + 'page.html', context)
Ejemplo n.º 9
0
    def add_comment(self, request):
        user = request.user
        if request.method == 'POST':
            add_comment_form = AddCommentForm(request.POST)
            if add_comment_form.is_valid():
                object_id = add_comment_form.cleaned_data['object_id']
                text = add_comment_form.cleaned_data['text']
                object_type = add_comment_form.cleaned_data['object_type']
                comment_repo = CommentRepo(user=request.user,
                                           object_type=object_type)
                my_comment = comment_repo.add(object_id=object_id, text=text)
                comments_count = comment_repo.count(object_id=object_id)
                my_comment_s = CommentSerializer(my_comment).data
                return JsonResponse({
                    'my_comment': my_comment_s,
                    'comments_count': comments_count
                })

                return JsonResponse({'result': '2'})

            return JsonResponse({'result': '3'})
        return JsonResponse({'result': '4'})
Ejemplo n.º 10
0
 def get(self, request, comment_id):
     comment = self.get_object(comment_id)
     serializer = CommentSerializer(comment)
     return Response(serializer.data)
Ejemplo n.º 11
0
 def get(self, request):
     comments = Comment.objects.all()
     serializer = CommentSerializer(comments, many=True)
     return Response(serializer.data)
    def post(self, request):
        keys = {"PENDING": 0, "PUBLISHED": 1, "REJECTED": 2}
        token = request.headers.get('Authorization', None)
        if token is None or token == "":
            return Response({"message": "Authorization credentials missing"},
                            status=status.HTTP_403_FORBIDDEN)

        user = get_user(token)
        if user is None:
            return Response(
                {"message": "You need to login to perform this action !"},
                status=status.HTTP_403_FORBIDDEN)

        request.data['user_id'] = user.id

        # Checking if an idea with idea_id exists
        try:
            idea = Idea.objects.get(id=(request.data)['idea_id'],
                                    is_reviewed=keys["PUBLISHED"])
        except:
            return Response({"message": "Invalid Idea Id"},
                            status=status.HTTP_400_BAD_REQUEST)

        # Validating and saving comment for that idea
        comment = CommentSerializer(data=request.data)
        if comment.is_valid():
            comment.save()

            response = comment.data
            registration_ids = []

            # Getting Registration ids
            response['username'] = user.username

            try:
                ideaUserFCMDevice = UserFCMDevice.objects.get(
                    user_id=idea.user_id.id)
                if not (user.id == ideaUserFCMDevice.user_id.id):
                    registration_ids.append(ideaUserFCMDevice.registration_id)
            except UserFCMDevice.DoesNotExist:
                pass

            if response['parent_comment_id'] == None:
                # Main Thread Comment
                response['child_comments'] = []
            else:
                # Child Comment
                response['child_comments'] = None
                try:
                    parentComment = Comment.objects.get(
                        id=response['parent_comment_id'])
                    parentCommentUserFCMDevice = UserFCMDevice.objects.get(
                        user_id=parentComment.user_id.id)
                    if not (parentCommentUserFCMDevice.registration_id
                            in registration_ids):
                        if not (parentCommentUserFCMDevice.user_id.id
                                == user.id):
                            registration_ids.append(
                                parentCommentUserFCMDevice.registration_id)
                except UserFCMDevice.DoesNotExist:
                    pass

            # Sending Notifications
            message_title = user.username + " Commented"
            message_body = response['body']
            data = {
                "url": "https://ideas.dscvit.com/ideas/" + str(idea.id) + "/"
            }

            result = send_notifs(registration_ids, message_title, message_body,
                                 data)

            if result:
                print("Failed to send notification")

            return Response({"message": response}, status=status.HTTP_200_OK)
        else:
            return Response({"message": comment.errors},
                            status=status.HTTP_400_BAD_REQUEST)
    def get(self, request, pk):

        offset = request.query_params.get('offset', None)
        if offset != None and offset != "":
            offset = int(offset)
            start = offset * 5
            end = (offset + 1) * 5

        total_pages = 0

        # Getting all comments
        comments = list(Comment.objects.filter(idea_id=pk))
        response = []
        if len(comments) == 0:
            return Response(
                {
                    "message": "There are no comments",
                    'total_pages': total_pages
                },
                status=status.HTTP_204_NO_CONTENT)
        else:
            # Adding parent comment for each thread
            comments = list(
                Comment.objects.filter(parent_comment_id=None, idea_id=pk))
            serializer = CommentSerializer(comments, many=True)

            total_pages = ceil(len(serializer.data) / 5)

            if offset == None or offset == "":
                response = serializer.data
            else:
                response = serializer.data[start:end]

            if len(response) == 0:
                return Response(
                    {
                        "message": "There are no comments",
                        'total_pages': total_pages
                    },
                    status=status.HTTP_204_NO_CONTENT)

            # Adding child comment for each thread
            for resp in response:
                user = User.objects.get(id=resp['user_id'])
                resp['username'] = user.username
                resp['child_comments'] = None
                child_comments = list(
                    Comment.objects.order_by('date_time').filter(
                        parent_comment_id=resp['id'], idea_id=pk))
                child_comment_serializer = CommentSerializer(child_comments,
                                                             many=True)
                resp['child_comments'] = child_comment_serializer.data
                for comm in resp['child_comments']:
                    childUser = User.objects.get(id=comm['user_id'])
                    comm['username'] = childUser.username

            return Response({
                "message": response,
                'total_pages': total_pages
            },
                            status=status.HTTP_200_OK)
Ejemplo n.º 14
0
 def post(self, request, format=None):
     serializer = CommentSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save(owner=request.user)
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(status=status.HTTP_400_BAD_REQUEST)