[FIX] mail: handle non-thread model in _message_format

Calling _message_format on a anything but a thread could throw a traceback.

Task-3254379

Part-of: odoo/odoo#117763
This commit is contained in:
Renaud Thiry
2023-04-05 15:11:33 +02:00
committed by Thibault Delavallée
parent 2cc2690c8f
commit 442467ceb8
3 changed files with 37 additions and 11 deletions
+2 -1
View File
@@ -884,7 +884,8 @@ class Message(models.Model):
if message_sudo.model and message_sudo.res_id:
record_sudo = self.env[message_sudo.model].browse(message_sudo.res_id).sudo()
record_name = record_sudo.with_prefetch(thread_ids_by_model_name[message_sudo.model]).display_name
default_subject = record_sudo._message_compute_subject()
# if not a thread, display_name is the default subject
default_subject = record_sudo._message_compute_subject() if hasattr(record_sudo, '_message_compute_subject') else record_name
else:
record_name = False
default_subject = False
+12 -10
View File
@@ -97,16 +97,18 @@ class TestMessageValues(MailCommon):
@mute_logger('odoo.models.unlink')
def test_mail_message_format(self):
record1 = self.env['mail.test.simple'].create({'name': 'Test1'})
message = self.env['mail.message'].create([{
'model': 'mail.test.simple',
'res_id': record1.id,
}])
res = message.message_format()
self.assertEqual(res[0].get('record_name'), 'Test1')
record1.write({"name": "Test2"})
res = message.message_format()
self.assertEqual(res[0].get('record_name'), 'Test2')
record2 = self.env['mail.test.nothread'].create({'name': 'Test2'})
messages = self.env['mail.message'].create([{
'model': record._name,
'res_id': record.id,
} for record in [record1, record2]])
for message, record in zip(messages, [record1, record2]):
with self.subTest(record=record):
formatted = message.message_format()[0]
self.assertEqual(formatted['record_name'], record.name)
record.write({'name': 'Just Test'})
formatted = message.message_format()[0]
self.assertEqual(formatted['record_name'], 'Just Test')
@mute_logger('odoo.models.unlink')
def test_mail_message_format_access(self):
@@ -403,6 +403,29 @@ class TestDiscuss(MailCommon, TestRecipients):
class TestNoThread(MailCommon, TestRecipients):
""" Specific tests for cross models thread features """
@users('employee')
def test_message_format(self):
""" Test formatting of messages when linked to non-thread models.
Format could be asked notably if an inbox notification due to a
'message_notify' happens. """
test_record = self.env['mail.test.nothread'].create({
'customer_id': self.partner_1.id,
'name': 'Not A Thread',
})
message = self.env['mail.message'].create({
'model': test_record._name,
'record_name': 'Not used in message_format',
'res_id': test_record.id,
})
formatted = message.message_format()[0]
self.assertEqual(formatted['default_subject'], test_record.name)
self.assertEqual(formatted['record_name'], test_record.name)
test_record.write({'name': 'Just Test'})
formatted = message.message_format()[0]
self.assertEqual(formatted['default_subject'], 'Just Test')
self.assertEqual(formatted['record_name'], 'Just Test')
@users('employee')
def test_message_notify(self):
""" Test notifying on non-thread models, using MailThread as an abstract