[imp]:website_form:improve user vote for post

bzr revid: sunilsharma.sharma07@gmail.com-20140306094042-sfpi19f41o3u8k95
This commit is contained in:
Sunil Sharma (OpenERP)
2014-03-06 15:10:42 +05:30
parent 4d35b34268
commit f02c071457
3 changed files with 119 additions and 9 deletions
+29
View File
@@ -264,3 +264,32 @@ class website_forum(http.Controller):
}
return request.website.render("website_forum.users", values)
@http.route('/forum/<model("website.forum"):forum>/post_vote/', type='http', auth="user", multilang=True, methods=['POST'], website=True)
def post_vote(self, forum, **post):
cr, uid, context = request.cr, request.uid, request.context
vote_obj = request.registry['website.forum.post.vote']
post_id = post.get('post_id');
post_ids = vote_obj.search(cr, uid, [('post_id', '=', int(post_id))],context=None)
if (not post_ids) and '1' == post.get('vote'):
up_post_vote_id = vote_obj.create(
cr, uid, {
'post_id': post_id,
'user_id': uid,
'vote': post.get('vote'),
}, context=context)
elif post_ids and '1' == post.get('vote'):
vote_obj.unlink(cr, uid, post_ids, context)
if (not post_ids) and '-1' == post.get('vote'):
down_post_vote_id = vote_obj.create(
cr, uid, {
'post_id': post_id,
'user_id': uid,
'vote': post.get('vote'),
}, context=context)
elif post_ids and '-1' == post.get('vote'):
vote_obj.unlink(cr, uid, post_ids, context)
return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.get('question_id')))
+29 -3
View File
@@ -42,6 +42,29 @@ class Post(osv.Model):
_name = 'website.forum.post'
_description = "Question"
_inherit = ['mail.thread', 'website.seo.metadata']
def _get_votes(self, cr, uid, ids, field_name, arg, context):
res = dict.fromkeys(ids, False)
for post in self.browse(cr, uid, ids, context=context):
if post.vote_ids:
for vote in post.vote_ids:
if vote.user_id.id == uid:
if vote.vote == '1':
res[post.id] = 1
else:
res[post.id] = -1
return res
def _get_votes_length(self, cr, uid, ids, field_name, arg, context):
res = dict.fromkeys(ids, 0)
for post in self.browse(cr, uid, ids, context=context):
if post.vote_ids:
for vote in post.vote_ids:
if vote.vote == '1':
res[post.id] += 1
else:
res[post.id] -= 1
return res
_columns = {
'forum_id': fields.many2one('website.forum', 'Forum', required=True),
@@ -73,6 +96,9 @@ class Post(osv.Model):
string='Post Messages',
help="Comments on forum post",
),
'user_vote':fields.function(_get_votes, string="Number of user votes", type='boolean'),
'vote_count':fields.function(_get_votes_length, string="Number of user votes count", type='integer'),
}
_defaults = {
'state': 'active',
@@ -122,7 +148,7 @@ class Post(osv.Model):
if method == 'unlink':
res.update({'type': 'deleted_question'})
if method == 'vote':
res.update({'type': 'voteted_question'})
res.update({'type': 'voted_question'})
Activity.create(cr, uid, res, context=context)
@@ -188,12 +214,12 @@ class Vote(osv.Model):
_columns = {
'post_id': fields.many2one('website.forum.post', 'Post', required=True),
'user_id': fields.many2one('res.users', 'User'),
'vote': fields.integer('rate'),
'vote': fields.selection([('1', '1'),('-1', '-1')], 'rate'),
}
def create(self, cr, uid, vals, context=None):
vote_id = super(Vote, self).create(cr, uid, vals, context=context)
self.pool['website.forum.post'].create_activity(cr, uid, [vals.get('post_id')], method='vote', context=context)
self.pool['website.forum.post'].create_activity(cr, uid, [int(vals.get('post_id'))], method='vote', context=context)
return vote_id
class ForumActivity(osv.Model):
+61 -6
View File
@@ -306,12 +306,51 @@
<template id="post_description_full" name="Question Navigation">
<t t-call="website_forum.header">
<form t-attf-action="/forum/#{ slug(forum) }/post_vote/" enctype="multipart/form-data" method="post" id="post_vote" role="form">
<input name="question_id" t-att-value="question.id" type="hidden"/>
<script type="text/javascript">
{
function getsupport (post_value, up_down)
{
var f=document.getElementById("post_vote")
var input = document.createElement("input");
input.name= "post_id";
input.type = "hidden";
input.value = post_value;
var vote = document.createElement("input");
vote.name= "vote";
vote.type = "hidden";
vote.value = up_down;
f.appendChild(input);
f.appendChild(vote);
document.getElementById('post_vote').submit();
}
}
</script>
</form>
<div class="question">
<div class="text-center pull-left">
<div t-attf-class="box oe_grey">
<a href="#" class="fa fa-thumbs-up text-success"/>
<span t-esc="len(question.vote_ids)"/>
<a href="#" class="fa fa-thumbs-down text-warning"/>
<t t-if="question.user_vote == 0">
<a class="fa fa-thumbs-up" t-attf-href="javascript:getsupport(#{ question.id },#{ 1 });"/>
</t>
<t t-if="question.user_vote == -1">
<a class="fa fa-thumbs-up" t-attf-href="javascript:getsupport(#{ question.id },#{ 1 });"/>
</t>
<t t-if="question.user_vote == 1">
<a class="fa fa-thumbs-up text-success" t-attf-href="javascript:getsupport(#{ question.id },#{ 1 });"/>
</t>
<span t-esc="question.vote_count"/>
<t t-if="question.user_vote == 0">
<a class="fa fa-thumbs-down" t-attf-href="javascript:getsupport(#{ question.id },#{ -1 });"/>
</t>
<t t-if="question.user_vote == -1">
<a class="fa fa-thumbs-down text-warning" t-attf-href="javascript:getsupport(#{ question.id },#{ -1 });"/>
</t>
<t t-if="question.user_vote == 1">
<a class="fa fa-thumbs-down" t-attf-href="javascript:getsupport(#{ question.id },#{ -1 });"/>
</t>
<div>
votes
</div>
@@ -344,9 +383,25 @@
<div t-foreach="question.child_ids" t-as="answer" class="mt16 mb32">
<div class="text-center pull-left">
<div t-attf-class="box oe_grey">
<a href="#" class="fa fa-thumbs-up text-success"/>
<span t-esc="len(question.vote_ids)"/>
<a href="#" class="fa fa-thumbs-down text-warning"/>
<t t-if="answer.user_vote == 0">
<a class="fa fa-thumbs-up" t-attf-href="javascript:getsupport(#{ answer.id },#{ 1 });"/>
</t>
<t t-if="answer.user_vote == -1">
<a class="fa fa-thumbs-up" t-attf-href="javascript:getsupport(#{ answer.id },#{ 1 });"/>
</t>
<t t-if="answer.user_vote == 1">
<a class="fa fa-thumbs-up text-success" t-attf-href="javascript:getsupport(#{ answer.id },#{ 1 });"/>
</t>
<span t-esc="answer.vote_count"/>
<t t-if="answer.user_vote == -1">
<a class="fa fa-thumbs-down text-warning" t-attf-href="javascript:getsupport(#{ answer.id },#{ -1 });"/>
</t>
<t t-if="answer.user_vote == 0">
<a class="fa fa-thumbs-down" t-attf-href="javascript:getsupport(#{ answer.id },#{ -1 });"/>
</t>
<t t-if="answer.user_vote == 1">
<a class="fa fa-thumbs-down" t-attf-href="javascript:getsupport(#{ answer.id },#{ -1 });"/>
</t>
<div>
votes
</div>