[IMP] improved code of vote.

bzr revid: tpa@tinyerp.com-20140306131736-rxch9ru8d49spzsf
This commit is contained in:
Turkesh Patel (Open ERP) 2014-03-06 18:47:36 +05:30
parent 976340ca34
commit d36175eba0
3 changed files with 21 additions and 61 deletions

View File

@ -289,29 +289,17 @@ class website_forum(http.Controller):
@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)
cr, uid, context, post_id = request.cr, request.uid, request.context, int(post.get('post_id'))
Vote = request.registry['website.forum.post.vote']
vote_ids = Vote.search(cr, uid, [('post_id', '=', post_id)], context=context)
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)
if vote_ids:
Vote.unlink(cr, uid, vote_ids, context=context)
else:
Vote.create(cr, uid, {
'post_id': post_id,
'user_id': uid,
'vote': post.get('vote'),
}, context=context)
return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.get('question_id')))

View File

@ -55,7 +55,7 @@ class Post(osv.Model):
res[post.id] = -1
return res
def _get_votes_length(self, cr, uid, ids, field_name, arg, context):
def _get_vote_count(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:
@ -98,7 +98,7 @@ class Post(osv.Model):
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'),
'vote_count':fields.function(_get_vote_count, string="Number of user votes count", type='integer'),
}
_defaults = {

View File

@ -332,25 +332,11 @@
<div class="question">
<div class="text-center pull-left">
<div t-attf-class="box oe_grey">
<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>
<a t-attf-class="fa fa-thumbs-up #{question.user_vote == 1 and 'text-success' or ''}"
t-attf-href="javascript:getsupport(#{ question.id },#{ 1 });"/>
<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>
<a t-attf-class="fa fa-thumbs-down #{question.user_vote == -1 and 'text-warning' or ''}"
t-attf-href="javascript:getsupport(#{ question.id },#{ -1 });"/>
<div>
votes
</div>
@ -386,25 +372,11 @@
<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">
<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>
<a t-attf-class="fa fa-thumbs-up #{answer.user_vote == 1 and 'text-success' or ''}"
t-attf-href="javascript:getsupport(#{ answer.id },#{ 1 });"/>
<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>
<a t-attf-class="fa fa-thumbs-down #{answer.user_vote == -1 and 'text-warning' or ''}"
t-attf-href="javascript:getsupport(#{ answer.id },#{ -1 });"/>
<div>
votes
</div>