From cd7bf7b2d248a4f744c38b03fb8d5d22318e1e84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Tue, 18 Sep 2012 14:21:39 +0200 Subject: [PATCH] [CLEAN] mail: vote backend: cleaned code. bzr revid: tde@openerp.com-20120918122139-543zxiuxytdzlqtk --- addons/mail/mail_message.py | 35 ++++++++++++++++++----------------- addons/mail/mail_thread.py | 2 +- addons/mail/mail_vote.py | 19 +++++++++++-------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/addons/mail/mail_message.py b/addons/mail/mail_message.py index 42553decb1f..4b7c0c645e3 100644 --- a/addons/mail/mail_message.py +++ b/addons/mail/mail_message.py @@ -131,7 +131,6 @@ class mail_message(osv.Model): type='boolean', string='Unread', help='Functional field to search for unread messages linked to uid'), 'vote_user_ids': fields.many2many('res.users', 'mail_vote', 'message_id', 'user_id', 'Votes'), - } def _needaction_domain_get(self, cr, uid, context=None): @@ -148,23 +147,25 @@ class mail_message(osv.Model): 'author_id': lambda self, cr, uid, ctx={}: self._get_default_author(cr, uid, ctx), 'body': '', } - - #--------------------------------------------------- - #Mail Vote system (Like or Unlike comments - #----------------------------------------------------- + + #------------------------------------------------------ + # Vote/Like + #------------------------------------------------------ + def vote_toggle(self, cr, uid, ids, user_ids=None, context=None): ''' Toggles when Comment is liked or unlike. create vote entries if current user like comment.. ''' - vote_pool = self.pool.get('mail.vote') - if not user_ids: user_ids = [uid] - for message in self.browse(cr, uid, ids, context): - voters_ids = [user.id for user in message.vote_user_ids if user.id == uid] - if not voters_ids: - self.write(cr, uid, ids, {'vote_user_ids': [(4, user_id) for user_id in user_ids]}, context=context) - else: - self.write(cr, uid, ids, {'vote_user_ids': [(3, user_id) for user_id in user_ids]}, context=context) + if not user_ids: + user_ids = [uid] + for message in self.read(cr, uid, ids, ['vote_user_ids'], context=context): + for user_id in user_ids: + has_voted = user_id in message['vote_user_ids'] + if not has_voted: + self.write(cr, uid, ids, {'vote_user_ids': [(4, user_id)]}, context=context) + else: + self.write(cr, uid, ids, {'vote_user_ids': [(3, user_id)]}, context=context) return True #------------------------------------------------------ @@ -174,12 +175,12 @@ class mail_message(osv.Model): def _message_dict_get(self, cr, uid, msg, context=None): """ Return a dict representation of the message browse record. """ vote_pool = self.pool.get('mail.vote') - has_voted = False; - vote_ids = [] - vote_ids = vote_pool.name_get(cr, uid, [user.id for user in msg.vote_user_ids], context=context) + has_voted = False + vote_ids = self.pool.get('res.users').name_get(cr, uid, [user.id for user in msg.vote_user_ids], context=context) for user_id in msg.vote_user_ids: if (user_id.id == uid): - has_voted = True; + has_voted = True + break attachment_ids = [{'id': attach[0], 'name': attach[1]} for attach in self.pool.get('ir.attachment').name_get(cr, uid, [x.id for x in msg.attachment_ids], context=context)] author_id = self.pool.get('res.partner').name_get(cr, uid, [msg.author_id.id], context=context)[0] author_user_id = self.pool.get('res.users').name_get(cr, uid, [msg.author_id.user_ids[0].id], context=context)[0] diff --git a/addons/mail/mail_thread.py b/addons/mail/mail_thread.py index d45637ff56f..abee20a35f8 100644 --- a/addons/mail/mail_thread.py +++ b/addons/mail/mail_thread.py @@ -176,7 +176,7 @@ class mail_thread(osv.AbstractModel): #------------------------------------------------------ # Automatic subscription when creating #------------------------------------------------------ - + def create(self, cr, uid, vals, context=None): """ Override to subscribe the current user. """ thread_id = super(mail_thread, self).create(cr, uid, vals, context=context) diff --git a/addons/mail/mail_vote.py b/addons/mail/mail_vote.py index 81ca4e4e75e..3d69734f198 100644 --- a/addons/mail/mail_vote.py +++ b/addons/mail/mail_vote.py @@ -20,17 +20,20 @@ ############################################################################## from osv import osv, fields -from tools.translate import _ + class mail_vote(osv.Model): - ''' - Mail vote feature allow users to like and unlike particular message or Document's message comment. - It counts the number of votes per document. - ''' + ''' Mail vote feature allow users to like and unlike messages attached + to a document. This allows for example to build a ranking-based + displaying of messages, for FAQ. ''' + _name = 'mail.vote' _description = 'Mail Vote' _columns = { - 'message_id': fields.many2one('mail.message', 'Message', required=True), - 'user_id': fields.many2one('res.users', 'User', required=True), + 'message_id': fields.many2one('mail.message', 'Message', required=True, + select=1), + 'user_id': fields.many2one('res.users', 'User', required=True, + select=1), } -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: \ No newline at end of file + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: