[MRG] added template and controller for edit and delete posts.

bzr revid: tpa@tinyerp.com-20140313090454-zso7153zubjkdwfi
This commit is contained in:
Turkesh Patel (Open ERP) 2014-03-13 14:34:54 +05:30
commit 01da080dd1
5 changed files with 118 additions and 8 deletions

View File

@ -334,3 +334,52 @@ class website_forum(http.Controller):
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)
@http.route('/forum/post_delete/', type='json', auth="user", multilang=True, methods=['POST'], website=True)
def delete_answer(self, **kwarg):
request.registry['website.forum.post'].unlink(request.cr, request.uid, [int(kwarg.get('post_id'))], context=request.context)
return True
@http.route('/forum/<model("website.forum"):forum>/delete/question/<model("website.forum.post"):post>', type='http', auth="user", multilang=True, website=True)
def delete_question(self, forum, post, **kwarg):
request.registry['website.forum.post'].unlink(request.cr, request.uid, [post.id], context=request.context)
return werkzeug.utils.redirect("/forum/%s/" % (slug(forum)))
@http.route('/forum/<model("website.forum"):forum>/edit/question/<model("website.forum.post"):post>', type='http', auth="user", multilang=True, website=True)
def edit_question(self, forum, post, **kwarg):
values = {
'post': post,
'forum': forum,
'searches': kwarg
}
return request.website.render("website_forum.edit_question", values)
@http.route('/forum/<model("website.forum"):forum>/question/savequestion/', type='http', auth="user", multilang=True, methods=['POST'], website=True)
def save_edited_question(self, forum, **post):
request.registry['website.forum.post'].write( request.cr, request.uid, [int(post.get('post_id'))], {
'content': post.get('answer_content'),
'name': post.get('question_name'),
}, context=request.context)
return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.get('post_id')))
@http.route('/forum/<model("website.forum"):forum>/answer/<model("website.forum.post"):post>/edit/<model("website.forum.post"):answer>', type='http', auth="user", multilang=True, website=True)
def edit_ans(self, forum, post, answer, **kwarg):
values = {
'post': post,
'post_answer': answer,
'forum': forum,
'searches': kwarg
}
return request.website.render("website_forum.edit_answer", values)
@http.route('/forum/correct_answer/', type='json', auth="user", multilang=True, methods=['POST'], website=True)
def correct_answer(self, **kwarg):
cr, uid, context = request.cr, request.uid, request.context
Post = request.registry['website.forum.post']
post = Post.browse(cr, uid, int(kwarg.get('post_id')), context=context)
if post.create_uid.id == uid:
correct = False if post.correct else True
Post.write( cr, uid, [int(kwarg.get('post_id'))], {
'correct': correct,
}, context=context)
return correct

View File

@ -118,7 +118,7 @@ class Post(osv.Model):
'active': fields.boolean('Active'),
'views': fields.integer('Page Views'),
'parent_id': fields.many2one('website.forum.post', 'Question'),
'parent_id': fields.many2one('website.forum.post', 'Question', ondelete='cascade'),
'child_ids': fields.one2many('website.forum.post', 'parent_id', 'Answers'),
'child_count':fields.function(_get_child_count, string="Answers", type='integer',
store={
@ -150,6 +150,7 @@ class Post(osv.Model):
'website.forum.post.vote': (_get_vote, [], 10),
}
),
'correct': fields.boolean('Correct Answer'),
}
_defaults = {
'state': 'active',
@ -173,10 +174,13 @@ class Post(osv.Model):
if context is None:
context = {}
create_context = dict(context, mail_create_nolog=True)
post_id = super(Post, self).create(cr, uid, vals, context=create_context)
body, subtype = "Asked a question", "website_forum.mt_question_create"
if vals.get("parent_id"):
body, subtype = "Answered a question", "website_forum.mt_answer_create"
#Note: because of no name it gives error on slug so set name of question in answer
question = self.browse(cr, uid, vals.get("parent_id"), context=context)
vals['name'] = question.name
post_id = super(Post, self).create(cr, uid, vals, context=create_context)
#Note: just have to pass subtype in message post: gives error on installation time
self.message_post(cr, uid, [post_id], body=_(body), context=context)
return post_id

View File

@ -36,3 +36,11 @@
.badge-bronze {
color: #eea91e;
}
.oe_answer_true {
color: #428bca;
}
.oe_answer_false {
color: #999
}

View File

@ -55,4 +55,32 @@ $(document).ready(function () {
});
return false;
});
$('.delete').on('click', function (ev) {
ev.preventDefault();
var $link = $(ev.currentTarget);
openerp.jsonRpc("/forum/post_delete/", 'call', {
'post_id': $link.attr("id")})
.then(function (data) {
$link.parents('#answer').remove();
});
return false;
});
$('.fa-check').on('click', function (ev) {
ev.preventDefault();
var $link = $(ev.currentTarget);
openerp.jsonRpc("/forum/correct_answer/", 'call', {
'post_id': $link.attr("id")})
.then(function (data) {
if (data) {
$link.removeClass("oe_answer_false").addClass('oe_answer_true');
}
else {
$link.removeClass("oe_answer_true").addClass('oe_answer_false');
}
});
return false;
});
});

