Simone Marzola committed 5f59c2e

catch IntegrityErrors in vote transaction commit and redirect to referer flashing error message

  • Participants
  • Parent commits ca1aadf

Comments (0)

Files changed (1)

File tgcomments/controllers/

 # -*- coding: utf-8 -*-
 """Main Controller"""
+from sqlalchemy.exc import IntegrityError
 from tg import TGController
 from tg import expose, flash, require, url, lurl, request, redirect, validate, config
 from tgcomments import model
 from tgcomments.model import DBSession, Comment, CommentVote
 from tgcomments.lib import get_user_gravatar, notify_comment_on_facebook, make_fake_comment_entity, FakeCommentEntity
+import transaction
     from tg import predicates
     def vote(self, comment, value):
+        transaction.begin()
         user = request.identity['user']
-        vote = DBSession.query(CommentVote).filter_by(comment=comment).filter_by(user=user).first()
-        if not vote:
+        vote = DBSession.query(CommentVote).filter_by(comment=comment, user=user).first()
+        if vote is None:
             vote = CommentVote(comment=comment, user=user)
         max_vote_value = votes_range[1]
         vote.value = min(max(min_vote_value, value), max_vote_value)
+        try:
+            DBSession.flush()
+            transaction.commit()
+        except IntegrityError:
+            return back_to_referer(success=False)
         flash(_('Thanks for your vote!'))
         return back_to_referer(success=True)