[IMP] mail, test_mail: simplify subtype in message_format

This makes the method easier to follow and does not increase the query count.

It actually reduces it when the subtype data were already in the cache, by
not always reading them again.

Part of task-2180311
PR: #43841
This commit is contained in:
Sébastien Theys
2020-03-06 16:15:44 +00:00
parent 04fda71e42
commit 2490676e16
2 changed files with 9 additions and 15 deletions
+3 -9
View File
@@ -1095,12 +1095,6 @@ class Message(models.Model):
message_tree = dict((m.id, m) for m in self.sudo())
self._message_read_dict_postprocess(message_values, message_tree)
# add subtype data (is_note flag, is_discussion flag , subtype_description). Do it as sudo
# because portal / public may have to look for internal subtypes
subtype_ids = [msg['subtype_id'][0] for msg in message_values if msg['subtype_id']]
subtypes = self.env['mail.message.subtype'].sudo().browse(subtype_ids).read(['internal', 'description','id'])
subtypes_dict = dict((subtype['id'], subtype) for subtype in subtypes)
com_id = self.env['ir.model.data'].xmlid_to_res_id('mail.mt_comment')
note_id = self.env['ir.model.data'].xmlid_to_res_id('mail.mt_note')
@@ -1110,9 +1104,9 @@ class Message(models.Model):
message.update({
'needaction_partner_ids': notifs.filtered(lambda n: not n.is_read).res_partner_id.ids,
'history_partner_ids': notifs.filtered(lambda n: n.is_read).res_partner_id.ids,
'is_note': message['subtype_id'] and subtypes_dict[message['subtype_id'][0]]['id'] == note_id,
'is_discussion': message['subtype_id'] and subtypes_dict[message['subtype_id'][0]]['id'] == com_id,
'subtype_description': message['subtype_id'] and subtypes_dict[message['subtype_id'][0]]['description']
'is_note': message_sudo.subtype_id.id == note_id,
'is_discussion': message_sudo.subtype_id.id == com_id,
'subtype_description': message_sudo.subtype_id.description
})
message['is_notification'] = message['message_type'] == 'user_notification'
+6 -6
View File
@@ -267,7 +267,7 @@ class TestMailAPIPerformance(BaseMailPerformance):
'partner_ids': [(4, customer_id)],
})
with self.assertQueryCount(__system__=34, emp=39):
with self.assertQueryCount(__system__=33, emp=38):
composer.send_mail()
@users('__system__', 'emp')
@@ -286,7 +286,7 @@ class TestMailAPIPerformance(BaseMailPerformance):
}).create({})
composer.onchange_template_id_wrapper()
with self.assertQueryCount(__system__=42, emp=46):
with self.assertQueryCount(__system__=41, emp=45):
composer.send_mail()
# remove created partner to ensure tests are the same each run
@@ -307,7 +307,7 @@ class TestMailAPIPerformance(BaseMailPerformance):
@warmup
def test_message_assignation_inbox(self):
record = self.env['mail.test.track'].create({'name': 'Test'})
with self.assertQueryCount(__system__=23, emp=25):
with self.assertQueryCount(__system__=22, emp=24):
record.write({
'user_id': self.user_test.id,
})
@@ -363,7 +363,7 @@ class TestMailAPIPerformance(BaseMailPerformance):
def test_message_post_one_inbox_notification(self):
record = self.env['mail.test.simple'].create({'name': 'Test'})
with self.assertQueryCount(__system__=18, emp=20):
with self.assertQueryCount(__system__=17, emp=19):
record.message_post(
body='<p>Test Post Performances with an inbox ping</p>',
partner_ids=self.user_test.partner_id.ids,
@@ -794,7 +794,7 @@ class TestMailComplexPerformance(BaseMailPerformance):
]
}])
with self.assertQueryCount(emp=10):
with self.assertQueryCount(emp=9):
res = messages.message_format()
self.assertEqual(len(res), 2)
for message in res:
@@ -803,7 +803,7 @@ class TestMailComplexPerformance(BaseMailPerformance):
messages.flush()
messages.invalidate_cache()
with self.assertQueryCount(emp=19):
with self.assertQueryCount(emp=18):
res = messages.message_format()
self.assertEqual(len(res), 2)
for message in res: