Skip to content

Commit

Permalink
[IMP] mail: use minimal context when rendering mail-related QWeb temp…
Browse files Browse the repository at this point in the history
…lates

Use option recently introduced to use a minimal context when rendering
some mail templates such as

 * notification template used to decorate messages sent to recipient
   when using email-based notifications;
 * user assignation template used when people are assigned on a record
   and a notification is sent to inbox or email;
 * message_post_with_template now uses minimal context meaning calls to
   that method should be sure evaluation context is correctly set in
   the given values;

This optimization allows to save some queries every time one of those
template is used, meaning all tests gain between 1 to 4 queries depending
on their complexity.

This commit is related to task ID 1817951. Closes #23291 .
  • Loading branch information
tde-banana-odoo committed Mar 20, 2018
1 parent 42a4b54 commit 2826edc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions addons/mail/models/mail_thread.py
Expand Up @@ -1931,7 +1931,7 @@ def message_post_with_view(self, views_or_xmlid, **kwargs):
return
for record in self:
values['object'] = record
rendered_template = views.render(values, engine='ir.qweb')
rendered_template = views.render(values, engine='ir.qweb', minimal_qcontext=True)
kwargs['body'] = rendered_template
record.message_post_with_template(False, **kwargs)

Expand Down Expand Up @@ -2148,7 +2148,7 @@ def _message_auto_subscribe_notify(self, partner_ids):
values = {
'object': record,
}
assignation_msg = assignation_tpl.render(values, engine='ir.qweb')
assignation_msg = assignation_tpl.render(values, engine='ir.qweb', minimal_qcontext=True)
assignation_msg = self.env['mail.thread']._replace_local_links(assignation_msg)
record.message_notify(
subject='You have been assigned to %s' % record.display_name,
Expand Down
2 changes: 1 addition & 1 deletion addons/mail/models/res_partner.py
Expand Up @@ -191,7 +191,7 @@ def _notify(self, message, layout=False, force_send=False, send_after_commit=Tru
template_ctx = {**base_template_ctx, **recipient_template_values, **values} # fixme: set button_unfollow to none
fol_values = {
'subject': message.subject or (message.record_name and 'Re: %s' % message.record_name),
'body': base_template.render(template_ctx, engine='ir.qweb'),
'body': base_template.render(template_ctx, engine='ir.qweb', minimal_qcontext=True),
}
fol_values['body'] = self.env['mail.thread']._replace_local_links(fol_values['body'])
# send email
Expand Down
20 changes: 10 additions & 10 deletions addons/test_mail/tests/test_performance.py
Expand Up @@ -167,7 +167,7 @@ def test_adv_activity_full(self):
'activity_type_id': self.env.ref('mail.mail_activity_data_todo').id,
})

with self.assertQueryCount(admin=60, emp=89): # test_mail only: 59 - 87
with self.assertQueryCount(admin=59, emp=88): # test_mail only: 58 - 86
activity.action_feedback(feedback='Zizisse Done !')

@mute_logger('odoo.addons.mail.models.mail_mail', 'odoo.models.unlink')
Expand All @@ -177,7 +177,7 @@ def test_message_assignation_email(self):
self.user_test.write({'notification_type': 'email'})
record = self.env['mail.test.track'].create({'name': 'Test'})

with self.assertQueryCount(admin=92, emp=120): # test_mail only: 90 - 118
with self.assertQueryCount(admin=90, emp=117): # test_mail only: 88 - 115
record.write({
'user_id': self.user_test.id,
})
Expand All @@ -187,7 +187,7 @@ def test_message_assignation_email(self):
def test_message_assignation_inbox(self):
record = self.env['mail.test.track'].create({'name': 'Test'})

with self.assertQueryCount(admin=51, emp=66): # test_mail only: 51 - 66
with self.assertQueryCount(admin=50, emp=65): # test_mail only: 50 - 65
record.write({
'user_id': self.user_test.id,
})
Expand Down Expand Up @@ -231,7 +231,7 @@ def test_message_post_no_notification(self):
def test_message_post_one_email_notification(self):
record = self.env['mail.test.simple'].create({'name': 'Test'})

with self.assertQueryCount(admin=80, emp=108): # com runbot 78 - 106 // test_mail only: 78 - 106
with self.assertQueryCount(admin=79, emp=106): # com runbot 77 - 104 // test_mail only: 77 - 104
record.message_post(
body='<p>Test Post Performances with an email ping</p>',
partner_ids=self.customer.ids,
Expand Down Expand Up @@ -366,7 +366,7 @@ def test_complex_message_post(self):
self.umbrella.message_subscribe(self.user_portal.partner_id.ids)
record = self.umbrella.sudo(self.env.user)

with self.assertQueryCount(admin=118, emp=150): # com runbot 116 - 148 // test_mail only: 114 - 146
with self.assertQueryCount(admin=117, emp=148): # com runbot 115 - 146 // test_mail only: 113 - 144
record.message_post(
body='<p>Test Post Performances</p>',
message_type='comment',
Expand All @@ -383,7 +383,7 @@ def test_complex_message_post_template(self):
record = self.umbrella.sudo(self.env.user)
template_id = self.env.ref('test_mail.mail_test_tpl').id

with self.assertQueryCount(admin=141, emp=187): # com runbot 139 - 185 // test_mail only: 137 - 183
with self.assertQueryCount(admin=140, emp=185): # com runbot 138 - 183 // test_mail only: 136 - 181
record.message_post_with_template(template_id, message_type='comment', composition_mode='comment')

self.assertEqual(record.message_ids[0].body, '<p>Adding stuff on %s</p>' % record.name)
Expand Down Expand Up @@ -455,7 +455,7 @@ def test_complex_tracking_assignation(self):
})
self.assertEqual(rec.message_partner_ids, self.partners | self.env.user.partner_id)

with self.assertQueryCount(admin=94, emp=123): # test_mail only: 92 - 121
with self.assertQueryCount(admin=92, emp=121): # test_mail only: 90 - 119
rec.write({'user_id': self.user_portal.id})

self.assertEqual(rec.message_partner_ids, self.partners | self.env.user.partner_id | self.user_portal.partner_id)
Expand All @@ -478,7 +478,7 @@ def test_complex_tracking_subscription_create(self):
customer_id = self.customer.id
user_id = self.user_portal.id

with self.assertQueryCount(admin=326, emp=388): # test_mail only: 319 - 381
with self.assertQueryCount(admin=321, emp=382): # test_mail only: 315 - 376
rec = self.env['mail.test.full'].create({
'name': 'Test',
'umbrella_id': umbrella_id,
Expand Down Expand Up @@ -507,7 +507,7 @@ def test_complex_tracking_subscription_subtype(self):
})
self.assertEqual(rec.message_partner_ids, self.user_portal.partner_id | self.env.user.partner_id)

with self.assertQueryCount(admin=230, emp=268): # test_mail only: 226 - 264
with self.assertQueryCount(admin=228, emp=265): # test_mail only: 224 - 261
rec.write({
'name': 'Test2',
'umbrella_id': self.umbrella.id,
Expand Down Expand Up @@ -545,7 +545,7 @@ def test_complex_tracking_subscription_write(self):
})
self.assertEqual(rec.message_partner_ids, self.user_portal.partner_id | self.env.user.partner_id)

with self.assertQueryCount(admin=233, emp=272): # test_mail only: 228 - 268
with self.assertQueryCount(admin=230, emp=269): # test_mail only: 226 - 265
rec.write({
'name': 'Test2',
'umbrella_id': umbrella_id,
Expand Down

0 comments on commit 2826edc

Please sign in to comment.