Beispiel #1
0
 def delete(self, request, format=None):
     serializer = CommentSerializer(data=request.data)
     if serializer.is_valid():
         obj = Comment.objects.filter(id=serializer.data['id'])
         obj.delete()
         return Response(stastus=status.HTTP_204_NO_CONTENT)
     return Response(status=status.HTTP_400_BAD_REQUEST)
Beispiel #2
0
 def put(self, request, pk, id, format=None):
     comment = self.get_object(id)
     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)
Beispiel #3
0
 def post(self, request, format=None):
     data = JSONParser().parse(request)
     data["user"] = request.user.id
     serializer = CommentSerializer(data=data)
     if serializer.is_valid():
         serializer.save()
         return JsonResponse(serializer.data, status=201)
     return JsonResponse(serializer.errors, status=400)
Beispiel #4
0
 def post(self, request, format=None):
     user = request.user
     serializer = CommentSerializer(data=request.data,
                                    context={'user': user})
     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)
Beispiel #5
0
 def comment_add(self, request, **kwargs):
     serializer = CommentSerializer(context={"host": request.get_host()},
                                    data=request.data)
     if not serializer.is_valid():
         return Response(data=serializer.errors,
                         status=status.HTTP_406_NOT_ACCEPTABLE)
     post = get_object_or_404(Post, id=kwargs.get('post_id'))
     self.check_object_permissions(request, post.group)
     serializer.save(owner=request.user, post=post)
     return JsonResponse(data=serializer.data, status=201, safe=False)
Beispiel #6
0
 def comment_edit(self, request, **kwargs):
     serializer = CommentSerializer(context={"host": request.get_host()},
                                    data=request.data)
     if not serializer.is_valid():
         return Response(data=serializer.errors,
                         status=status.HTTP_406_NOT_ACCEPTABLE)
     post = get_object_or_404(Post, id=kwargs.get('post_id'))
     comment = get_object_or_404(Comment,
                                 id=kwargs.get('comment_id'),
                                 post=post)
     self.check_object_permissions(request, comment)
     serializer.update(comment, serializer.validated_data)
     return JsonResponse(data=serializer.data, status=200, safe=False)
Beispiel #7
0
 def create_comment(self, request, **kwargs):
     try:
         post = Post.objects.get(**kwargs)
     except Post.DoesNotExist:
         return response404('Post')
     self.check_object_permissions(request=request, obj=post.group)
     serializer = CommentSerializer(data=request.data, partial=True)
     if not serializer.is_valid():
         return response406({
             **serializer.errors, 'message': 'Błąd walidacji'
         })
     serializer.save(post=post, owner=request.user)
     return response200({
         **serializer.data, 'message':
         'Pomyślnie utworzono komentarz'
     })
Beispiel #8
0
    def get(self, request, pk, format=None):
        obj = CommentsModel.objects.filter(post=pk)

        if obj:
            serializer = CommentSerializer(obj, many=True)
            return Response({'comments': serializer.data})
        return Response({'comments': ''})
 def post(self, request):
     post_id = request.data.get("post_id")
     if post_id is None:
         return Response(
             {
                 "success": False,
                 "message": "Required Param post_id is missing"
             },
             status=400)
     try:
         post = Post.objects.get(pk=post_id)
     except Post.DoesNotExist:
         return Response(
             {
                 "success": False,
                 "message": "Invalid post_id param provided"
             },
             status=400)
     data = {
         "commenter": request.user.id,
         "post": post.id,
         "comment": request.data.get("comment")
     }
     comment = CommentSerializer(data=data)
     if comment.is_valid():
         instance = comment.save()
         serializer = CommentSerializer(instance, many=False)
         msg = "{} added a new comment".format(request.user.username)
         if int(post.user.id) != int(request.user.id):
             NotificationFunctions.create_notification(
                 post=post.id,
                 user=post.user.id,
                 auther=request.user.id,
                 message=msg,
                 notification_type=Notification.POST_COMMENT)
         return Response({
             "success": True,
             "message": "Comment Saved.",
             "data": serializer.data
         })
     return Response({
         "success": False,
         "message": comment.errors
     },
                     status=400)
Beispiel #10
0
 def update_comment(self, request, **kwargs):
     try:
         comment = Comment.objects.get(**kwargs)
     except Comment.DoesNotExist:
         return response404('Comment')
     serializer = CommentSerializer(comment,
                                    data=request.data,
                                    partial=True)
     self.check_object_permissions(request=request, obj=comment)
     if not serializer.is_valid():
         return response406({
             **serializer.errors, 'message': 'Błąd walidacji'
         })
     serializer.save()
     return response200({
         **serializer.data, 'message':
         'Pomyślnie zaktualizowano komentarz'
     })
