From ef332df319ee6fda8026470b4b293120c22dc4eb Mon Sep 17 00:00:00 2001 From: Deimos Date: Fri, 31 Jan 2020 11:58:29 -0700 Subject: [PATCH] Fix autoflush issue with repeated comment votes I removed this no_autoflush block in another recent change, but it was still necessary due to the calls to _mark_comment_read_from_interaction not being inside the try: block looking for an IntegrityError. This could also be done in a different order to avoid the issue without needing to disable autoflush, but this works fine. --- tildes/tildes/views/api/web/comment.py | 29 ++++++++++++++++++-------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/tildes/tildes/views/api/web/comment.py b/tildes/tildes/views/api/web/comment.py index c6dc784..0125b5c 100644 --- a/tildes/tildes/views/api/web/comment.py +++ b/tildes/tildes/views/api/web/comment.py @@ -33,11 +33,12 @@ def _mark_comment_read_from_interaction(request: Request, comment: Comment) -> N if not request.user.interact_mark_notifications_read: return - request.query(CommentNotification).filter( - CommentNotification.user == request.user, - CommentNotification.comment == comment, - CommentNotification.is_unread == True, # noqa - ).update({"is_unread": False}, synchronize_session=False) + with request.db_session.no_autoflush: + request.query(CommentNotification).filter( + CommentNotification.user == request.user, + CommentNotification.comment == comment, + CommentNotification.is_unread == True, # noqa + ).update({"is_unread": False}, synchronize_session=False) @ic_view_config( @@ -199,8 +200,13 @@ def put_vote_comment(request: Request) -> dict: # the user has already voted on this comment savepoint.rollback() - # a trigger updates this - set it manually so the button displays the correct number - comment.num_votes += 1 + # re-query the comment to get updated data + comment = ( + request.query(Comment) + .join_all_relationships() + .filter_by(comment_id=comment.comment_id) + .one() + ) return {"name": "vote", "subject": comment, "is_toggled": True} @@ -226,8 +232,13 @@ def delete_vote_comment(request: Request) -> dict: # manually commit the transaction so triggers will execute request.tm.commit() - # a trigger updates this - set it manually so the button displays the correct number - comment.num_votes -= 1 + # re-query the comment to get updated data + comment = ( + request.query(Comment) + .join_all_relationships() + .filter_by(comment_id=comment.comment_id) + .one() + ) return {"name": "vote", "subject": comment, "is_toggled": False}