diff --git a/addons/mail/mail_message.py b/addons/mail/mail_message.py index 652c95d1b05..1a2cd1881ed 100644 --- a/addons/mail/mail_message.py +++ b/addons/mail/mail_message.py @@ -124,7 +124,7 @@ class mail_message(osv.Model): 'unread': fields.function(_get_unread, fnct_search=_search_unread, type='boolean', string='Unread', help='Functional field to search for unread messages linked to uid'), - 'vote_ids': fields.one2many('mail.vote', 'msg_id', 'Votes'), + 'vote_user_ids': fields.many2many('mail.vote', 'message_vote_rel', 'message_id', 'vote_id', 'Votes'), } @@ -154,13 +154,14 @@ class mail_message(osv.Model): vote_pool = self.pool.get('mail.vote') new_vote_id = False for message in self.browse(cr, uid, ids, context): - voters_ids = [x.id for x in message.vote_ids if x.user_id.id == uid] + voters_ids = [x.id for x in message.vote_user_ids if x.user_id.id == uid] if not voters_ids: new_vote_id = vote_pool.create(cr, uid, {'msg_id': message.id, 'user_id': uid}, context=context) - else: + self.write(cr, uid, ids, {'vote_user_ids': [(4, new_vote_id)]}, context=context) + else: + self.write(cr, uid, ids, {'vote_user_ids': [(3, voters_ids[0])]}, context=context) vote_pool.unlink(cr, uid, voters_ids, context=context) - return True - + return True #------------------------------------------------------ # Message loading for web interface @@ -168,7 +169,13 @@ 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') attachment_ids = self.pool.get('ir.attachment').name_get(cr, uid, [x.id for x in msg.attachment_ids], context=context) + vote_ids = vote_pool.name_get(cr, uid, [x.id for x in msg.vote_user_ids], context=context) + has_voted = False; + for vote in msg.vote_user_ids: + if (uid == vote.user_id.id): + has_voted = True; 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] partner_ids = self.pool.get('res.partner').name_get(cr, uid, [x.id for x in msg.partner_ids], context=context) @@ -186,6 +193,8 @@ class mail_message(osv.Model): 'author_user_id': author_user_id, 'partner_ids': partner_ids, 'child_ids': [], + 'vote_user_ids': vote_ids, + 'has_voted': has_voted } def message_read_tree_flatten(self, cr, uid, messages, current_level, level, context=None): diff --git a/addons/mail/static/src/js/mail.js b/addons/mail/static/src/js/mail.js index e902451ee1b..026808f8905 100644 --- a/addons/mail/static/src/js/mail.js +++ b/addons/mail/static/src/js/mail.js @@ -289,19 +289,16 @@ openerp.mail = function(session) { render_vote: function(message_id){ var self = this; - var mail_vote = new session.web.DataSetSearch(self, 'mail.vote', self.session.context, [['msg_id','=',parseInt(message_id)]]); - mail_vote.read_slice(['user_id']).then(function(result){ - vote_count = result.length; - is_vote_liked = false; - _.each(result, function(vote){ - if (self.session.uid == vote.user_id[0]){ - is_vote_liked = true; - } - }); + this.ds_message.call('message_read', [[parseInt(message_id)]]).then(function(result){ + vote_count = 0; + if (result[0].vote_user_ids){length + vote_count = result[0].vote_user_ids.length; + } parent_element = self.find_parent_element(".oe_mail_msg_vote", message_id); - vote_element = session.web.qweb.render('VoteDisplay', {'msg_id': message_id, 'vote_count': vote_count, 'is_vote_liked': is_vote_liked}); + vote_element = session.web.qweb.render('VoteDisplay', {'msg_id': message_id, 'vote_count': vote_count, 'has_voted': result[0].has_voted}); $(parent_element).html(vote_element); self.add_vote_event($(parent_element)); + }); }, @@ -446,8 +443,6 @@ openerp.mail = function(session) { var self = this; var _expendable = false; _(records).each(function (record) { - //Render Votes. - self.render_vote(record.id); if (record.type == 'expandable') { _expendable = true; self.update_fetch_more(true); @@ -484,6 +479,8 @@ openerp.mail = function(session) { * - record.is_author: is the current user the author of the record */ display_record: function (record) { // formatting and additional fields + //Render Votes. + this.render_vote(record.id); record.date = session.web.format_value(record.date, {type:"datetime"}); record.timerelative = $.timeago(record.date); if (record.type == 'email') { diff --git a/addons/mail/static/src/xml/mail.xml b/addons/mail/static/src/xml/mail.xml index 2cab9825946..3b3a5d2de9e 100644 --- a/addons/mail/static/src/xml/mail.xml +++ b/addons/mail/static/src/xml/mail.xml @@ -98,12 +98,12 @@ Votes :
  • - + - +