diff --git a/addons/analytic_contract_crm/analytic_contract_crm.py b/addons/analytic_contract_crm/analytic_contract_crm.py index a69849faf36..07257dc9505 100644 --- a/addons/analytic_contract_crm/analytic_contract_crm.py +++ b/addons/analytic_contract_crm/analytic_contract_crm.py @@ -27,8 +27,11 @@ class account_analytic_account(osv.osv): def create(self, cr, uid, vals, context=None): obj_id = super(account_analytic_account, self).create(cr, uid, vals, context=context) - # subscribe salesteam followers & subtypes to the contract - self._subscribe_salesteam_followers_to_contract(cr, uid, [obj_id], context) + manager_id = self.browse(cr, uid, obj_id, context=context).manager_id + if manager_id: + if manager_id.default_section_id: + # subscribe salesteam followers & subtypes to the contract + self._subscribe_followers_subtype(cr, uid, [obj_id], manager_id.default_section_id, 'crm.case.section', context=context) if obj_id: self.create_send_note(cr, uid, [obj_id], context=context) return obj_id @@ -39,34 +42,11 @@ class account_analytic_account(osv.osv): res = super(account_analytic_account, self).write(cr, uid, ids, vals, context=context) # subscribe new salesteam followers & subtypes to the contract if vals.get('manager_id'): - self._subscribe_salesteam_followers_to_contract(cr, uid, ids, context) + section_id = self.pool.get('res.users').browse(cr, uid, vals.get('manager_id'), context=context).default_section_id + if section_id: + self._subscribe_followers_subtype(cr, uid, ids, section_id, 'crm.case.section', context=context) return res - def _subscribe_salesteam_followers_to_contract(self, cr, uid, obj_id, context=None): - follower_obj = self.pool.get('mail.followers') - subtype_obj = self.pool.get('mail.message.subtype') - record = self.browse(cr, uid, obj_id, context=context) - manager_id = record and record[0].manager_id or False - if manager_id: - if manager_id.default_section_id: - # fetch subscribers - followers = [follow.id for follow in manager_id.default_section_id.message_follower_ids] - contract_subtype_ids = subtype_obj.search(cr, uid, ['|', ('res_model', '=', False), ('res_model', '=', self._name)], context=context) - contract_subtypes = subtype_obj.browse(cr, uid, contract_subtype_ids, context=context) - # fetch subscriptions - follower_ids = follower_obj.search(cr, uid, [('res_model', '=', 'crm.case.section'), ('res_id', '=', manager_id.default_section_id.id)], context=context) - # when subscribe new salesteam update followers - self.write(cr, uid, obj_id, {'message_follower_ids': [(6, 0, followers)]}, context=context) - # copy followers & select subtypes - for follower in follower_obj.browse(cr, uid, follower_ids, context=context): - if not follower.subtype_ids: - continue - salesteam_subtype_names = [salesteam_subtype.name for salesteam_subtype in follower.subtype_ids] - contract_subtype_ids = [contract_subtype.id for contract_subtype in contract_subtypes if contract_subtype.name in salesteam_subtype_names] - self.message_subscribe(cr, uid, obj_id, [follower.partner_id.id], subtype_ids=contract_subtype_ids, context=context) - else: - return - account_analytic_account() # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/crm/crm_lead.py b/addons/crm/crm_lead.py index 7e48a4eb7ee..fbdc76f9ba6 100644 --- a/addons/crm/crm_lead.py +++ b/addons/crm/crm_lead.py @@ -305,34 +305,13 @@ class crm_lead(base_stage, format_address, osv.osv): def create(self, cr, uid, vals, context=None): obj_id = super(crm_lead, self).create(cr, uid, vals, context) - # subscribe salesteam followers & subtypes to the lead - self._subscribe_salesteam_followers_to_lead(cr, uid, [obj_id], context=context) + section_id = self.browse(cr, uid, obj_id, context=context).section_id + if section_id: + # subscribe salesteam followers & subtypes to the lead + self._subscribe_followers_subtype(cr, uid, [obj_id], section_id, 'crm.case.section', context=context) self.create_send_note(cr, uid, [obj_id], context=context) return obj_id - def _subscribe_salesteam_followers_to_lead(self, cr, uid, obj_id, context=None): - follower_obj = self.pool.get('mail.followers') - subtype_obj = self.pool.get('mail.message.subtype') - rec = self.browse(cr, uid, obj_id, context=context) - section_id = rec and rec[0].section_id or False - if section_id: - # fetch subscribers - followers = [follow.id for follow in section_id.message_follower_ids] - # fetch subtypes - lead_subtype_ids = subtype_obj.search(cr, uid, ['|', ('res_model', '=', False), ('res_model', '=', self._name)], context=context) - lead_subtypes = subtype_obj.browse(cr, uid, lead_subtype_ids, context=context) - # fetch subscriptions - follower_ids = follower_obj.search(cr, uid, [('res_model', '=', 'crm.case.section'), ('res_id', '=', section_id)], context=context) - # when subscribe new salesteam update followers - self.write(cr, uid, obj_id, {'message_follower_ids': [(6, 0, followers)]}, context=context) - # copy followers & select subtypes - for follower in follower_obj.browse(cr, uid, follower_ids, context=context): - if not follower.subtype_ids: - continue - salesteam_subtype_names = [salesteam_subtype.name for salesteam_subtype in follower.subtype_ids] - lead_subtype_ids = [lead_subtype.id for lead_subtype in lead_subtypes if lead_subtype.name in salesteam_subtype_names] - self.message_subscribe(cr, uid, obj_id, [follower.partner_id.id], subtype_ids=lead_subtype_ids, context=context) - def onchange_stage_id(self, cr, uid, ids, stage_id, context=None): if not stage_id: return {'value':{}} @@ -934,10 +913,15 @@ class crm_lead(base_stage, format_address, osv.osv): stage = self.pool.get('crm.case.stage').browse(cr, uid, vals['stage_id'], context=context) if stage.on_change: vals['probability'] = stage.probability + if vals.get('section_id'): + section_id = self.pool.get('crm.case.section').browse(cr, uid, vals.get('section_id'), context=context) + if section_id: + vals.setdefault('message_follower_ids', []) + vals['message_follower_ids'] += [(6, 0,[follower.id]) for follower in section_id.message_follower_ids] res = super(crm_lead,self).write(cr, uid, ids, vals, context) # subscribe new salesteam followers & subtypes to the lead if vals.get('section_id'): - self._subscribe_salesteam_followers_to_lead(cr, uid, ids, context=context) + self._subscribe_followers_subtype(cr, uid, ids, vals.get('section_id'), 'crm.case.section', context=context) return res # ---------------------------------------- diff --git a/addons/mail/mail_thread.py b/addons/mail/mail_thread.py index 63043f992f3..e581d6fa75f 100644 --- a/addons/mail/mail_thread.py +++ b/addons/mail/mail_thread.py @@ -847,4 +847,25 @@ class mail_thread(osv.AbstractModel): ''', (ids, self._name, partner_id)) return True + def _subscribe_followers_subtype(self, cr, uid, ids, res_id, model, context=None): + """ TDE note: not the best way to do this, we could override _get_followers + of task, and perform a better mapping of subtypes than a mapping + based on names. + However we will keep this implementation, maybe to be refactored + in 7.1 of future versions. """ + subtype_obj = self.pool.get('mail.message.subtype') + follower_obj = self.pool.get('mail.followers') + # create mapping + subtype_ids = subtype_obj.search(cr, uid, ['|', ('res_model', '=', False), ('res_model', '=', self._name)], context=context) + subtypes = subtype_obj.browse(cr, uid, subtype_ids, context=context) + # fetch subscriptions + follower_ids = follower_obj.search(cr, uid, [('res_model', '=', model), ('res_id', '=', res_id)], context=context) + # copy followers + for follower in follower_obj.browse(cr, uid, follower_ids, context=context): + if not follower.subtype_ids: + continue + subtype_names = [follower_subtype.name for follower_subtype in follower.subtype_ids] + subtype_ids = [subtype.id for subtype in subtypes if subtype.name in subtype_names] + self.message_subscribe(cr, uid, ids, [follower.partner_id.id], + subtype_ids=subtype_ids, context=context) # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/project/project.py b/addons/project/project.py index f0414952e02..f6ba1941ab3 100644 --- a/addons/project/project.py +++ b/addons/project/project.py @@ -1106,35 +1106,12 @@ class task(base_stage, osv.osv): }, context=context) return True - def _subscribe_project_followers_to_task(self, cr, uid, task_id, context=None): - """ TDE note: not the best way to do this, we could override _get_followers - of task, and perform a better mapping of subtypes than a mapping - based on names. - However we will keep this implementation, maybe to be refactored - in 7.1 of future versions. """ - # task followers are project followers, with matching subtypes - task_record = self.browse(cr, uid, task_id, context=context) - subtype_obj = self.pool.get('mail.message.subtype') - follower_obj = self.pool.get('mail.followers') - if task_record.project_id: - # create mapping - task_subtype_ids = subtype_obj.search(cr, uid, ['|', ('res_model', '=', False), ('res_model', '=', self._name)], context=context) - task_subtypes = subtype_obj.browse(cr, uid, task_subtype_ids, context=context) - # fetch subscriptions - follower_ids = follower_obj.search(cr, uid, [('res_model', '=', 'project.project'), ('res_id', '=', task_record.project_id.id)], context=context) - # copy followers - for follower in follower_obj.browse(cr, uid, follower_ids, context=context): - if not follower.subtype_ids: - continue - project_subtype_names = [project_subtype.name for project_subtype in follower.subtype_ids] - task_subtype_ids = [task_subtype.id for task_subtype in task_subtypes if task_subtype.name in project_subtype_names] - self.message_subscribe(cr, uid, [task_id], [follower.partner_id.id], - subtype_ids=task_subtype_ids, context=context) - def create(self, cr, uid, vals, context=None): task_id = super(task, self).create(cr, uid, vals, context=context) - # subscribe project followers to the task - self._subscribe_project_followers_to_task(cr, uid, task_id, context=context) + project_id = self.browse(cr, uid, task_id, context=context).project_id + if project_id: + # subscribe project followers to the task + self._subscribe_followers_subtype(cr, uid, [task_id], project_id, 'project.project', context=context) self._store_history(cr, uid, [task_id], context=context) self.create_send_note(cr, uid, [task_id], context=context) @@ -1163,8 +1140,7 @@ class task(base_stage, osv.osv): # subscribe new project followers to the task if vals.get('project_id'): - for id in ids: - self._subscribe_project_followers_to_task(cr, uid, id, context=context) + self._subscribe_followers_subtype(cr, uid, ids, vals.get('project_id'), 'project.project', context=context) return result def unlink(self, cr, uid, ids, context=None): diff --git a/addons/project_issue/project_issue.py b/addons/project_issue/project_issue.py index 4d20f53c213..3255898999b 100644 --- a/addons/project_issue/project_issue.py +++ b/addons/project_issue/project_issue.py @@ -366,31 +366,6 @@ class project_issue(base_stage, osv.osv): return super(project_issue, self).copy(cr, uid, id, default=default, context=context) - def _subscribe_project_followers_to_issue(self, cr, uid, task_id, context=None): - """ TDE note: not the best way to do this, we could override _get_followers - of issue, and perform a better mapping of subtypes than a mapping - based on names. - However we will keep this implementation, maybe to be refactored - in 7.1 of future versions. """ - # task followers are project followers, with matching subtypes - task_record = self.browse(cr, uid, task_id, context=context) - subtype_obj = self.pool.get('mail.message.subtype') - follower_obj = self.pool.get('mail.followers') - if task_record.project_id: - # create mapping - task_subtype_ids = subtype_obj.search(cr, uid, ['|', ('res_model', '=', False), ('res_model', '=', self._name)], context=context) - task_subtypes = subtype_obj.browse(cr, uid, task_subtype_ids, context=context) - # fetch subscriptions - follower_ids = follower_obj.search(cr, uid, [('res_model', '=', 'project.project'), ('res_id', '=', task_record.project_id.id)], context=context) - # copy followers - for follower in follower_obj.browse(cr, uid, follower_ids, context=context): - if not follower.subtype_ids: - continue - project_subtype_names = [project_subtype.name for project_subtype in follower.subtype_ids] - task_subtype_ids = [task_subtype.id for task_subtype in task_subtypes if task_subtype.name in project_subtype_names] - self.message_subscribe(cr, uid, [task_id], [follower.partner_id.id], - subtype_ids=task_subtype_ids, context=context) - def write(self, cr, uid, ids, vals, context=None): #Update last action date every time the user change the stage, the state or send a new email logged_fields = ['stage_id', 'state', 'message_ids'] @@ -399,8 +374,7 @@ class project_issue(base_stage, osv.osv): # subscribe new project followers to the issue if vals.get('project_id'): - for id in ids: - self._subscribe_project_followers_to_issue(cr, uid, id, context=context) + self._subscribe_followers_subtype(cr, uid, ids, vals.get('project_id'), 'project.project', context=context) return super(project_issue, self).write(cr, uid, ids, vals, context) @@ -420,8 +394,10 @@ class project_issue(base_stage, osv.osv): def create(self, cr, uid, vals, context=None): obj_id = super(project_issue, self).create(cr, uid, vals, context=context) - # subscribe project follower to the issue - self._subscribe_project_followers_to_issue(cr, uid, obj_id, context=context) + project_id = self.browse(cr, uid, obj_id, context=context).project_id + if project_id: + # subscribe project follower to the issue + self._subscribe_followers_subtype(cr, uid, [obj_id], project_id, 'project.project', context=context) self.create_send_note(cr, uid, [obj_id], context=context) return obj_id diff --git a/addons/sale_crm/sale_crm.py b/addons/sale_crm/sale_crm.py index 74383f45bd9..c1f50800d24 100644 --- a/addons/sale_crm/sale_crm.py +++ b/addons/sale_crm/sale_crm.py @@ -31,37 +31,17 @@ class sale_order(osv.osv): def create(self, cr, uid, vals, context=None): order = super(sale_order, self).create(cr, uid, vals, context=context) - # subscribe salesteam followers & subtypes to the sale order - self._subscribe_salesteam_followers_to_order(cr, uid, [order], context=context) - return order - - def _subscribe_salesteam_followers_to_order(self, cr, uid, order, context=None): - follower_obj = self.pool.get('mail.followers') - subtype_obj = self.pool.get('mail.message.subtype') - rec = self.browse(cr, uid, order, context=context) - section_id = rec and rec[0].section_id or False + section_id = self.browse(cr, uid, order, context=context).section_id if section_id: - # fetch subscribers - followers = [follow.id for follow in section_id.message_follower_ids] - order_subtype_ids = subtype_obj.search(cr, uid, ['|', ('res_model', '=', False), ('res_model', '=', self._name)], context=context) - order_subtypes = subtype_obj.browse(cr, uid, order_subtype_ids, context=context) - # fetch subscriptions - follower_ids = follower_obj.search(cr, uid, [('res_model', '=', 'crm.case.section'), ('res_id', '=', section_id)], context=context) - # when subscribe new salesteam update followers - self.write(cr, uid, order, {'message_follower_ids': [(6, 0, followers)]}, context=context) - # copy followers & select subtypes - for follower in follower_obj.browse(cr, uid, follower_ids, context=context): - if not follower.subtype_ids: - continue - salesteam_subtype_names = [salesteam_subtype.name for salesteam_subtype in follower.subtype_ids] - order_subtype_ids = [order_subtype.id for order_subtype in order_subtypes if order_subtype.name in salesteam_subtype_names] - self.message_subscribe(cr, uid, order, [follower.partner_id.id], subtype_ids=order_subtype_ids, context=context) + # subscribe salesteam followers & subtypes to the sale order + self._subscribe_followers_subtype(cr, uid, [order], section_id, 'crm.case.section', context=context) + return order def write(self, cr, uid, ids, vals, context=None): res = super(sale_order, self).write(cr, uid, ids, vals, context=context) # subscribe new salesteam followers & subtypes to the sale order if vals.get('section_id'): - self._subscribe_salesteam_followers_to_order(cr, uid, ids, context=context) + self._subscribe_followers_subtype(cr, uid, ids, vals.get('section_id'), 'crm.case.section', context=context) return res sale_order()