def edit_comment_commit(comment, request): comment_form = CommentForm(request.POST) if not comment_form.validate(): # TODO return HTTPBadRequest() comment.content = comment_form.message.data # Redirect to the comment in context return HTTPSeeOther( location=request.route_url('comments.view', comment=comment, _anchor="comment-{0}".format(comment.id)))
def reply_to_discussion_commit(discussion_or_comment, request): """Add a comment""" if isinstance(discussion_or_comment, model.Discussion): discussion = discussion_or_comment comment = None elif isinstance(discussion_or_comment, model.Comment): discussion = discussion_or_comment.discussion comment = discussion_or_comment comment_form = CommentForm(request.POST) if not comment_form.validate(): # TODO return HTTPBadRequest() # Re-get the discussion with a separate query for locking purposes # XXX necessary? discussion = model.session.query(model.Discussion) \ .with_lockmode('update') \ .get(discussion.id) # XXX put this all in the Comment constructor? if comment: righter_comments = model.session.query(model.Comment) \ .with_parent(discussion) righter_comments \ .filter(model.Comment.left > comment.right) \ .update({ 'left': model.Comment.left + 2 }) righter_comments \ .filter(model.Comment.right > comment.right) \ .update({ 'right': model.Comment.right + 2 }) new_comment_left = comment.right new_comment_right = comment.right + 1 comment.right += 2 model.session.add(comment) else: max_right, = model.session.query(func.max(model.Comment.right)) \ .with_parent(discussion) \ .one() if max_right is None: # FIRST POST LOLOL no seriously default to 1 and 2. max_right = 0 new_comment_left = max_right + 1 new_comment_right = max_right + 2 new_comment = model.Comment( discussion=discussion, author=request.user, content=comment_form.message.data, left=new_comment_left, right=new_comment_right, ) discussion.comment_count += 1 model.session.add(new_comment) model.session.add(discussion) model.session.flush() # Need to get the new comment's id # Redirect to the new comment return HTTPSeeOther( location=request.route_url('comments.view', comment=new_comment, _anchor="comment-{0}".format(new_comment.id)))
def reply_to_discussion_commit(discussion_or_comment, request): """Add a comment""" if isinstance(discussion_or_comment, model.Discussion): discussion = discussion_or_comment comment = None elif isinstance(discussion_or_comment, model.Comment): discussion = discussion_or_comment.discussion comment = discussion_or_comment comment_form = CommentForm(request.POST) if not comment_form.validate(): # TODO return HTTPBadRequest() # Re-get the discussion with a separate query for locking purposes # XXX necessary? discussion = model.session.query(model.Discussion) \ .with_lockmode('update') \ .get(discussion.id) # XXX put this all in the Comment constructor? if comment: righter_comments = model.session.query(model.Comment) \ .with_parent(discussion) righter_comments \ .filter(model.Comment.left > comment.right) \ .update({ 'left': model.Comment.left + 2 }) righter_comments \ .filter(model.Comment.right > comment.right) \ .update({ 'right': model.Comment.right + 2 }) new_comment_left = comment.right new_comment_right = comment.right + 1 comment.right += 2 model.session.add(comment) else: max_right, = model.session.query(func.max(model.Comment.right)) \ .with_parent(discussion) \ .one() if max_right is None: # FIRST POST LOLOL no seriously default to 1 and 2. max_right = 0 new_comment_left = max_right + 1 new_comment_right = max_right + 2 new_comment = model.Comment( discussion=discussion, author=request.user, content=comment_form.message.data, left=new_comment_left, right=new_comment_right, ) discussion.comment_count += 1 model.session.add(new_comment) model.session.add(discussion) model.session.flush() # Need to get the new comment's id # Redirect to the new comment return HTTPSeeOther(location=request.route_url( 'comments.view', comment=new_comment, _anchor="comment-{0}".format(new_comment.id)))