From d6a601fe3fe2f7983415f8bb5d8552d28ad2cf04 Mon Sep 17 00:00:00 2001 From: XavierDo Date: Thu, 2 Aug 2018 13:18:53 +0200 Subject: [PATCH] [IMP] mail: do not subscribe inactive partners We do not want inactive partners to be added as followers. Indeed this may lead to unwanted notifications send as inactive partners are not easily found in the various widgets. It also creates unnecessary data and computation. This commit has a small impact on performances as we choose to do it in message_ subscribe method. Private _message_method is left untouched as parameters given to this method are left to caller control. This commit is linked to task ID 1838588. --- addons/mail/models/mail_thread.py | 4 ++++ addons/test_mail/tests/test_performance.py | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index fbdfcbd42e9..1288f780d49 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -2203,6 +2203,10 @@ class MailThread(models.AbstractModel): self.check_access_rights('write') self.check_access_rule('write') + # filter inactive + if partner_ids and not adding_current: + partner_ids = self.env['res.partner'].sudo().search([('id', 'in', partner_ids), ('active', '=', True)]).ids + return self._message_subscribe(partner_ids, channel_ids, subtype_ids, customer_ids=customer_ids) def _message_subscribe(self, partner_ids=None, channel_ids=None, subtype_ids=None, customer_ids=None): diff --git a/addons/test_mail/tests/test_performance.py b/addons/test_mail/tests/test_performance.py index 94f5fcd7817..f79c235e377 100644 --- a/addons/test_mail/tests/test_performance.py +++ b/addons/test_mail/tests/test_performance.py @@ -199,7 +199,7 @@ class TestAdvMailPerformance(TransactionCase): self.user_test.write({'notification_type': 'email'}) record = self.env['mail.test.track'].create({'name': 'Test'}) - with self.assertQueryCount(margin=1, admin=56, emp=71): # com runbot: 56 - 71 // test_mail only: 56 - 71 + with self.assertQueryCount(margin=1, admin=56, emp=71): # com runbot: 56 - 71 // test_mail only: 56 - 71 record.write({ 'user_id': self.user_test.id, }) @@ -253,7 +253,7 @@ class TestAdvMailPerformance(TransactionCase): def test_message_post_one_email_notification(self): record = self.env['mail.test.simple'].create({'name': 'Test'}) - with self.assertQueryCount(margin=1, admin=47, emp=64): # test_mail only: 47 - 64 + with self.assertQueryCount(margin=1, admin=47, emp=64): # com runbot: 45 - 62 // test_mail only: 47 - 64 record.message_post( body='

Test Post Performances with an email ping

', partner_ids=self.customer.ids, @@ -278,10 +278,10 @@ class TestAdvMailPerformance(TransactionCase): def test_message_subscribe_default(self): record = self.env['mail.test.simple'].create({'name': 'Test'}) - with self.assertQueryCount(admin=6, emp=6): # test_mail only: 6 - 6 + with self.assertQueryCount(admin=7, emp=7): # test_mail only: 6 - 6 record.message_subscribe(partner_ids=self.user_test.partner_id.ids) - with self.assertQueryCount(admin=2, emp=2): # test_mail only: 2 - 2 + with self.assertQueryCount(admin=3, emp=3): # test_mail only: 2 - 2 record.message_subscribe(partner_ids=self.user_test.partner_id.ids) @mute_logger('odoo.models.unlink') @@ -291,10 +291,10 @@ class TestAdvMailPerformance(TransactionCase): record = self.env['mail.test.simple'].create({'name': 'Test'}) subtype_ids = (self.env.ref('test_mail.st_mail_test_simple_external') | self.env.ref('mail.mt_comment')).ids - with self.assertQueryCount(admin=5, emp=5): # test_mail only: 5 - 5 + with self.assertQueryCount(admin=6, emp=6): # test_mail only: 5 - 5 record.message_subscribe(partner_ids=self.user_test.partner_id.ids, subtype_ids=subtype_ids) - with self.assertQueryCount(admin=1, emp=1): # test_mail only: 1 - 1 + with self.assertQueryCount(admin=2, emp=2): # test_mail only: 1 - 1 record.message_subscribe(partner_ids=self.user_test.partner_id.ids, subtype_ids=subtype_ids) @@ -431,7 +431,7 @@ class TestHeavyMailPerformance(TransactionCase): self.assertEqual(rec.message_channel_ids, self.env['mail.channel']) # subscribe new followers with forced given subtypes - with self.assertQueryCount(admin=8, emp=8): # test_mail only: 8 - 8 + with self.assertQueryCount(admin=9, emp=9): # test_mail only: 8 - 8 rec.message_subscribe( partner_ids=pids[:4], channel_ids=cids, @@ -442,7 +442,7 @@ class TestHeavyMailPerformance(TransactionCase): self.assertEqual(rec.message_channel_ids, self.channel) # subscribe existing and new followers with force=False, meaning only some new followers will be added - with self.assertQueryCount(admin=6, emp=6): # test_mail only: 6 - 6 + with self.assertQueryCount(admin=7, emp=7): # test_mail only: 6 - 6 rec.message_subscribe( partner_ids=pids[:6], channel_ids=cids, @@ -453,7 +453,7 @@ class TestHeavyMailPerformance(TransactionCase): self.assertEqual(rec.message_channel_ids, self.channel) # subscribe existing and new followers with force=True, meaning all will have the same subtypes - with self.assertQueryCount(admin=7, emp=7): # test_mail only: 7 - 7 + with self.assertQueryCount(admin=8, emp=8): # test_mail only: 7 - 7 rec.message_subscribe( partner_ids=pids, channel_ids=cids, @@ -499,7 +499,7 @@ class TestHeavyMailPerformance(TransactionCase): customer_id = self.customer.id user_id = self.user_portal.id - with self.assertQueryCount(margin=1, admin=155, emp=184): # com runbot: 154 - 183 // test_mail only: 155 - 184 + with self.assertQueryCount(margin=1, admin=155, emp=184): # com runbot: 155 - 184 // test_mail only: 155 - 184 rec = self.env['mail.test.full'].create({ 'name': 'Test', 'umbrella_id': umbrella_id,