Nimesh Contractor(Open ERP) has proposed merging
lp:~openerp-dev/openobject-addons/trunk-openchatter-mail_vote-part-4-atp-vote_improvemet-nco
into lp:~openerp-dev/openobject-addons/trunk-openchatter-mail_vote-part-4-atp.
Requested reviews:
Atul Patel(OpenERP) (atp-openerp)
For more details, see:
https://code.launchpad.net/~openerp-dev/openobject-addons/trunk-openchatter-mail_vote-part-4-atp-vote_improvemet-nco/+merge/123957
Hello sir,
I have reducing the call when rendering vote.
Improve the .py and .js code.
Thanks,
NCO.
--
https://code.launchpad.net/~openerp-dev/openobject-addons/trunk-openchatter-mail_vote-part-4-atp-vote_improvemet-nco/+merge/123957
Your team OpenERP R&D Team is subscribed to branch
lp:~openerp-dev/openobject-addons/trunk-openchatter-mail_vote-part-4-atp.
=== modified file 'mail/mail_message.py'
--- mail/mail_message.py 2012-09-07 12:10:28 +0000
+++ mail/mail_message.py 2012-09-12 13:19:32 +0000
@@ -124,7 +124,7 @@
'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_user_ids': fields.many2many('mail.vote', 'message_vote_rel', 'message_id', 'vote_id', 'Votes'),
+ 'vote_user_ids': fields.many2many('res.users', 'mail_vote', 'message_id', 'user_id', 'Votes'),
}
@@ -146,21 +146,20 @@
#---------------------------------------------------
#Mail Vote system (Like or Unlike comments
#-----------------------------------------------------
- def vote_toggle(self, cr, uid, ids, context=None):
+ 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..
'''
+ if not user_ids: user_ids = [uid]
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_user_ids if x.user_id.id == uid]
+ voters_ids = [x.id for x in message.vote_user_ids if x.id == uid]
if not voters_ids:
- new_vote_id = vote_pool.create(cr, uid, {'msg_id': message.id, 'user_id': uid}, context=context)
- self.write(cr, uid, ids, {'vote_user_ids': [(4, new_vote_id)]}, context=context)
+ 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, voters_ids[0])]}, context=context)
- vote_pool.unlink(cr, uid, voters_ids, context=context)
+ self.write(cr, uid, ids, {'vote_user_ids': [(3, uid)]}, context=context)
return True
#------------------------------------------------------
@@ -170,12 +169,14 @@
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 = []
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;
+ if msg.vote_user_ids:
+ for user_id in msg.vote_user_ids:
+ if (uid == 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)
=== modified file 'mail/mail_vote.py'
--- mail/mail_vote.py 2012-08-09 09:53:41 +0000
+++ mail/mail_vote.py 2012-09-12 13:19:32 +0000
@@ -30,7 +30,7 @@
_name = 'mail.vote'
_description = 'Mail Vote'
_columns = {
- 'msg_id': fields.many2one('mail.message', 'Message', required=True),
+ 'message_id': fields.many2one('mail.message', 'Message', required=True),
'user_id': fields.many2one('res.users', 'User', required=True),
}
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
\ No newline at end of file
=== modified file 'mail/static/src/js/mail.js'
--- mail/static/src/js/mail.js 2012-09-12 05:12:31 +0000
+++ mail/static/src/js/mail.js 2012-09-12 13:19:32 +0000
@@ -272,7 +272,7 @@
});
return
},
-
+
find_parent_element: function(name, message_id){
parent_element = false;
_.each($(name), function(element){
@@ -283,27 +283,26 @@
return parent_element;
},
- render_vote: function(message_id){
+ render_vote: function(record){
+ vote_element = session.web.qweb.render('VoteDisplay', {});
var self = this;
+ var message_id = record.id
+ vote_element = session.web.qweb.render('VoteDisplay', {'message_id': message_id, 'vote_count': record.vote_user_ids.length, 'has_voted': record.has_voted});
+ parent_element = self.find_parent_element(".oe_mail_msg_vote",message_id);
+ $(parent_element).html(vote_element);
+ self.add_vote_event($(parent_element));
+ },
+ fetch_voters: function (message_id) {
+ var self= this
this.ds_message.call('message_read', [[parseInt(message_id)]]).then(function(result){
- vote_count = 0;
- _.each(result, function(res){
- if (res.vote_user_ids){
- vote_count = res.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, 'has_voted': res.has_voted});
- $(parent_element).html(vote_element);
- self.add_vote_event($(parent_element));
- });
+ self.render_vote(result[0]);
});
},
-
subscribe_vote: function(message_id){
var self = this;
this.mail_message = new session.web.DataSet(this, 'mail.message');
return this.mail_message.call('vote_toggle', [[parseInt(message_id)]]).then(function(result){
- self.render_vote(message_id);
+ self.fetch_voters(message_id);
});
},
@@ -481,8 +480,6 @@
* - 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') {
@@ -509,6 +506,9 @@
moreClass: 'oe_mail_expand',
lessClass: 'oe_mail_reduce',
});
+
+ //Render Votes.
+ this.render_vote(record);
},
/** Display 'show more' button */
=== modified file 'mail/static/src/xml/mail.xml'
--- mail/static/src/xml/mail.xml 2012-09-11 04:56:32 +0000
+++ mail/static/src/xml/mail.xml 2012-09-12 13:19:32 +0000
@@ -99,12 +99,12 @@
</t>
<li>
<t t-if="!has_voted">
- <button class="oe_mail_msg_vote_like" t-att-data-id="msg_id" title="Click to Vote.">
+ <button class="oe_mail_msg_vote_like" t-att-data-id="message_id" title="Click to Vote.">
<span>+1</span>
</button>
</t>
<t t-if="has_voted">
- <button class="oe_mail_msg_vote_like" t-att-data-id="msg_id" title="Click to Unvote." style="background:#DC5F59; padding:1px">
+ <button class="oe_mail_msg_vote_like" t-att-data-id="message_id" title="Click to Unvote." style="background:#DC5F59; padding:1px">
<span>-1</span>
</button>
</t>
_______________________________________________
Mailing list: https://launchpad.net/~openerp-dev-gtk
Post to : [email protected]
Unsubscribe : https://launchpad.net/~openerp-dev-gtk
More help : https://help.launchpad.net/ListHelp