From 034d369c100511e8c9ee2dd469a453d6240dc45d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Voet=20=28ryv=29?= Date: Mon, 6 Dec 2021 15:19:46 +0000 Subject: [PATCH] [FIX] mail: batch `_insert_followers` in create of `mail.thread` The `_insert_followers` wasn't batch in the `create` of `mail.thread` for no reason, which call the create of a `mail.follower` one by one. It is inefficient for the batch records creation (import or some flows) of a model with `_inherit = [mail.thread, ...]`. Then batch it, to miminize the cost of `_insert_followers`: The creation of 100 `mail.follower` takes: - 0.107 sec if you create one by one (before) - 0.022 sec if you create in batch (now) Also add two tests (on model with a chatter): - One testing the behavior of follower and subtype in case of multi-create - One testing the number of queries done to create 5 records. The old SQL request count was 23, now it is 19 (-1 by create call). task-2711448 closes odoo/odoo#80954 Signed-off-by: Thibault Delavallee (tde) --- addons/mail/models/mail_thread.py | 15 +++++++-------- addons/test_mail/tests/test_mail_followers.py | 7 +++++++ addons/test_mail/tests/test_performance.py | 6 ++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 6f98ab4540f..7d2c9a5f17b 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -263,14 +263,13 @@ class MailThread(models.AbstractModel): threads = super(MailThread, self).create(vals_list) # subscribe uid unless asked not to - if not self._context.get('mail_create_nosubscribe'): - for thread in threads: - self.env['mail.followers']._insert_followers( - thread._name, thread.ids, - self.env.user.partner_id.ids, subtypes=None, - customer_ids=[], - check_existing=False - ) + if not self._context.get('mail_create_nosubscribe') and threads: + self.env['mail.followers']._insert_followers( + threads._name, threads.ids, + self.env.user.partner_id.ids, subtypes=None, + customer_ids=[], + check_existing=False + ) # auto_subscribe: take values and defaults into account create_values_list = {} diff --git a/addons/test_mail/tests/test_mail_followers.py b/addons/test_mail/tests/test_mail_followers.py index 112b9bb40cf..f06f1c006e5 100644 --- a/addons/test_mail/tests/test_mail_followers.py +++ b/addons/test_mail/tests/test_mail_followers.py @@ -162,6 +162,13 @@ class BaseFollowersTest(TestMailCommon): document._message_subscribe(partner_ids=(self.partner_portal | private_address).ids) self.assertEqual(document.message_follower_ids.partner_id, self.partner_portal | private_address) + @users('employee') + def test_create_multi_followers(self): + documents = self.env['mail.test.simple'].create([{'name': 'ninja'}] * 5) + for document in documents: + self.assertEqual(document.message_follower_ids.partner_id, self.env.user.partner_id) + self.assertEqual(document.message_follower_ids.subtype_ids, self.default_group_subtypes) + @tagged('mail_followers') class AdvancedFollowersTest(TestMailCommon): diff --git a/addons/test_mail/tests/test_performance.py b/addons/test_mail/tests/test_performance.py index 34d5bf0298d..eec84943fa2 100644 --- a/addons/test_mail/tests/test_performance.py +++ b/addons/test_mail/tests/test_performance.py @@ -193,6 +193,12 @@ class TestBaseMailPerformance(BaseMailPerformance): with self.assertQueryCount(__system__=7, employee=7): self.env['mail.test.simple'].create({'name': 'Test'}) + @users('__system__', 'employee') + @warmup + def test_create_mail_simple_multi(self): + with self.assertQueryCount(__system__=19, employee=19): + self.env['mail.test.simple'].create([{'name': 'Test'}] * 5) + @users('__system__', 'employee') @warmup def test_write_mail_simple(self):