[FIX] website_forum: respect karma when posting questions/answers/comments + more...
authorOlivier Dony <odo@openerp.com>
Mon, 20 Oct 2014 23:19:05 +0000 (01:19 +0200)
committerOlivier Dony <odo@openerp.com>
Mon, 20 Oct 2014 23:23:28 +0000 (01:23 +0200)
While posting new questions and answers the check
for karma limit was bypassed because it was using
super-user mode: use regular user instead.
Also improve the user feedback when karma level is
too low to perform some actions: post comment,
post question, post answer.
The usability in these cases still needs to be
improved.

addons/website_forum/controllers/main.py
addons/website_forum/models/forum.py
addons/website_forum/views/website_forum.xml

index ea68250..08e03d9 100644 (file)
@@ -298,12 +298,10 @@ class WebsiteForum(http.Controller):
         cr, uid, context = request.cr, request.uid, request.context
         if kwargs.get('comment') and post.forum_id.id == forum.id:
             # TDE FIXME: check that post_id is the question or one of its answers
-            request.registry['forum.post'].message_post(
-                cr, uid, post.id,
+            request.registry['forum.post']._post_comment(
+                cr, uid, post,
                 body=kwargs.get('comment'),
-                type='comment',
-                subtype='mt_comment',
-                context=dict(context, mail_create_nosubcribe=True))
+                context=context)
         return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
 
     @http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/toggle_correct', type='json', auth="public", website=True)
index b39298a..1bc9b69 100644 (file)
@@ -10,8 +10,9 @@ from openerp.osv import osv, fields
 from openerp.tools import html2plaintext
 from openerp.tools.translate import _
 
+from werkzeug.exceptions import Forbidden
 
-class KarmaError(ValueError):
+class KarmaError(Forbidden):
     """ Karma-related error, used for forum and posts. """
     pass
 
@@ -314,11 +315,11 @@ class Post(osv.Model):
             context = {}
         create_context = dict(context, mail_create_nolog=True)
         post_id = super(Post, self).create(cr, uid, vals, context=create_context)
-        post = self.browse(cr, SUPERUSER_ID, post_id, context=context)  # SUPERUSER_ID to avoid read access rights issues when creating
+        post = self.browse(cr, uid, post_id, context=context)
         # karma-based access
-        if post.parent_id and not post.can_ask:
+        if not post.parent_id and not post.can_ask:
             raise KarmaError('Not enough karma to create a new question')
-        elif not post.parent_id and not post.can_answer:
+        elif post.parent_id and not post.can_answer:
             raise KarmaError('Not enough karma to answer to a question')
         # messaging and chatter
         base_url = self.pool['ir.config_parameter'].get_param(cr, uid, 'web.base.url')
@@ -508,6 +509,15 @@ class Post(osv.Model):
         res_id = post.parent_id and "%s#answer-%s" % (post.parent_id.id, post.id) or post.id
         return "/forum/%s/question/%s" % (post.forum_id.id, res_id)
 
+    def _post_comment(self, cr, uid, post, body, context=None):
+        context = dict(context or {}, mail_create_nosubcribe=True)
+        if not post.can_comment:
+            raise KarmaError('Not enough karma to comment')
+        return self.message_post(cr, uid, post.id,
+                                 body=body,
+                                 type='comment',
+                                 subtype='mt_comment',
+                                 context=context)
 
 class PostReason(osv.Model):
     _name = "forum.post.reason"
index 9f1ef51..9fbd27e 100644 (file)
                     <t t-raw="0"/>
                 </div>
                 <div class="col-sm-3" id="right-column">
-                    <a t-if="not header.get('ask_hide')" class="btn btn-primary btn-lg btn-block mb16" t-attf-href="/forum/#{slug(forum)}/ask">Ask a Question</a>
+                    <a t-if="not header.get('ask_hide')"
+                       t-attf-class="btn btn-primary btn-lg btn-block mb16 #{user.karma &gt;= forum.karma_ask and '' or 'karma_required'}"
+                       t-attf-href="/forum/#{slug(forum)}/ask"
+                       t-attf-data-karma="#{forum.karma_ask}">Ask a Question</a>
                     <div class="panel panel-default">
                         <div class="panel-heading">
                             <h3 class="panel-title">Keep Informed</h3>
             <br/>
             <input type="text" name="question_tags" placeholder="Tags" class="form-control load_tags"/>
             <br/>
-            <button class="btn btn-primary" id="btn_ask_your_question">Post Your Question</button>
+            <button t-attf-class="btn btn-primary #{(user.karma &lt;= forum.karma_ask) and 'karma_required' or ''}"
+                id="btn_ask_your_question" t-att-data-karma="forum.karma_ask">Post Your Question</button>
         </form>
         <script type="text/javascript">
             CKEDITOR.replace("content");
     <form t-attf-action="/forum/#{ slug(forum) }/post/#{slug(question)}/new" method="post" role="form">
         <input type="hidden" name="karma" t-attf-value="#{user.karma}" id="karma"/>
         <textarea name="content" class="form-control load_editor" required="True"/>
-        <button class="btn btn-primary mt16" id="btn_ask_your_question">Post Your Answer</button>
+        <button t-attf-class="btn btn-primary mt16 #{not question.can_answer and 'karma_required' or ''}"
+                id="btn_ask_your_question" t-att-data-karma="question.karma_answer">Post Your Answer</button>
     </form>
     <script type="text/javascript">
         CKEDITOR.replace("content");
                         </div>
                         <ul class="list-inline" id="options">
                             <li>
-                                <a style="cursor: pointer" data-toggle="collapse"
+                                <a style="cursor: pointer" t-att-data-toggle="question.can_comment and 'collapse' or ''"
                                     t-attf-class="fa fa-comment-o #{not question.can_comment and 'karma_required text-muted' or ''}"
                                     t-attf-data-karma="#{not question.can_comment and question.karma_comment or 0}"
                                     t-attf-data-target="#comment#{ question._name.replace('.','') + '-' + str(question.id) }">
                             <li>
                                 <a t-attf-class="fa fa-comment-o #{not answer.can_comment and 'karma_required text-muted' or ''}"
                                     t-attf-data-karma="#{not answer.can_comment and answer.karma_comment or 0}"
-                                    style="cursor: pointer" data-toggle="collapse"
+                                    style="cursor: pointer" t-att-data-toggle="answer.can_comment and 'collapse' or ''"
                                     t-attf-data-target="#comment#{ answer._name.replace('.','') + '-' + str(answer.id) }"> Comment
                                 </a>
                             </li>