View File

@ -285,6 +285,26 @@
</t>
</template>
<template id="edit_question">
<t t-call="website_forum.header">
<h3>Edit question</h3>
<form t-attf-action="/forum/#{ slug(forum) }/question/savequestion/" method="post" role="form">
<input type="text" name="question_name" required="True" t-attf-value="#{post.name or ''}"
class="form-control" placeholder="Edit your Question"/>
<h5 class="mt20">Please enter a descriptive question (should finish by a '?')</h5>
<textarea name="answer_content" required="True" value="content" class="form-control" id="textarea_ckeditor">
<t t-esc="post.content"/>
</textarea>
<input type="text" name="question_tag" class="form-control" style="margin-top: 10px" placeholder="Tags"/>
<input name="post_id" t-att-value="post.id" type="hidden"/>
<button class="btn btn-primary btn-lg">Save</button>
</form>
<script type="text/javascript">
CKEDITOR.replace("answer_content");
</script>
</t>
</template>
<template id="post_description_full" name="Question Navigation">
<t t-call="website_forum.header">
@ -324,8 +344,8 @@
comment
</a>
</li>
<li><a class="text-muted" href="">edit</a></li>
<li><a class="text-muted" href="">delete</a></li>
<li><a class="text-muted" t-attf-href="/forum/#{ slug(forum) }/edit/question/#{ question.id }">edit</a></li>
<li><a class="text-muted" t-attf-href="/forum/#{ slug(forum) }/delete/question/#{ question.id }">delete</a></li>
<li><a class="text-muted" href="">share</a></li>
</ul>
</div>
@ -341,7 +361,7 @@
</div>
<hr/>
<div t-foreach="question.child_ids" t-as="answer" class="mt16 mb32">
<div t-foreach="question.child_ids" t-as="answer" class="mt16 mb32" id="answer">
<div class="text-center pull-left">
<div t-attf-class="box oe_grey">
<a t-attf-class="fa fa-thumbs-up #{answer.user_vote == 1 and 'text-success' or ''}"
@ -354,7 +374,8 @@
</div>
</div>
<div class="text-muted">
<a href="#" class="fa fa-2x fa-check"/>
<a t-attf-id="#{answer.id}" t-if="answer.correct" class="fa fa-2x fa-check oe_answer_true"/>
<a t-attf-id="#{answer.id}" t-if="not answer.correct" class="fa fa-2x fa-check oe_answer_false"/>
</div>
</div>
<div style="margin-left: 95px;" class="clearfix">
@ -367,8 +388,8 @@
comment
</a>
</li>
<li><a class="text-muted" href="">edit</a></li>
<li><a class="text-muted" href="">delete</a></li>
<li><a class="text-muted" t-attf-href="/forum/#{ slug(forum) }/answer/#{ question.id }/edit/#{ answer.id }">edit</a></li>
<li><a class="text-muted delete" href="" t-attf-id="#{answer.id}">delete</a></li>
<li><a class="text-muted" href="">share</a></li>
</ul>
<t t-set="user" t-value="answer.create_uid"/>