[MERGE]: Merged lp:~openerp-dev/openobject-addons/trunk-openchatter-mail_vote-part-4-atp-vote_improvemet-nco with some improvement..
bzr revid: atp@tinyerp.com-20120912165720-kqkochsbub3ootkz
This commit is contained in:
+12
-13
@@ -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_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,19 @@ class mail_message(osv.Model):
|
||||
#---------------------------------------------------
|
||||
#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..
|
||||
'''
|
||||
vote_pool = self.pool.get('mail.vote')
|
||||
new_vote_id = False
|
||||
if not user_ids: user_ids = [uid]
|
||||
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 = [user.id for user in message.vote_user_ids if user.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, user_id) for user_id in user_ids]}, context=context)
|
||||
return True
|
||||
|
||||
#------------------------------------------------------
|
||||
@@ -170,12 +168,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;
|
||||
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, [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;
|
||||
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)
|
||||
|
||||
@@ -30,7 +30,7 @@ class mail_vote(osv.Model):
|
||||
_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:
|
||||
@@ -265,17 +265,36 @@ openerp.mail = function(session) {
|
||||
//Mail vote Functionality...
|
||||
add_vote_event: function(element){
|
||||
self = this;
|
||||
vote_img = element.find('.oe_mail_msg_vote_like');
|
||||
if (vote_img)
|
||||
vote_img.click(function(){
|
||||
vote_button = element.find('.oe_mail_msg_vote_like');
|
||||
if (vote_button)
|
||||
vote_button.click(function(){
|
||||
self.subscribe_vote($(this).attr('data-id'));
|
||||
});
|
||||
return
|
||||
},
|
||||
|
||||
// Fetch Records using updated message_id
|
||||
|
||||
find_parent_element: function(name, message_id){
|
||||
fetch_voters: function (message_id) {
|
||||
var self= this
|
||||
this.ds_message.call('message_read', [[parseInt(message_id)]]).then(function(results){
|
||||
_.each(results, function(result){
|
||||
self.render_vote(result);
|
||||
});
|
||||
});
|
||||
},
|
||||
// Like and unlike..
|
||||
subscribe_vote: function(message_id){
|
||||
var self = this;
|
||||
return this.ds_message.call('vote_toggle', [[parseInt(message_id)]]).then(function(result){
|
||||
self.fetch_voters(message_id);
|
||||
});
|
||||
},
|
||||
// Find parent element using current message id.
|
||||
|
||||
find_parent_element: function(elements, message_id){
|
||||
parent_element = false;
|
||||
_.each($(name), function(element){
|
||||
_.each($(elements), function(element){
|
||||
if ($(element).attr("data-id") == message_id){
|
||||
parent_element = element;
|
||||
}
|
||||
@@ -283,28 +302,14 @@ openerp.mail = function(session) {
|
||||
return parent_element;
|
||||
},
|
||||
|
||||
render_vote: 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));
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
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);
|
||||
});
|
||||
// Render vote Display template.
|
||||
render_vote: function(record){
|
||||
var self = this;
|
||||
vote_element = session.web.qweb.render('VoteDisplay', {});
|
||||
// Get all element from record..
|
||||
vote_element = session.web.qweb.render('VoteDisplay', {'message_id': record.id, 'vote_count': record.vote_user_ids.length, 'has_voted': record.has_voted});
|
||||
parent_element = self.find_parent_element(".oe_mail_msg_vote", record.id);
|
||||
$(parent_element).html(vote_element).appendTo(this.add_vote_event($(parent_element)));
|
||||
},
|
||||
|
||||
/** Customize the display
|
||||
@@ -481,8 +486,6 @@ 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') {
|
||||
@@ -509,6 +512,9 @@ openerp.mail = function(session) {
|
||||
moreClass: 'oe_mail_expand',
|
||||
lessClass: 'oe_mail_reduce',
|
||||
});
|
||||
|
||||
//Render Votes.
|
||||
this.render_vote(record);
|
||||
},
|
||||
|
||||
/** Display 'show more' button */
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user