def comment_list(request, format=None): """ List all comments, or create a new comment. """ if request.method == 'GET': comments = Comment.objects.all() if format is None: form = CommentForm() data = {'form':form,'comments':comments} return Response(data, template_name='comments.html') else: serializer = CommentSerializer(comments, many=True) return Response(serializer.data) elif request.method == 'POST': comment = Comment(userid=3) serializer = CommentSerializer(comment,request.DATA) logger.debug('point 2: ' + str(serializer.data)) if serializer.is_valid(): serializer.save() logger.debug('It is valid') return Response(serializer.data, status=status.HTTP_201_CREATED) else: logger.debug(serializer.errors) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create(self, request): serializer = CommentSerializer(data=request.data) if serializer.is_valid(): serializer.save(creator=request.user) return Response(serializer.data) else: return Response(serializer.errors)
def add_comment(request): data = request.data['comment'].copy() serializer = CommentSerializer(data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status=201) return JSONResponse(serializer.errors, status=404)
def put(self, request, feed_id, comment_id, format=None): comment = self.get_object(feed_id, comment_id) if comment.done: return Response({"message": "Comment locked"}, status=status.HTTP_400_BAD_REQUEST) 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)
def post(self, request): comment = CommentSerializer(data=request.DATA) if comment.is_valid(): comment.save() return Response(comment.data, status=status.HTTP_201_CREATED) return Response(comment.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, feed_id, format=None): feed = get_object_or_404(Feed, feed_id=feed_id) serializer = CommentSerializer(data=request.data) if serializer.is_valid(): if serializer.validated_data.get('parent_id'): parent_comment = Comment.objects.get( pk=serializer.validated_data.get('parent_id')) if parent_comment.done: return Response({"message": "Comment locked"}, status=status.HTTP_400_BAD_REQUEST) try: comment = serializer.save(feed=feed) except Comment.DoesNotExist: return Response({"message": "Invalid parent comment"}, status=status.HTTP_400_BAD_REQUEST) owner_email = comment.owner.email feed.add_collaborator(comment.owner) r = Response(serializer.data, status=status.HTTP_201_CREATED) set_vidfeed_user_cookie(r, owner_email) # if this is a reply send an email to the comment owner if comment.parent_comment: # find all people in the thread all_replies = Comment.objects.filter( feed=comment.feed, parent_comment=comment.parent_comment) reply_list = [comment.parent_comment.owner] for c in all_replies: reply_list.append(c.owner) # remove duplicates reply_list = list(set(reply_list)) ctx = { 'feed': feed, 'comment_author': owner_email, 'message': comment.body, } # send to everyone in the list for u in reply_list: # actually skip the person who created the comment if u != comment.owner: send_email( 'new_reply', ctx, owner_email + " replied to your comment on " + feed.get_video_title(), u.email) # else send first comment email if first comment from this user else: user_comments = Comment.objects.filter( feed=feed, owner__email=owner_email) if user_comments.count() == 1 and \ owner_email.strip().lower() != feed.owner.email.strip().lower(): ctx = { 'feed': feed, 'comment_author': owner_email, 'message': comment.body, 'too_email': feed.owner.email, } send_email( 'new_comment', ctx, owner_email + " just left their first comment on " + feed.get_video_title(), feed.owner.email) return r return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)