[IMP] added subtypes and improved code for post vote.

bzr revid: tpa@tinyerp.com-20140313084130-8kdlra4dhwzl016o
This commit is contained in:
Turkesh Patel (Open ERP) 2014-03-13 14:11:30 +05:30
parent c03ffffa8a
commit 0c8d2af2fa
3 changed files with 56 additions and 27 deletions

View File

@ -157,6 +157,7 @@ class website_forum(http.Controller):
Post = request.registry['website.forum.post'] Post = request.registry['website.forum.post']
Vote = request.registry['website.forum.post.vote'] Vote = request.registry['website.forum.post.vote']
Activity = request.registry['mail.message'] Activity = request.registry['mail.message']
Data = request.registry["ir.model.data"]
question_ids = Post.search(cr, uid, [('forum_id', '=', forum.id), ('create_uid', '=', user.id), ('parent_id', '=', False)], context=context) question_ids = Post.search(cr, uid, [('forum_id', '=', forum.id), ('create_uid', '=', user.id), ('parent_id', '=', False)], context=context)
user_questions = Post.browse(cr, uid, question_ids, context=context) user_questions = Post.browse(cr, uid, question_ids, context=context)
@ -170,8 +171,9 @@ class website_forum(http.Controller):
up_votes = Vote.search(cr, uid, [('post_id.forum_id', '=', forum.id), ('post_id.create_uid', '=', user.id), ('vote', '=', '1')], count=True, context=context) up_votes = Vote.search(cr, uid, [('post_id.forum_id', '=', forum.id), ('post_id.create_uid', '=', user.id), ('vote', '=', '1')], count=True, context=context)
down_votes = Vote.search(cr, uid, [('post_id.forum_id', '=', forum.id), ('post_id.create_uid', '=', user.id), ('vote', '=', '-1')], count=True, context=context) down_votes = Vote.search(cr, uid, [('post_id.forum_id', '=', forum.id), ('post_id.create_uid', '=', user.id), ('vote', '=', '-1')], count=True, context=context)
user_post_ids = user_questions + obj_ids user_post_ids = question_ids + obj_ids
activity_ids = Activity.search(cr, uid, [('res_id', 'in', user_post_ids), ('model', '=', 'website.forum.post')], context=context) model, comment = Data.get_object_reference(cr, uid, 'mail', 'mt_comment')
activity_ids = Activity.search(cr, uid, [('res_id', 'in', user_post_ids), ('model', '=', 'website.forum.post'), '|', ('subtype_id', '!=', comment), ('subtype_id', '=', False)], context=context)
activities = Activity.browse(cr, uid, activity_ids, context=context) activities = Activity.browse(cr, uid, activity_ids, context=context)
posts = {} posts = {}
for rec in user_answers + user_questions: for rec in user_answers + user_questions:

View File

@ -23,5 +23,34 @@
<field name="state">open</field> <field name="state">open</field>
</record--> </record-->
<!-- related subtypes -->
<record id="mt_question_create" model="mail.message.subtype">
<field name="name">Asked a question</field>
<field name="res_model">website.forum.post</field>
<field name="default" eval="False"/>
<field name="description">Asked a question</field>
</record>
<record id="mt_answer_create" model="mail.message.subtype">
<field name="name">Answered a question</field>
<field name="res_model">website.forum.post</field>
<field name="default" eval="False"/>
<field name="description">Answered a question</field>
</record>
<record id="mt_question_edit" model="mail.message.subtype">
<field name="name">Question edited</field>
<field name="res_model">website.forum.post</field>
<field name="default" eval="False"/>
<field name="description">Question edited</field>
</record>
<record id="mt_answer_edit" model="mail.message.subtype">
<field name="name">Answer edited</field>
<field name="res_model">website.forum.post</field>
<field name="default" eval="False"/>
<field name="description">Answer edited</field>
</record>
</data> </data>
</openerp> </openerp>

View File

