From f02c071457e1ac1e574ba1e113c09b05ea1e7b93 Mon Sep 17 00:00:00 2001 From: "Sunil Sharma (OpenERP)" Date: Thu, 6 Mar 2014 15:10:42 +0530 Subject: [PATCH] [imp]:website_form:improve user vote for post bzr revid: sunilsharma.sharma07@gmail.com-20140306094042-sfpi19f41o3u8k95 --- addons/website_forum/controllers/main.py | 29 +++++++++ addons/website_forum/models/website_forum.py | 32 +++++++++- addons/website_forum/views/website_forum.xml | 67 ++++++++++++++++++-- 3 files changed, 119 insertions(+), 9 deletions(-) diff --git a/addons/website_forum/controllers/main.py b/addons/website_forum/controllers/main.py index 3a817a7e31b..39ec0002c9d 100644 --- a/addons/website_forum/controllers/main.py +++ b/addons/website_forum/controllers/main.py @@ -264,3 +264,32 @@ class website_forum(http.Controller): } return request.website.render("website_forum.users", values) + + @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) + + 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) + + 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 7a0bd4b42e6..819791cb101 100644 --- a/addons/website_forum/models/website_forum.py +++ b/addons/website_forum/models/website_forum.py @@ -42,6 +42,29 @@ class Post(osv.Model): _name = 'website.forum.post' _description = "Question" _inherit = ['mail.thread', 'website.seo.metadata'] + + def _get_votes(self, cr, uid, ids, field_name, arg, context): + res = dict.fromkeys(ids, False) + for post in self.browse(cr, uid, ids, context=context): + if post.vote_ids: + for vote in post.vote_ids: + if vote.user_id.id == uid: + if vote.vote == '1': + res[post.id] = 1 + else: + res[post.id] = -1 + return res + + def _get_votes_length(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: + for vote in post.vote_ids: + if vote.vote == '1': + res[post.id] += 1 + else: + res[post.id] -= 1 + return res _columns = { 'forum_id': fields.many2one('website.forum', 'Forum', required=True), @@ -73,6 +96,9 @@ class Post(osv.Model): string='Post Messages', 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'), + } _defaults = { 'state': 'active', @@ -122,7 +148,7 @@ class Post(osv.Model): if method == 'unlink': res.update({'type': 'deleted_question'}) if method == 'vote': - res.update({'type': 'voteted_question'}) + res.update({'type': 'voted_question'}) Activity.create(cr, uid, res, context=context) @@ -188,12 +214,12 @@ class Vote(osv.Model): _columns = { 'post_id': fields.many2one('website.forum.post', 'Post', required=True), 'user_id': fields.many2one('res.users', 'User'), - 'vote': fields.integer('rate'), + 'vote': fields.selection([('1', '1'),('-1', '-1')], 'rate'), } def create(self, cr, uid, vals, context=None): vote_id = super(Vote, self).create(cr, uid, vals, context=context) - self.pool['website.forum.post'].create_activity(cr, uid, [vals.get('post_id')], method='vote', context=context) + self.pool['website.forum.post'].create_activity(cr, uid, [int(vals.get('post_id'))], method='vote', context=context) return vote_id class ForumActivity(osv.Model): diff --git a/addons/website_forum/views/website_forum.xml b/addons/website_forum/views/website_forum.xml index c8803a06d37..635ec62ca26 100644 --- a/addons/website_forum/views/website_forum.xml +++ b/addons/website_forum/views/website_forum.xml @@ -306,12 +306,51 @@