website_forum: fixed issue when voting. The method should work in batch. Previously...
authorThibault Delavallée <tde@openerp.com>
Tue, 15 Jul 2014 14:44:55 +0000 (16:44 +0200)
committerRichard Mathot <rim@openerp.com>
Wed, 16 Jul 2014 08:39:38 +0000 (10:39 +0200)
addons/website_forum/models/forum.py

index caa9dff..edcddfe 100644 (file)
@@ -256,6 +256,8 @@ class Post(osv.Model):
     def vote(self, cr, uid, ids, upvote=True, context=None):
         Vote = self.pool['forum.post.vote']
         vote_ids = Vote.search(cr, uid, [('post_id', 'in', ids), ('user_id', '=', uid)], context=context)
+        new_vote = '1' if upvote else '-1'
+        voted_forum_ids = set()
         if vote_ids:
             for vote in Vote.browse(cr, uid, vote_ids, context=context):
                 if upvote:
@@ -263,9 +265,9 @@ class Post(osv.Model):
                 else:
                     new_vote = '0' if vote.vote == '1' else '-1'
                 Vote.write(cr, uid, vote_ids, {'vote': new_vote}, context=context)
-        else:
+                voted_forum_ids.add(vote.post_id.id)
+        for post_id in set(ids) - voted_forum_ids:
             for post_id in ids:
-                new_vote = '1' if upvote else '-1'
                 Vote.create(cr, uid, {'post_id': post_id, 'vote': new_vote}, context=context)
         return {'vote_count': self._get_vote_count(cr, uid, ids, None, None, context=context)[ids[0]]}