From 556d7dfa2a662211e30050fc25d1dbf8f3a35b7f Mon Sep 17 00:00:00 2001 From: "Sunil Sharma (OpenERP)" Date: Wed, 26 Mar 2014 17:44:24 +0530 Subject: [PATCH] [imp]:improve own_post or annonymous user vote alert bzr revid: sunilsharma.sharma07@gmail.com-20140326121424-a1oompnmu2f0132t --- addons/website_forum/controllers/main.py | 4 +++- addons/website_forum/models/forum.py | 2 +- .../website_forum/static/src/js/website_forum.js | 14 +++++++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/addons/website_forum/controllers/main.py b/addons/website_forum/controllers/main.py index 5f84c2a2999..61111af9d27 100644 --- a/addons/website_forum/controllers/main.py +++ b/addons/website_forum/controllers/main.py @@ -411,8 +411,10 @@ class website_forum(http.Controller): return request.website.render("website_forum.users", values) - @http.route('/forum/post_vote/', type='json', auth="user", multilang=True, methods=['POST'], website=True) + @http.route('/forum/post_vote/', type='json', auth="public", multilang=True, methods=['POST'], website=True) def post_vote(self, **post): + 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')) Vote = request.registry['website.forum.post.vote'] return Vote.vote(cr, uid, post_id, post.get('vote'), context) diff --git a/addons/website_forum/models/forum.py b/addons/website_forum/models/forum.py index e76024f8049..ec850c3fc4d 100644 --- a/addons/website_forum/models/forum.py +++ b/addons/website_forum/models/forum.py @@ -338,7 +338,7 @@ class Vote(osv.Model): #user can not vote on own post. post = self.pool['website.forum.post'].browse(cr, uid, post_id, context=context) if post.user_id.id == uid: - return False + return {'error': 'own_post'} vote_ids = self.search(cr, uid, [('post_id', '=', post_id), ('user_id','=',uid)], context=context) if vote_ids: #when user will click again on vote it should set it 0. diff --git a/addons/website_forum/static/src/js/website_forum.js b/addons/website_forum/static/src/js/website_forum.js index 2bc85119f39..13dc84804d5 100644 --- a/addons/website_forum/static/src/js/website_forum.js +++ b/addons/website_forum/static/src/js/website_forum.js @@ -4,17 +4,25 @@ $(document).ready(function () { ev.preventDefault(); var $link = $(ev.currentTarget); var value = $link.attr("value") + openerp.jsonRpc("/forum/post_vote/", 'call', { 'post_id': $link.attr("id"), 'vote': value}) .then(function (data) { - if (data == false){ - vote_alert = $link.parent().find("#vote_alert"); - if (vote_alert.length == 0) { + if (data['error']){ + if (data['error'] == 'own_post'){ var $warning = $('
'+ ''+ 'Sorry, you cannot vote for your own posts'+ '
'); + } else if (data['error'] == 'anonymous_user'){ + var $warning = $('
'+ + ''+ + 'Sorry, anonymous users cannot vote'+ + '
'); + } + vote_alert = $link.parent().find("#vote_alert"); + if (vote_alert.length == 0) { $link.parent().append($warning); } } else {