[FIX] mail: Clean residual context keys on _message_track_post_template
Purpose ======= Clean the context to get rid of residual default_* keys that could cause issues afterward during the mail.message generation. Example: 'default_parent_id' would refer to the parent_id of the current record that was used during its creation, but could refer to wrong parent message id, leading to a traceback in case the related message_id doesn't exist closes odoo/odoo#43830 Taskid: 2176445 X-original-commit: 82d2d581a2590a9c782cbc71b5005a4198b70d77 Signed-off-by: Yannick Tivisse (yti) <yti@odoo.com>
This commit is contained in:
@@ -589,6 +589,14 @@ class MailThread(models.AbstractModel):
|
||||
def _message_track_post_template(self, changes):
|
||||
if not changes:
|
||||
return True
|
||||
# Clean the context to get rid of residual default_* keys
|
||||
# that could cause issues afterward during the mail.message
|
||||
# generation. Example: 'default_parent_id' would refer to
|
||||
# the parent_id of the current record that was used during
|
||||
# its creation, but could refer to wrong parent message id,
|
||||
# leading to a traceback in case the related message_id
|
||||
# doesn't exist
|
||||
self = self.with_context(clean_context(self._context))
|
||||
templates = self._track_template(changes)
|
||||
for field_name, (template, post_kwargs) in templates.items():
|
||||
if not template:
|
||||
|
||||
@@ -206,3 +206,52 @@ class TestTracking(TestMailCommon):
|
||||
new_partner = Partner.search([('email', '=', email_new_partner)])
|
||||
self.assertTrue(new_partner)
|
||||
self.assertEqual(new_partner.company_id, company1)
|
||||
|
||||
def test_track_template(self):
|
||||
# Test: Check that default_* keys are not taken into account in _message_track_post_template
|
||||
magic_code = 'Up-Up-Down-Down-Left-Right-Left-Right-Square-Triangle'
|
||||
|
||||
mt_name_changed = self.env['mail.message.subtype'].create({
|
||||
'name': 'MAGIC CODE WOOP WOOP',
|
||||
'description': 'SPECIAL CONTENT UNLOCKED'
|
||||
})
|
||||
self.env['ir.model.data'].create({
|
||||
'name': 'mt_name_changed',
|
||||
'model': 'mail.message.subtype',
|
||||
'module': 'mail',
|
||||
'res_id': mt_name_changed.id
|
||||
})
|
||||
mail_template = self.env['mail.template'].create({
|
||||
'name': 'SPECIAL CONTENT UNLOCKED',
|
||||
'subject': 'SPECIAL CONTENT UNLOCKED',
|
||||
'model_id': self.env.ref('test_mail.model_mail_test').id,
|
||||
'auto_delete': True,
|
||||
'body_html': '''<div>WOOP WOOP</div>''',
|
||||
})
|
||||
|
||||
def _track_subtype(self, init_values):
|
||||
if 'name' in init_values and init_values['name'] == magic_code:
|
||||
return 'mail.mt_name_changed'
|
||||
return False
|
||||
self.registry('mail.test')._patch_method('_track_subtype', _track_subtype)
|
||||
|
||||
def _track_template(self, changes):
|
||||
res = {}
|
||||
if 'name' in changes:
|
||||
res['name'] = (mail_template, {'composition_mode': 'mass_mail'})
|
||||
return res
|
||||
self.registry('mail.test')._patch_method('_track_template', _track_template)
|
||||
|
||||
cls = type(self.env['mail.test'])
|
||||
self.assertFalse(hasattr(getattr(cls, 'name'), 'track_visibility'))
|
||||
getattr(cls, 'name').track_visibility = 'always'
|
||||
|
||||
@self.addCleanup
|
||||
def cleanup():
|
||||
del getattr(cls, 'name').track_visibility
|
||||
|
||||
test_mail_record = self.env['mail.test'].create({
|
||||
'name': 'Zizizatestmailname',
|
||||
'description': 'Zizizatestmaildescription',
|
||||
})
|
||||
test_mail_record.with_context(default_parent_id=2147483647).write({'name': magic_code})
|
||||
|
||||
Reference in New Issue
Block a user