[IMP] Improved code

1) Edit Subscriptions of -> Edit Subscription (without 's') of
2) Instead of defining a new 'apply_edited_subtype', you can use 'message_subscribe' that has an optional 'subtype_ids' parameter that already allows to manage subtypes of a subscription.
3) In _get_subscription_data: do not use context when it is not necessary. Please add a parameter user_id=None that will be used instead of uid to find user_pid.
4) Instead of defining a new 'on_apply_subtype', can't you reuse 'do_update_subscription' with maybe some update to handle the classic use / edit widget ?
The above changes has been done.

bzr revid: psa@tinyerp.com-20130404070723-5v1v35fte6vl2b2j
This commit is contained in:
Paramjit Singh Sahota 2013-04-04 12:37:23 +05:30
parent 06986e4737
commit b32cc3b442
2 changed files with 32 additions and 54 deletions

View File

@ -119,12 +119,10 @@ class mail_thread(osv.AbstractModel):
return True
return False
def _get_subscription_data(self, cr, uid, ids, name, args, context=None):
def _get_subscription_data(self, cr, uid, ids, user_id, name, args, context=None):
""" Computes:
- message_subtype_data: data about document subtypes: which are
available, which are followed if any """
if context is None:
context = {}
res = dict((id, dict(message_subtype_data='')) for id in ids)
user_pid = self.pool.get('res.users').read(cr, uid, uid, ['partner_id'], context=context)['partner_id'][0]
@ -135,8 +133,8 @@ class mail_thread(osv.AbstractModel):
for id in ids:
res[id]['message_subtype_data'] = subtype_dict.copy()
if 'partner_id' in context:
user_pid = context.get('partner_id')
if user_id:
user_pid = user_id
# find the document followers, update the data
fol_obj = self.pool.get('mail.followers')
fol_ids = fol_obj.search(cr, uid, [
@ -301,17 +299,6 @@ class mail_thread(osv.AbstractModel):
default['message_follower_ids'] = []
return super(mail_thread, self).copy(cr, uid, id, default=default, context=context)
def apply_edited_subtypes(self, cr, uid, ids, partner_id, check_list, context=None):
""" Apply the edited subtypes of the user."""
fol_obj = self.pool.get('mail.followers')
fol_ids = fol_obj.search(cr, uid, [
('partner_id', '=', partner_id),
('res_id', 'in', ids),
('res_model', '=', self._name),
], context=context)
fol_obj.write(cr, uid, fol_ids, {'subtype_ids': [(6,0, check_list)]}, context=context)
return True
#------------------------------------------------------
# Automatically log tracked fields
#------------------------------------------------------
@ -1102,21 +1089,23 @@ class mail_thread(osv.AbstractModel):
# Followers API
#------------------------------------------------------
def message_get_subscription_data(self, cr, uid, ids, context=None):
def message_get_subscription_data(self, cr, uid, ids, user_id=None, context=None):
""" Wrapper to get subtypes data. """
return self._get_subscription_data(cr, uid, ids, None, None, context=context)
return self._get_subscription_data(cr, uid, ids, user_id, None, None, context=context)
def message_subscribe_users(self, cr, uid, ids, user_ids=None, subtype_ids=None, context=None):
def message_subscribe_users(self, cr, uid, ids, user_ids=None, partner_id=None, subtype_ids=None, context=None):
""" Wrapper on message_subscribe, using users. If user_ids is not
provided, subscribe uid instead. """
if user_ids is None:
user_ids = [uid]
partner_ids = [user.partner_id.id for user in self.pool.get('res.users').browse(cr, uid, user_ids, context=context)]
return self.message_subscribe(cr, uid, ids, partner_ids, subtype_ids=subtype_ids, context=context)
return self.message_subscribe(cr, uid, ids, partner_ids, partner_id, subtype_ids=subtype_ids, context=context)
def message_subscribe(self, cr, uid, ids, partner_ids, subtype_ids=None, context=None):
def message_subscribe(self, cr, uid, ids, partner_ids, user_id, subtype_ids=None, context=None):
""" Add partners to the records followers. """
user_pid = self.pool.get('res.users').read(cr, uid, uid, ['partner_id'], context=context)['partner_id'][0]
if user_id:
partner_ids = [user_id]
if set(partner_ids) == set([user_pid]):
self.check_access_rights(cr, uid, 'read')
else:

View File

@ -82,38 +82,20 @@ openerp_mail_followers = function(session, mail) {
on_edit_subtype: function(event) {
var self = this;
var partner_id = $(event.target).data('id');
var context = new session.web.CompoundContext(this.build_context(), {'partner_id': partner_id});
self.$dialog = new session.web.dialog($('<div class = "oe_edit_actions">'), {
self.$dialog = new session.web.dialog($('<div class="oe_edit_actions">'), {
modal: true,
width: 'auto',
height: 'auto',
title: _t('Edit Subscriptions of ') + $(event.target).siblings('a').text(),
title: _t('Edit Subscription of ') + $(event.target).siblings('a').text(),
buttons: [
{ text: _t("Apply"), click: function() {
self.on_apply_subtype(partner_id);
self.do_update_subscription(event, partner_id);
$(this).remove();
}},
{ text: _t("Cancel"), click: function() { $(this).remove(); }}
],
});
return self.fetch_subtypes(context);
},
on_apply_subtype: function(partner_id) {
var check_list = new Array();
var id = this.view.datarecord.id;
_($('.oe_edit_actions input[type="checkbox"]')).each(function (record) {
if ($(record).is(':checked')) {
check_list.push(parseInt($(record).data('id')));
}
});
if (!check_list.length) {
this.do_unfollow();
} else {
return this.ds_model.call('apply_edited_subtypes', [[id], partner_id, check_list, new session.web.CompoundContext(this.build_context(), {})])
.then(this.proxy('read_value'));
}
return self.fetch_subtypes(partner_id);
},
on_invite_follower: function (event) {
@ -251,17 +233,17 @@ openerp_mail_followers = function(session, mail) {
},
/** Fetch subtypes, only if current user is follower */
fetch_subtypes: function (context) {
fetch_subtypes: function (partner_id) {
var self = this;
var mode = "";
if (context && context.eval('partner_id')) {
if (partner_id) {
mode = "edit_follower";
} else {
var subtype_list_ul = this.$('.oe_subtype_list').empty();
if (! this.message_is_follower) return;
}
var id = this.view.datarecord.id;
this.ds_model.call('message_get_subscription_data', [[id],context])
this.ds_model.call('message_get_subscription_data', [[id], partner_id, new session.web.CompoundContext(this.build_context(), {})])
.then(function (data) {self.display_subtypes(data, id, mode);});
},
@ -307,23 +289,30 @@ openerp_mail_followers = function(session, mail) {
return false;
},
do_update_subscription: function (event) {
do_update_subscription: function (event, partner_id) {
var self = this;
var checklist = new Array();
_(this.$('.oe_actions input[type="checkbox"]')).each(function (record) {
if ($(record).is(':checked')) {
checklist.push(parseInt($(record).data('id')));
}
});
if (partner_id) {
_($('.oe_edit_actions input[type="checkbox"]')).each(function (record) {
if ($(record).is(':checked')) {
checklist.push(parseInt($(record).data('id')));
}
});
} else {
_(this.$('.oe_actions input[type="checkbox"]')).each(function (record) {
if ($(record).is(':checked')) {
checklist.push(parseInt($(record).data('id')));
}
});
}
if (!checklist.length) {
if (!this.do_unfollow()) {
$(event.target).attr("checked", "checked");
}
} else {
var context = new session.web.CompoundContext(this.build_context(), {});
return this.ds_model.call('message_subscribe_users', [[this.view.datarecord.id], [this.session.uid], checklist, context])
return this.ds_model.call('message_subscribe_users', [[this.view.datarecord.id], [this.session.uid], partner_id, checklist, context])
.then(this.proxy('read_value'));
}
},