website_forum: fixed issue when voting. The method should work in batch. Previously to this implementation having one post with a vote bypassed the other posts without vote when called with several ids.

This commit is contained in:
Thibault Delavallée 2014-07-15 16:44:55 +02:00 committed by Richard Mathot
parent c959f49179
commit 9955624029
1 changed files with 4 additions and 2 deletions

View 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]]}