From 2238408d51659c06da1b1a88d2d8a177c8febcb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Thu, 11 Jan 2018 14:34:00 +0100 Subject: [PATCH] [REM] mail: remove unnecessary context switch in mail send method Commit 0d3d0e95636cf8ae0d52c650a28f505a89c3dc89 added a key in context when sending emails to know the target model name. It was used at that time to forge for footer of notification emails. Since that time notification emails have evolved a lot. The code based on that context key has been removed. Context key has been transformed into a context switch. However it is not used anymore and can be removed. It allows to clean a bit the code and save a context switch. This commit is purely technical and does not hold any functional change. Purpose is to clean code in order to prepare future changes in mail application. This commit allows to gain some queries in mail sending process, meaning removing context switch is interesting. One query is gained when using the send process. Complex performance test involving several message post gain 3 queries each. --- addons/mail/models/mail_mail.py | 7 ------- addons/test_mail/tests/test_performance.py | 10 +++++----- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/addons/mail/models/mail_mail.py b/addons/mail/models/mail_mail.py index 96040b015f4..bff6e3d8ca7 100644 --- a/addons/mail/models/mail_mail.py +++ b/addons/mail/models/mail_mail.py @@ -255,13 +255,6 @@ class MailMail(models.Model): if mail.state != 'exception' and mail.auto_delete: mail.sudo().unlink() continue - # TDE note: remove me when model_id field is present on mail.message - done here to avoid doing it multiple times in the sub method - if mail.model: - model = self.env['ir.model']._get(mail.model)[0] - else: - model = None - if model: - mail = mail.with_context(model_name=model.name) # load attachment binary data with a separate read(), as prefetching all # `datas` (binary field) could bloat the browse cache, triggerring diff --git a/addons/test_mail/tests/test_performance.py b/addons/test_mail/tests/test_performance.py index eeef3e2519d..c3649a25c6d 100644 --- a/addons/test_mail/tests/test_performance.py +++ b/addons/test_mail/tests/test_performance.py @@ -230,7 +230,7 @@ class TestHeavyMailPerformance(TransactionCase): }) mail_ids = mail.ids - with self.assertQueryCount(admin=16, emp=24): # com runbot 16 - 24 // test_mail only: 14 - 22 + with self.assertQueryCount(admin=16, emp=24): # com runbot 16 - 24 // test_mail only: 13 - 21 self.env['mail.mail'].browse(mail_ids).send() self.assertEqual(mail.body_html, '

Test

') @@ -243,7 +243,7 @@ class TestHeavyMailPerformance(TransactionCase): self.umbrella.message_subscribe(self.user_portal.partner_id.ids) record = self.umbrella.sudo(self.env.user) - with self.assertQueryCount(admin=123, emp=156): # com runbot 121 - 154 // test_mail only: 117 - 149 + with self.assertQueryCount(admin=120, emp=153): # com runbot 118 - 151 // test_mail only: 114 - 146 record.message_post( body='

Test Post Performances

', message_type='comment', @@ -260,7 +260,7 @@ class TestHeavyMailPerformance(TransactionCase): record = self.umbrella.sudo(self.env.user) template_id = self.env.ref('test_mail.mail_test_tpl').id - with self.assertQueryCount(admin=148, emp=196): # com runbot 146 - 194 // test_mail only: 140 - 186 + with self.assertQueryCount(admin=145, emp=193): # com runbot 143 - 191 // test_mail only: 137 - 183 record.message_post_with_template(template_id, message_type='comment', composition_mode='comment') self.assertEqual(record.message_ids[0].body, '

Adding stuff on %s

' % record.name) @@ -276,7 +276,7 @@ class TestHeavyMailPerformance(TransactionCase): customer_id = self.customer.id user_id = self.user_portal.id - with self.assertQueryCount(admin=335, emp=397): # test_mail only: 322 - 382 + with self.assertQueryCount(admin=332, emp=394): # test_mail only: 319 - 379 rec = self.env['mail.test.full'].create({ 'name': 'Test', 'umbrella_id': umbrella_id, @@ -315,7 +315,7 @@ class TestHeavyMailPerformance(TransactionCase): }) self.assertEqual(rec.message_partner_ids, self.user_portal.partner_id | self.env.user.partner_id) - with self.assertQueryCount(admin=238, emp=278): # test_mail only: 231 - 270 + with self.assertQueryCount(admin=235, emp=275): # test_mail only: 228 - 267 rec.write({ 'name': 'Test2', 'umbrella_id': umbrella_id,