[IMP] website_forum: karma management for vote
authorThibault Delavallée <tde@openerp.com>
Fri, 11 Apr 2014 18:22:54 +0000 (20:22 +0200)
committerThibault Delavallée <tde@openerp.com>
Fri, 11 Apr 2014 18:22:54 +0000 (20:22 +0200)
bzr revid: tde@openerp.com-20140411182254-0wyvzpcqxk4jyqev

addons/website_forum/controllers/main.py
addons/website_forum/static/src/js/website_forum.js
addons/website_forum/views/website_forum.xml

index 99446f4..c1d0adb 100644 (file)
@@ -285,9 +285,9 @@ class WebsiteForum(http.Controller):
         cr, uid, context = request.cr, request.uid, request.context
         if not request.session.uid:
             return {'error': 'anonymous_user'}
-        # # if user have not access to accept answer then reise warning
+        # if user have not access to accept answer then reise warning
         if post.parent_id is False or post.parent_id.create_uid.id != uid:
-            return {'error': 'user'}
+            return {'error': 'own_post'}
 
         # set all answers to False, only one can be accepted
         request.registry['forum.post'].write(cr, uid, [c.id for c in post.parent_id.child_ids], {'is_correct': False}, context=context)
@@ -344,13 +344,22 @@ class WebsiteForum(http.Controller):
         # check for karma and not self vote
         if not request.session.uid:
             return {'error': 'anonymous_user'}
+        if request.uid == post.create_uid.id:
+            return {'error': 'own_post'}
+        user = request.registry['res.users'].browse(request.cr, SUPERUSER_ID, request.uid, context=request.context)
+        if user.karma <= 5:
+            return {'error': 'not_enough_karma', 'karma': 5}
         return request.registry['forum.post'].vote(request.cr, request.uid, [post.id], upvote=True, context=request.context)
 
     @http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/downvote', type='json', auth="public", multilang=True, website=True)
     def post_downvote(self, forum, post, **kwargs):
-        # check for karma and not self vote
         if not request.session.uid:
             return {'error': 'anonymous_user'}
+        if request.uid == post.create_uid.id:
+            return {'error': 'own_post'}
+        user = request.registry['res.users'].browse(request.cr, SUPERUSER_ID, request.uid, context=request.context)
+        if user.karma <= 50:
+            return {'error': 'not_enough_karma', 'karma': 50}
         return request.registry['forum.post'].vote(request.cr, request.uid, [post.id], upvote=False, context=request.context)
 
     # User
@@ -464,18 +473,15 @@ class WebsiteForum(http.Controller):
         return request.website.render("website_forum.user_detail_full", values)
 
     @http.route('/forum/<model("forum.forum"):forum>/user/<model("res.users"):user>/edit', type='http', auth="user", multilang=True, website=True)
-    def edit_profile(self, forum, user, **kwarg):
-        cr, context = request.cr, request.context
+    def edit_profile(self, forum, user, **kwargs):
         country = request.registry['res.country']
-        country_ids = country.search(cr, SUPERUSER_ID, [], context=context)
-        countries = country.browse(cr, SUPERUSER_ID, country_ids, context)
-        values = {
-            'user': user,
-            'forum': forum,
-            'searches': kwarg,
+        country_ids = country.search(request.cr, SUPERUSER_ID, [], context=request.context)
+        countries = country.browse(request.cr, SUPERUSER_ID, country_ids, context=request.context)
+        values = self._prepare_forum_values(forum=forum, searches=kwargs)
+        values.update({
             'countries': countries,
             'notifications': self._get_notifications(),
-        }
+        })
         return request.website.render("website_forum.edit_profile", values)
 
     @http.route('/forum/<model("forum.forum"):forum>/user/<model("res.users"):user>/save', type='http', auth="user", multilang=True, website=True)
@@ -488,8 +494,7 @@ class WebsiteForum(http.Controller):
             'country_id': kwargs.get('country'),
             'website_description': kwargs.get('description'),
         }, context=request.context)
-        user.refresh()
-        return werkzeug.utils.redirect("/forum/%s/user/%s" % (slug(forum), slug(user)))
+        return werkzeug.utils.redirect("/forum/%s/user/%d" % (slug(forum), user.id))
 
     # Badges
     # --------------------------------------------------
index cee5348..2c42874 100644 (file)
@@ -17,11 +17,10 @@ $(document).ready(function () {
                             'Sorry you must be logged to vote'+
                             '</div>');
                     }
-                    else if (data['error'] == 'lessthen_10_karma')
-                    {
+                    else if (data['error'] == 'not_enough_karma') {
                         var $warning = $('<div class="alert alert-danger alert-dismissable" id="vote_alert" style="position:absolute; margin-top: -30px; margin-left: 90px;">'+
                             '<button type="button" class="close notification_close" data-dismiss="alert" aria-hidden="true">&times;</button>'+
-                            '10 karma required to downvote'+
+                            'Sorry, at least ' + data['karma'] + ' karma is required to vote'+
                             '</div>');
                     }
                     vote_alert = $link.parent().find("#vote_alert");
@@ -55,7 +54,7 @@ $(document).ready(function () {
                         '<button type="button" class="close notification_close" data-dismiss="alert" aria-hidden="true">&times;</button>'+
                         'Sorry, anonymous users cannot choose correct answer.'+
                         '</div>');
-                } else if (data['error'] == 'user'){
+                } else if (data['error'] == 'own_post'){
                     var $warning = $('<div class="alert alert-danger alert-dismissable" id="correct_answer_alert" style="position:absolute; margin-top: -30px; margin-left: 90px;">'+
                         '<button type="button" class="close notification_close" data-dismiss="alert" aria-hidden="true">&times;</button>'+
                         'Sorry, the user who asked this question can only accept the answer as correct.'+
index df4cde6..2d1787a 100644 (file)
                         <span t-field="user.city"/>
                         <span t-if="user.city and user.country_id">, </span>
                         <span t-field="user.country_id"/>
-                        <t t-if="user.country_id"> <span t-field="user.country_id.image" t-field-options='{"widget": "image", "class":"forum_user_flag"}'/></t>
+                        <t t-if="user.country_id"> <span t-field="user.country_id.image" t-field-options='{"widget": "image", "class": "country_flag"}'/></t>
                         
                     </td>
                 </tr>