Beispiel #11
0
 def get(self, request, id, format=None):
     try:
         ids = [int(i) for i in id.split("/")]
         comments = Comment.objects.filter(post=Post.objects.get(
             auto_id=ids[0]))
         if len(ids) == 2:
             comments = comments.filter(parent_comment=id[2])
         serializer = CommentSerializer(comments, many=True)
         return JsonResponse(serializer.data, safe=False)
     except:
         return JsonResponse(["err"], safe=False)
 def handle_posting_comment(self, content):
     post = Post.objects.get(pk=content['postID'])
     comment = Comment.objects.create(
         author=self.user, content=content['content'], post=post)
     data = CommentSerializer(comment).data
     async_to_sync(self.channel_layer.group_send)(
         self.room_group_name,
         {
             'type': 'send_event_to_client',
             'event_type': 'POSTED_COMMENT',
             'content': data,
         })
Beispiel #13
0
 def get_comments(self, request, **kwargs):
     paginator = PageNumberPagination()
     try:
         post = Post.objects.get(id=kwargs.get('id'))
     except Post.DoesNotExist:
         return paginator.get_paginated_response(data=[])
     comments = Comment.objects.filter(
         post=post).order_by('date_commented').reverse()
     self.check_object_permissions(request=request, obj=post.group)
     serializer = CommentSerializer(comments, many=True)
     paginator.page_size = 10
     data = paginator.paginate_queryset(serializer.data, request)
     return paginator.get_paginated_response(data=data)
Beispiel #14
0
 def upvote(self, request, pk=None):
     serializer = CommentUpvoteSerializer(data={'comment': pk},
                                          context={'request': request})
     if serializer.is_valid():
         serializer.save()
         comment = Comment.objects.get(pk=pk)
         return Response(
             CommentSerializer(comment, context={
                 'request': request
             }).data)
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Beispiel #15
0
def comments(request, id):
    if request.method == 'GET':
        commentss = Comment.objects.filter(post__id=id).order_by('-pub_date')
        serializer = CommentSerializer(commentss, many=True)
        return Response(serializer.data)
    elif request.method == 'POST':
        request.data['post'] = id
        serializer = CommentSerializer(data=request.data)
        print(serializer)
        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)
 def handle_editing_comment(self, content):
     comment = Comment.objects.get(pk=content['commentID'])
     if self.user != comment.author:
         return
     comment.content = content['content']
     comment.save()
     data = CommentSerializer(comment).data
     async_to_sync(self.channel_layer.group_send)(
         self.room_group_name,
         {
             'type': 'send_event_to_client',
             'event_type': 'EDITED_COMMENT',
             'content': data,
         }
     )
Beispiel #17
0
 def post_detail(self, request, **kwargs):
     post = get_object_or_404(Post, id=kwargs.get('post_id'))
     self.check_object_permissions(request, post.group)
     response_data = {
         'post':
         PostSerializer(post, context={
             "host": request.get_host()
         }).data,
         'comments':
         CommentSerializer(Comment.objects.filter(post=post),
                           context={
                               "host": request.get_host()
                           },
                           many=True).data
     }
     return JsonResponse(data=response_data, status=200, safe=False)
Beispiel #18
0
 def cancel_upvote(self, request, pk=None):
     try:
         comment = Comment.objects.get(pk=pk)
         instance = CommentUpvote.objects.get(comment=comment,
                                              voter=self.request.user)
         self.perform_destroy(instance)
         return Response(
             CommentSerializer(comment, context={
                 'request': request
             }).data)
     except ObjectDoesNotExist:
         return Response(
             {
                 'status': 'Not Fount',
                 'message': 'This upvote is not exist.'
             },
             status=status.HTTP_404_NOT_FOUND)
Beispiel #19
0
 def comments(self, request, pk):
     post = self.get_object()
     if request.method == "GET":
         comment_qs = Comment.objects.filter(post=post)
         serializer = CommentSerializer(comment_qs, many=True)
         return Response(serializer.data)
     else:
         comment = request.data
         comment["user"] = request.user.id
         comment["post"] = post.id
         serializer = CommentSerializer(data=comment)
         if serializer.is_valid():
             serializer.save()
             return Response(serializer.data)
         else:
             return Response(serializer.errors,
                             status=status.HTTP_400_BAD_REQUEST)
 def post(self, request):
     post_id = request.data.get("post_id")
     page = request.data.get("page")
     if page is None:
         page = 1
     size = 10
     if post_id is None:
         return Response(
             {
                 "success": False,
                 "message": "Required Param post_id is missing"
             },
             status=400)
     try:
         post = Post.objects.get(pk=post_id)
     except Post.DoesNotExist:
         return Response(
             {
                 "success": False,
                 "message": "Invalid post_id param provided"
             },
             status=400)
     post_comments = PostComment.objects.filter(
         post=post.id).order_by('created_at')
     try:
         paginated_data = Paginator(post_comments, size)
     except (EmptyPage, InvalidPage):
         return Response({
             "success": False,
             "message": "Empty Page"
         },
                         status=400)
     post_comment_serializer = CommentSerializer(paginated_data.page(page),
                                                 many=True)
     return Response({
         "success": True,
         "data": post_comment_serializer.data,
         "total": paginated_data.count,
         "pages": paginated_data.num_pages,
         "current_page": int(page)
     })
