From f8b3788fdea058d266f170a6ab459df7e6e24803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Tue, 18 Dec 2012 16:34:57 +0100 Subject: [PATCH] [IMP] mail: refactored mechanism for auto subscription, based on a parent. Mapping of subtypes is done using parent_id and parent_field on child subtypes. For example, project subtypes are linked to task subtypes, using parent_id and parent_field project_id, to browse task.project_id. bzr revid: tde@openerp.com-20121218153457-uh50bjedvxaawdqj --- addons/mail/mail_message_subtype.py | 4 ++++ addons/mail/mail_message_subtype.xml | 2 ++ addons/mail/mail_thread.py | 26 +++++++++++++++++++++++++ addons/mail/tests/test_mail_features.py | 3 +-- 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/addons/mail/mail_message_subtype.py b/addons/mail/mail_message_subtype.py index 12da272fa4b..d6d983611a0 100644 --- a/addons/mail/mail_message_subtype.py +++ b/addons/mail/mail_message_subtype.py @@ -39,6 +39,10 @@ class mail_message_subtype(osv.osv): 'description': fields.text('Description', translate=True, help='Description that will be added in the message posted for this '\ 'subtype. If void, no message will be added.'), + 'parent_id': fields.many2one('mail.message.subtype', string='Parent', + ondelete='set null'), + 'parent_field': fields.char('Parent field', + help='Field used to find the parent model'), 'res_model': fields.char('Model', help="Model the subtype applies to. If False, this subtype exists for all models."), 'default': fields.boolean('Default', diff --git a/addons/mail/mail_message_subtype.xml b/addons/mail/mail_message_subtype.xml index 3084a98d481..da138858446 100644 --- a/addons/mail/mail_message_subtype.xml +++ b/addons/mail/mail_message_subtype.xml @@ -26,6 +26,8 @@ + + diff --git a/addons/mail/mail_thread.py b/addons/mail/mail_thread.py index 90efba013ab..55bf33b949d 100644 --- a/addons/mail/mail_thread.py +++ b/addons/mail/mail_thread.py @@ -239,6 +239,7 @@ class mail_thread(osv.AbstractModel): # subscribe uid unless asked not to if not context.get('mail_nosubscribe'): self.message_subscribe_users(cr, uid, [thread_id], [uid], context=context) + self.message_subscribe_from_parent(cr, uid, [thread_id], context=context) # automatic logging # self.message_post(cr, uid, thread_id, body='Document created.', context=context) @@ -953,6 +954,31 @@ class mail_thread(osv.AbstractModel): self.check_access_rights(cr, uid, 'read') return self.write(cr, SUPERUSER_ID, ids, {'message_follower_ids': [(3, pid) for pid in partner_ids]}, context=context) + def message_subscribe_from_parent(self, cr, uid, ids, context=None): + + subtype_obj = self.pool.get('mail.message.subtype') + follower_obj = self.pool.get('mail.followers') + + # fetch record subtypes + subtype_ids = subtype_obj.search(cr, uid, ['|', ('parent_id.res_model', '=', False), ('parent_id.res_model', '=', self._name)], context=context) + if not subtype_ids: + return + subtypes = subtype_obj.browse(cr, uid, subtype_ids, context=context) + + for record in self.browse(cr, uid, ids, context=context): + new_followers = dict() + for subtype in subtypes: + if subtype.parent_field and subtype.parent_id: + if subtype.parent_field in self._columns and getattr(record, subtype.parent_field): + parent_res_id = getattr(record, subtype.parent_field).id + parent_model = subtype.res_model + follower_ids = follower_obj.search(cr, SUPERUSER_ID, [('res_model', '=', parent_model), ('res_id', '=', parent_res_id), ('subtype_ids', 'in', [subtype.id])], context=context) + for follower in follower_obj.browse(cr, SUPERUSER_ID, follower_ids, context=context): + new_followers.setdefault(follower.partner_id.id, set()).add(subtype.parent_id.id) + + for pid, subtypes in new_followers.items(): + self.message_subscribe(cr, uid, [record.id], [pid], list(subtypes), context=context) + 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 diff --git a/addons/mail/tests/test_mail_features.py b/addons/mail/tests/test_mail_features.py index 3666c139279..8a5dc4fb690 100644 --- a/addons/mail/tests/test_mail_features.py +++ b/addons/mail/tests/test_mail_features.py @@ -594,9 +594,8 @@ class test_mail(TestMailBase): def _strip_string_spaces(body): return body.replace(' ', '').replace('\n', '') - cr, uid = self.cr, self.uid - # Data: subscribe Raoul to Pigs, because he will change the public attribute and may loose access to the record + cr, uid = self.cr, self.uid self.mail_group.message_subscribe_users(cr, uid, [self.group_pigs_id], [self.user_raoul_id]) # Data: res.users.group, to test group_public_id automatic logging