[IMP] improved code to raise warning on accepting anser.

bzr revid: tpa@tinyerp.com-20140401132806-102jkjwxj2esukcr
This commit is contained in:
Turkesh Patel (Open ERP) 2014-04-01 18:58:06 +05:30
parent c3610687fc
commit 30b49e20d4
6 changed files with 47 additions and 29 deletions

View File

@ -534,24 +534,27 @@ class website_forum(http.Controller):
}, context=context)
return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.get('post_id')))
@http.route('/forum/correct_answer/', type='json', auth="user", multilang=True, methods=['POST'], website=True)
@http.route('/forum/correct_answer/', type='json', auth="public", multilang=True, methods=['POST'], website=True)
def correct_answer(self, **kwarg):
cr, uid, context = request.cr, request.uid, request.context
if not request.session.uid:
return {'error': 'anonymous_user'}
Post = request.registry['website.forum.post']
post = Post.browse(cr, uid, int(kwarg.get('post_id')), context=context)
user = request.registry['res.users'].browse(cr, uid, uid, context=None)
if post.user_id.id == uid or user.karma >= 500:
correct = False if post.correct else True
#Note: only one answer can be right.
for child in post.parent_id.child_ids:
if child.correct and child.id != post.id:
Post.write( cr, uid, [child.id], {
'correct': False,
}, context=context)
Post.write( cr, uid, [post.id, post.parent_id.id], {
'correct': correct,
}, context=context)
return correct
#if user have not access to accept answer then reise warning
if not (post.parent_id.user_id.id == uid or user.karma >= 500):
return {'error': 'user'}
#Note: only one answer can be right.
correct = False if post.correct else True
for child in post.parent_id.child_ids:
if child.correct and child.id != post.id:
Post.write( cr, uid, [child.id], { 'correct': False }, context=context)
Post.write( cr, uid, [post.id, post.parent_id.id], { 'correct': correct }, context=context)
return correct
@http.route('/forum/<model("website.forum"):forum>/close/question/<model("website.forum.post"):post>', type='http', auth="user", multilang=True, website=True)
def close_question(self, forum, post, **kwarg):

View File

@ -282,7 +282,7 @@ Thank you in advance</field>
</field>
<field name="parent_id" ref="question_6" />
<field name="correct">True</field>
<field name="user_id" ref="base.user_root"/>
<field name="user_id" ref="base.user_demo"/>
</record>
<record id="answer_6_1" model="website.forum.post">
@ -356,7 +356,7 @@ Thank you in advance</field>
</field>
<field name="parent_id" ref="question_13" />
<field name="correct">True</field>
<field name="user_id" ref="base.user_demo"/>
<field name="user_id" ref="base.user_root"/>
</record>
<!-- Post Vote -->

View File

@ -239,10 +239,6 @@ class Post(osv.Model):
user = self.pool['res.users'].browse(cr, uid ,uid, context=context)
self.create_history(cr, uid, ids, context=context)
for post in self.browse(cr, uid, ids, context=context):
#just for precaution
if user.karma < 300 and post.user_id != uid:
raise osv.except_osv(_('Error!'), _('You are not allowed to edit this post!'))
body, subtype = "Edited question", "website_forum.mt_question_edit"
if post.parent_id:
body, subtype = "Edited answer", "website_forum.mt_answer_edit"

View File

@ -59,4 +59,8 @@
.load_tags{
width: 845px !important;
}
.fa-thumbs-up, .fa-thumbs-down, .oe_answer_true, .oe_answer_false {
cursor:pointer;
}

View File

@ -64,15 +64,30 @@ $(document).ready(function () {
ev.preventDefault();
var $link = $(ev.currentTarget);
openerp.jsonRpc("/forum/correct_answer/", 'call', {
'post_id': $link.attr("id")})
.then(function (data) {
par = $link.parents().find(".oe_answer_true")
$link.parents().find(".oe_answer_true").removeClass("oe_answer_true").addClass('oe_answer_false')
if (data) {
'post_id': $link.attr("id")})
.then(function (data) {
if (data['error']) {
if (data['error'] == 'anonymous_user'){
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, anonymous users cannot choose correct answer.'+
'</div>');
} else if (data['error'] == 'user'){
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, You cannot choose correct answer.'+
'</div>');
}
correct_answer_alert = $link.parent().find("#correct_answer_alert");
if (correct_answer_alert.length == 0) {
$link.parent().append($warning);
}
} else {
$link.parents().find(".oe_answer_true").removeClass("oe_answer_true").addClass('oe_answer_false');
$link.removeClass("oe_answer_false").addClass('oe_answer_true');
}
});
return false;
}
});
return true;
});
$('.comment_delete').on('click', function (ev) {

View File

@ -448,8 +448,8 @@
<li><a class="text-muted fa fa-share" href="">share</a></li>
</ul>
</div>
<t t-set="user" t-value="question.user_id"/>
<t t-call="website_forum.user_detail">
<t t-set="user" t-value="question.user_id"/>
<span class="text-muted">Asked on <span t-field="question.write_date"/></span>
</t>
<div class="alert alert-info" t-if="question.state == 'close'">
@ -505,8 +505,8 @@
</li>
<li><a class="text-muted fa fa-share" t-attf-href="/forum/#{ slug(forum) }/question/#{ question.id }/#answer-#{ answer.id }">share</a></li>
</ul>
<t t-set="user" t-value="answer.user_id"/>
<t t-call="website_forum.user_detail">
<t t-set="user" t-value="answer.user_id"/>
<span class="text-muted">Answered on <span t-field="answer.create_date"/></span>
</t>
</div>