From 73cb78e2115720b80f018bf0aed8b3d47d32a57e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 9 Apr 2014 19:39:41 +0200 Subject: [PATCH] [CLEAN] website_forum: cleaned vote feature before merging bzr revid: tde@openerp.com-20140409173941-4pc7p8bre5btgbhu --- addons/website_forum/controllers/main.py | 2 +- addons/website_forum/models/forum.py | 48 ++++++++++++------------ 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/addons/website_forum/controllers/main.py b/addons/website_forum/controllers/main.py index 70278056fe8..cb2722b78dd 100644 --- a/addons/website_forum/controllers/main.py +++ b/addons/website_forum/controllers/main.py @@ -448,7 +448,7 @@ class WebsiteForum(http.Controller): if not request.session.uid: return {'error': 'anonymous_user'} cr, uid, context, post_id = request.cr, request.uid, request.context, int(post.get('post_id')) - return request.registry['website.forum.post'].vote(cr, uid, [post_id], post.get('vote'), context) + return request.registry['website.forum.post'].vote(cr, uid, [post_id], upvote=(post.get('vote') == '1'), context=context) @http.route('/forum/post_delete', type='json', auth="user", multilang=True, methods=['POST'], website=True) def delete_answer(self, **kwarg): diff --git a/addons/website_forum/models/forum.py b/addons/website_forum/models/forum.py index d073e1c1aa1..6657dbedb04 100644 --- a/addons/website_forum/models/forum.py +++ b/addons/website_forum/models/forum.py @@ -159,32 +159,30 @@ class Post(osv.Model): self.pool['res.users'].write(cr, SUPERUSER_ID, [post.user_id.id], {'karma': value}, context=context) return res - def vote(self, cr, uid, ids, vote, context=None): - print ids, vote - return True - # try: - # vote = int(vote) - # except: - # return {'error': 'Wrong Vote Value'} + def vote(self, cr, uid, ids, upvote=True, context=None): + Vote = self.pool['website.forum.post.vote'] + user = self.pool['res.users'].browse(cr, uid, uid, context=context) + # must have at least 10 karma to vote + if not upvote and user.karma <= 10: + return {'error': 'lessthen_10_karma'} + # user can not vote on own post + posts = self.pool['website.forum.post'].browse(cr, uid, ids, context=context) + if any(post.user_id.id == uid for post in posts): + return {'error': 'own_post'} - # if not vote in [-1, 0, 1]: - # return {'error': 'Wrong Vote Value'} - # user = self.pool['res.users'].browse(cr, uid, uid, context=context) - # # must have at least 10 karma to vote - # if (vote == '-1') and (user.karma <= 10): - # return {'error': 'lessthen_10_karma'} - # # user can not vote on own post - # posts = self.pool['website.forum.post'].browse(cr, uid, ids, context=context) - # if any(post.user_id.id == uid for post in posts): - # return {'error': 'own_post'} - - # vote_ids = self.pool['website.forum.post.vote'].search(cr, uid, [('post_id', 'in', post_ids), ('user_id', '=', uid)], context=context) - # if vote_ids: - # self.pool['website.forum.post.vote'].write(cr, uid, vote_ids, {'vote': new_vote}, context=context) - # else: - # self.popol['website.forum.post.vote'].create(cr, uid, {'post_id': post_id, 'vote': vote}, context=context) - # post.refresh() - # return {'vote_count': post.vote_count} + vote_ids = Vote.search(cr, uid, [('post_id', 'in', ids), ('user_id', '=', uid)], context=context) + if vote_ids: + for vote in Vote.browse(cr, uid, vote_ids, context=context): + if upvote: + new_vote = '0' if vote.vote == '-1' else '1' + else: + new_vote = '0' if vote.vote == '1' else '-1' + Vote.write(cr, uid, vote_ids, {'vote': new_vote}, context=context) + else: + 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]]} def set_viewed(self, cr, uid, ids, context=None): for post in self.browse(cr, uid, ids, context=context):