Beispiel #21
0
    def post(self, request, pk, format=None):
        post = self.get_object(pk)
        commentform = CommentForm(request.data)

        if commentform.is_valid():
            comment = commentform.save(commit=False)
            comment.author = request.user
            comment.save()

            post.comments.add(comment)
            post.comment_count = post.comments.count()
            post.save()

            try:
                image_ids = request.data['images']
                images = PostFiles.objects.filter(id__in=image_ids)
                for image in images:
                    comment.images.add(image)
            except:
                pass

            if post.author.notify_comment_post and not request.user == post.author:
                notification = Notification(user=post.author,
                                            created_by=request.user,
                                            notification_type='UPC',
                                            object_id=post.id,
                                            object_type='Post',
                                            object_name=post.title,
                                            message=request.user.first_name +
                                            " commented on your post " +
                                            post.title)
                notification.save()

                user = post.author
                user.notifications_count += 1
                user.save()

            serializer = CommentSerializer(comment)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #22
0
def comment_list(request, post):
    if request.method == 'GET':
        comments = Comment.objects.filter(post=post)
        
        comments_serializer = CommentSerializer(comments, many=True)
        return JsonResponse(comments_serializer.data, safe=False)
        # 'safe=False' for objects serialization
 
    elif request.method == 'POST':
        comment_data = JSONParser().parse(request)
        comment_data['post']=post 
        comment_serializer = CommentSerializer(data=comment_data)
        if comment_serializer.is_valid():
            comment_serializer.save()
            return JsonResponse(comment_serializer.data, status=status.HTTP_201_CREATED) 
        return JsonResponse(comment_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #23
0
 def comment(self, request, comment_pk, pk):
     try:
         comment = Comment.objects.get(id=comment_pk)
         self.check_object_permissions(self.request, comment)
     except ObjectDoesNotExist:
         raise NotFound(detail="Comment does not exist")
     if request.method == "GET":
         serializer = CommentSerializer(comment)
         return Response(serializer.data)
     if request.method == "DELETE":
         comment.delete()
         return Response(status=status.HTTP_204_NO_CONTENT)
     if request.method == "PATCH":
         serializer = CommentSerializer(comment,
                                        data=request.data,
                                        partial=True)
         if serializer.is_valid():
             serializer.save()
             return Response(serializer.data)
         else:
             return Response(serializer.errors,
                             status=status.HTTP_400_BAD_REQUEST)
Beispiel #24
0
 def get(self, request, format=None):
     comments = Comment.objects.all()
     serializer = CommentSerializer(comments, many=True)
     return Response(serializer.data)
 def get(self, request, post_id):
     post = Post.objects.get(pk=post_id)
     serializer = CommentSerializer(post.get_comments(), many=True)
     return Response(serializer.data, status=status.HTTP_200_OK)
Beispiel #26
0
    def get(self, request):
        comments = Comment.objects.all()
        serializer = CommentSerializer(comments, many=True)

        return create_response(data=serializer.data, status=status.HTTP_200_OK)
Beispiel #27
0
 def get(self, request, pk, format=None):
     post = Post.objects.get(id=pk)
     comments = post.comments.all().order_by('id')
     serializer = CommentSerializer(comments, many=True)
     return Response(serializer.data)
Beispiel #28
0
    def get(self, request, pk, id, format=None):
        comment = self.get_object(id)
        serializer = CommentSerializer(comment)

        return Response(serializer.data)
Beispiel #29
0
 def get(self, request, format=None):
     comment = Comment.objects.all().order_by('-comment_id')
     serializer = CommentSerializer(comment, many=True)
     return Response(serializer.data)
Beispiel #30
0
def getComment(request, post_id=None, format=None):
    comments = Comment.objects.filter(post_id=post_id).order_by('comment_id')
    serializer = CommentSerializer(comments, many=True)
    return JsonResponse(serializer.data)