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 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 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 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 comment_with_id(id): pretty = True if 'X-Requested-With' in request.headers: pretty = False print pretty comment = Comment.query.filter_by(id=id).first_or_404() if request.method == 'GET': pv = PageView(page='Comment') db.session.add(pv) db.session.commit() return Response( CommentSerializer().serialize(comment, pretty=pretty), mimetype='application/json' if not pretty else 'text/html') elif request.method == 'PUT': comment.save(**(request.json)) pv = PageView(page='Comment') db.session.add(pv) db.session.commit() return Response( CommentInputSerializer().serialize(comment, pretty=pretty), mimetype='application/json' if not pretty else 'text/html') elif request.method == 'DELETE': db.session.delete(comment) pv = PageView(page='Comment') db.session.add(pv) db.session.commit() return '', 204
def comment(): pretty = True if 'X-Requested-With' in request.headers: pretty = False print pretty if request.method == 'GET': pv = PageView(page='Comment') db.session.add(pv) db.session.commit() return Response( CommentSerializer().serialize(Comment.query.all(), many=True, pretty=pretty), mimetype='application/json' if not pretty else 'text/html') elif request.method == 'POST': comment = Comment(**(request.json)) db.session.add(comment) pv = PageView(page='Comment') db.session.add(pv) db.session.commit() return Response( CommentInputSerializer().serialize(comment, pretty=pretty), status=201, mimetype='application/json' if not pretty else 'text/html')
def set_comment_done(request, feed_id, comment_id): comment_done = CommentDoneSerializer(data=request.data) if comment_done.is_valid(): comment = get_object_or_404(Comment, feed__feed_id=feed_id, pk=comment_id) comment.done = comment_done.data.get('done') comment.save() return Response(CommentSerializer(comment).data, status=status.HTTP_200_OK) return Response(comment_done._errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): post_id = request.params.get('post_id','') comment_id = request.params.get('comment_id','') body = request.params.get('body','') comment = Comment.objects.get(id=int(comment_id)) comment.body = body comment.save() comments = Comment.objects.filter(post_id=int(post_id)) serializer = CommentSerializer(comments,many=True) return {"comments":serializer.data}
def post(self, request): post_id = request.data['post_id'] comment_id = request.data['comment_id'] body = request.data['body'] comment = Comment.objects.get(id=int(comment_id)) comment.body = body comment.save() comments = Comment.objects.filter(post_id=int(post_id)) serializer = CommentSerializer(comments,many=True) return {"comments":serializer.data}
def action_comment_with_id(id): pretty = True if 'X-Requested-With' in request.headers: pretty = False print pretty if request.method == 'GET': pv = PageView(page='Action_Comment') db.session.add(pv) db.session.commit() comments = Comment.query.filter_by(action_id=id).all() return Response(CommentSerializer().serialize(comments, many=True), mimetype='application/json')
def post(self, request): comment_id = request.data['comment_id'] post_id = request.data['post_id'] try: comment = Comment.objects.get(id=int(comment_id)) comment.delete() comments = Comment.objects.filter(post_id=int(post_id)) serializer = CommentSerializer(comments,many=True) return {"comments":serializer.data} except Exception, R: return {'message':'error','error':str(R)}
def add_comment(request, data_type, id): token = getToken(request.META) user = getUser(token) serialized = CommentSerializer(data=request.DATA) if(serialized.is_valid): comment = Comment() comment.content = serialized.initial_data['content'] if(user): comment.user = user if(data_type == 'stories'): comment.story = Story.objects.get(pk=id) elif(data_type == 'pictures'): comment.picture = Picture.objects.get(pk=id) comment.save() serialized = CommentSerializer(comment) return Response(serialized.data, status=status.HTTP_201_CREATED) else: return Response(serialized._errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): user = request.user comment_id = request.params.get('comment_id', '') post_id = request.params.get('post_id', '') try: comment = Comment.objects.get(id=int(comment_id)) comment.delete() comments = Comment.objects.filter(post_id=int(post_id)) serializer = CommentSerializer(comments, many=True) return {"comments": serializer.data} except Exception as R: return {'message': 'error', 'error': str(R)}
def post(self, request): user = request.user post_id = request.data['post_id'] try: post = Post.objects.get(id=int(post_id)) comments = Comment.objects.filter(post=post) serializer = CommentSerializer(comments,many=True) return {"comments":serializer.data} except Exception,R: logger.error('Internal Server Error: %s', request.path, exc_info=sys.exc_info(), extra={ 'status_code': 500, 'request': request } ) return {'message':'error','error':str(R)}
def get(self, request): user = request.user post_id = request.params.get('post_id', '') body = request.params.get('body', '') try: try: post = Post.objects.get(id=int(post_id)) post.total_comments = post.total_comments + 1 post.save() except Exception, R: return {'message': 'error', 'error': str(R)} anonymous_avatar = 'http://divorcesus.com/static/images/user_no_avatar.png' if not user.is_authenticated(): comment = Comment.objects.create(title='', body=body, username='******', is_anonymous=True, post=post, avatar=anonymous_avatar, is_flagged=False) else: profile = Profile.objects.get(id=user.id) comment = Comment.objects.create( title='', body=body, author=user, username=user.username, is_anonymous=False, post=post, avatar=profile.profile_image_path, is_flagged=False) comments = Comment.objects.filter(post_id=int(post_id)) serializer = CommentSerializer(comments, many=True) return {"comments": serializer.data}
def post(self, request, direction, comment_id): if comment_id: try: comment = Comment.objects.get(id=comment_id) except Comment.DoesNotExist: return Response({'error': 'comment does not exist.'}, status=status.HTTP_400_BAD_REQUEST) if direction == 'up': comment.score += 1 elif direction == 'down': comment.score -= 1 else: return Response({'error': 'invalid direction'}, status=status.HTTP_400_BAD_REQUEST) comment.save() serializer = CommentSerializer(comment) return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response({'error': 'please provide a comment id.'}, status=status.HTTP_400_BAD_REQUEST)
def discussion(request, pk): """ View an individual discussion. """ if 'login_prefix' in request.session: del request.session['login_prefix'] topic = get_object_or_404(Topic, pk=pk) r = JSONRenderer() comments = r.render([CommentSerializer(c).data for c in topic.comments.all()]) users = r.render([TopicUserSerializer(u).data for u in topic.users.all()]) groups = r.render([GroupSerializer(g).data for g in topic.groups.all()]) topic_user = None if request.user.is_authenticated(): topic_user = request.user.topic_user(topic) return render(request, "discussion.html", {"topic": topic, "comments": comments, "users": users, "groups": groups, "total_users": User.objects.all().count(), "topic_user": topic_user, "active_users": Profile.objects.filter(active=True).count()} )
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 get_comments_by_place(request): place_id = request.data['place_id'] comments = Comment.objects.filter(place_id=place_id) serializer = CommentSerializer(comments, many=True) return JSONResponse(serializer.data, status=201)
def get(self, request, feed_id, format=None): serializer = CommentSerializer(self.get_objects(feed_id), many=True) return Response(serializer.data)
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)