Avoid StaleDataError exception during user.recalculate_attitude()

This commit is contained in:
rimu 2024-11-02 16:34:16 +13:00
parent f36135d163
commit 915e8c779a

View file

@ -895,20 +895,21 @@ class User(UserMixin, db.Model):
def recalculate_attitude(self):
upvotes = downvotes = 0
last_50_votes = PostVote.query.filter(PostVote.user_id == self.id).order_by(-PostVote.id).limit(50)
for vote in last_50_votes:
if vote.effect > 0:
upvotes += 1
if vote.effect < 0:
downvotes += 1
with db.session.no_autoflush: # Avoid StaleDataError exception
last_50_votes = PostVote.query.filter(PostVote.user_id == self.id).order_by(-PostVote.id).limit(50)
for vote in last_50_votes:
if vote.effect > 0:
upvotes += 1
if vote.effect < 0:
downvotes += 1
comment_upvotes = comment_downvotes = 0
last_50_votes = PostReplyVote.query.filter(PostReplyVote.user_id == self.id).order_by(-PostReplyVote.id).limit(50)
for vote in last_50_votes:
if vote.effect > 0:
comment_upvotes += 1
if vote.effect < 0:
comment_downvotes += 1
comment_upvotes = comment_downvotes = 0
last_50_votes = PostReplyVote.query.filter(PostReplyVote.user_id == self.id).order_by(-PostReplyVote.id).limit(50)
for vote in last_50_votes:
if vote.effect > 0:
comment_upvotes += 1
if vote.effect < 0:
comment_downvotes += 1
total_upvotes = upvotes + comment_upvotes
total_downvotes = downvotes + comment_downvotes