From d36175eba0bf39da49313f2d44e21a37f142cfaa Mon Sep 17 00:00:00 2001 From: "Turkesh Patel (Open ERP)" Date: Thu, 6 Mar 2014 18:47:36 +0530 Subject: [PATCH] [IMP] improved code of vote. bzr revid: tpa@tinyerp.com-20140306131736-rxch9ru8d49spzsf --- addons/website_forum/controllers/main.py | 34 +++++---------- addons/website_forum/models/website_forum.py | 4 +- addons/website_forum/views/website_forum.xml | 44 ++++---------------- 3 files changed, 21 insertions(+), 61 deletions(-) diff --git a/addons/website_forum/controllers/main.py b/addons/website_forum/controllers/main.py index ee4239668fb..e592f272ebd 100644 --- a/addons/website_forum/controllers/main.py +++ b/addons/website_forum/controllers/main.py @@ -289,29 +289,17 @@ class website_forum(http.Controller): @http.route('/forum//post_vote/', type='http', auth="user", multilang=True, methods=['POST'], website=True) def post_vote(self, forum, **post): - cr, uid, context = request.cr, request.uid, request.context - vote_obj = request.registry['website.forum.post.vote'] - post_id = post.get('post_id'); - post_ids = vote_obj.search(cr, uid, [('post_id', '=', int(post_id))],context=None) + cr, uid, context, post_id = request.cr, request.uid, request.context, int(post.get('post_id')) + Vote = request.registry['website.forum.post.vote'] + vote_ids = Vote.search(cr, uid, [('post_id', '=', post_id)], context=context) - if (not post_ids) and '1' == post.get('vote'): - up_post_vote_id = vote_obj.create( - cr, uid, { - 'post_id': post_id, - 'user_id': uid, - 'vote': post.get('vote'), - }, context=context) - elif post_ids and '1' == post.get('vote'): - vote_obj.unlink(cr, uid, post_ids, context) - - if (not post_ids) and '-1' == post.get('vote'): - down_post_vote_id = vote_obj.create( - cr, uid, { - 'post_id': post_id, - 'user_id': uid, - 'vote': post.get('vote'), - }, context=context) - elif post_ids and '-1' == post.get('vote'): - vote_obj.unlink(cr, uid, post_ids, context) + if vote_ids: + Vote.unlink(cr, uid, vote_ids, context=context) + else: + Vote.create(cr, uid, { + 'post_id': post_id, + 'user_id': uid, + 'vote': post.get('vote'), + }, context=context) return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.get('question_id'))) diff --git a/addons/website_forum/models/website_forum.py b/addons/website_forum/models/website_forum.py index 4d01988a7f8..f69386e94e3 100644 --- a/addons/website_forum/models/website_forum.py +++ b/addons/website_forum/models/website_forum.py @@ -55,7 +55,7 @@ class Post(osv.Model): res[post.id] = -1 return res - def _get_votes_length(self, cr, uid, ids, field_name, arg, context): + def _get_vote_count(self, cr, uid, ids, field_name, arg, context): res = dict.fromkeys(ids, 0) for post in self.browse(cr, uid, ids, context=context): if post.vote_ids: @@ -98,7 +98,7 @@ class Post(osv.Model): help="Comments on forum post", ), 'user_vote':fields.function(_get_votes, string="Number of user votes", type='boolean'), - 'vote_count':fields.function(_get_votes_length, string="Number of user votes count", type='integer'), + 'vote_count':fields.function(_get_vote_count, string="Number of user votes count", type='integer'), } _defaults = { diff --git a/addons/website_forum/views/website_forum.xml b/addons/website_forum/views/website_forum.xml index e3c00ab8190..5d3741cee3b 100644 --- a/addons/website_forum/views/website_forum.xml +++ b/addons/website_forum/views/website_forum.xml @@ -332,25 +332,11 @@