@ -81,10 +81,7 @@ class Post(osv.Model):
for post in self.browse(cr, uid, ids, context=context): for post in self.browse(cr, uid, ids, context=context):
if post.vote_ids: if post.vote_ids:
for vote in post.vote_ids: for vote in post.vote_ids:
if vote.vote == '1': res[post.id] += int(vote.vote)
res[post.id] += 1
else:
res[post.id] -= 1
return res return res
def _get_vote(self, cr, uid, ids, context=None): def _get_vote(self, cr, uid, ids, context=None):
@ -177,21 +174,23 @@ class Post(osv.Model):
context = {} context = {}
create_context = dict(context, mail_create_nolog=True) create_context = dict(context, mail_create_nolog=True)
post_id = super(Post, self).create(cr, uid, vals, context=create_context) post_id = super(Post, self).create(cr, uid, vals, context=create_context)
body = "asked a question" body, subtype = "Asked a question", "website_forum.mt_question_create"
if vals.get("parent_id"): if vals.get("parent_id"):
body = "answered a question" body, subtype = "Answered a question", "website_forum.mt_answer_create"
self.message_post(cr, uid, [post_id], body=body, context=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 return post_id
def write(self, cr, uid, ids, vals, context=None): def write(self, cr, uid, ids, vals, context=None):
self.create_history(cr, uid, ids, vals, context=context) self.create_history(cr, uid, ids, vals, context=context)
result = super(Post, self).write(cr, uid, ids, vals, context=context) #NOTE: to avoid message post on write of last comment time
for post in self.browse(cr, uid, ids, context=context): if not vals.get('message_last_post'):
body = "edited question" for post in self.browse(cr, uid, ids, context=context):
if post.parent_id: body, subtype = "Edited question", "website_forum.mt_question_edit"
body = "edited answer" if post.parent_id:
self.message_post(cr, uid, ids, body=body, context=context) body, subtype = "Edited answer", "website_forum.mt_answer_edit"
return result self.message_post(cr, uid, [post.id], body=_(body), subtype=subtype, context=context)
return super(Post, self).write(cr, uid, ids, vals, context=context)
class Users(osv.Model): class Users(osv.Model):
_inherit = 'res.users' _inherit = 'res.users'
@ -248,33 +247,32 @@ class Vote(osv.Model):
'user_id': lambda self, cr, uid, ctx: uid, 'user_id': lambda self, cr, uid, ctx: uid,
'vote': lambda *args: 1 'vote': lambda *args: 1
} }
# TODO: improve this: translate strings _()
# no need to have different text for question/answer
# need different text for upvote, downvote
def create(self, cr, uid, vals, context=None): def create(self, cr, uid, vals, context=None):
vote_id = super(Vote, self).create(cr, uid, vals, context=context) vote_id = super(Vote, self).create(cr, uid, vals, context=context)
Post = self.pool["website.forum.post"] Post = self.pool["website.forum.post"]
record = Post.browse(cr, uid, vals.get('post_id'), context=context) record = Post.browse(cr, uid, vals.get('post_id'), context=context)
body = "voted question" body = "voted %s %s" % ('answer' if record.parent_id else 'question','up' if vals.get('vote')==1 else 'down')
if record.parent_id: Post.message_post(cr, uid, [record.id], body=_(body), context=context)
body = "voted answer"
Post.message_post(cr, uid, [record.id], body=body, context=context)
return vote_id return vote_id
def vote(self, cr, uid, post_id, vote, context=None): def vote(self, cr, uid, post_id, vote, context=None):
assert int(vote) in (1, -1, 0), "vote can be -1 or 1, nothing else" assert int(vote) in (1, -1, 0), "vote can be -1 or 1, nothing else"
post_obj = self.pool.get('website.forum.post') Post = self.pool.get('website.forum.post')
vote_ids = self.search(cr, uid, [('post_id', '=', post_id), ('user_id','=',uid)], context=context) vote_ids = self.search(cr, uid, [('post_id', '=', post_id), ('user_id','=',uid)], context=context)
if vote_ids: if vote_ids:
self.write(cr, uid, vote_uid, { #when user will click again on vote it should set it 0.
'vote': vote record = self.browse(cr,uid, vote_ids[0], context=context)
new_vote = '0' if record.vote in ['1','-1'] else vote
self.write(cr, uid, vote_ids, {
'vote': new_vote
}, context=context) }, context=context)
else: else:
self.create(cr, uid, { self.create(cr, uid, {
'post_id': post_id, 'post_id': post_id,
'vote': vote, 'vote': vote,
}, context=context) }, context=context)
return post_obj.browse(cr, uid, post_id, context=context).vote_count return Post.browse(cr, uid, post_id, context=context).vote_count
class Badge(osv.Model): class Badge(osv.Model):
_inherit = 'gamification.badge' _inherit = 'gamification.badge'