[FIX] website_forum: respect karma when posting questions/answers/comments + more user-friendly errors

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.
This commit is contained in:
Olivier Dony 2014-10-21 01:19:05 +02:00
parent bfb61cd5e9
commit b9953acb0f
3 changed files with 27 additions and 14 deletions

View 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)

View 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"

View File

@ -99,7 +99,10 @@
<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>
@ -325,7 +328,8 @@
<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");
@ -411,7 +415,8 @@
<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");
@ -488,7 +493,7 @@
</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) }">
@ -588,7 +593,7 @@
<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>