diff --git a/addons/mail/models/mail_template.py b/addons/mail/models/mail_template.py index a72cad177db..3de352dad0b 100644 --- a/addons/mail/models/mail_template.py +++ b/addons/mail/models/mail_template.py @@ -6,7 +6,7 @@ import itertools import logging from odoo import _, api, fields, models, tools, Command -from odoo.exceptions import UserError +from odoo.exceptions import ValidationError, UserError from odoo.tools import is_html_empty from odoo.tools.safe_eval import safe_eval, time @@ -146,12 +146,22 @@ class MailTemplate(models.Model): record.attachment_ids.write({'res_model': record._name, 'res_id': record.id}) return self + def _check_abstract_models(self, vals_list): + model_names = self.sudo().env['ir.model'].browse(filter(None, ( + vals.get('model_id') for vals in vals_list + ))).mapped('model') + for model in model_names: + if self.env[model]._abstract: + raise ValidationError(_('You may not define a template on an abstract model: %s', model)) + @api.model_create_multi def create(self, vals_list): + self._check_abstract_models(vals_list) return super().create(vals_list)\ ._fix_attachment_ownership() def write(self, vals): + self._check_abstract_models([vals]) super().write(vals) self._fix_attachment_ownership() return True diff --git a/addons/mail/tests/test_mail_template.py b/addons/mail/tests/test_mail_template.py index 4d26f315b80..e0d4f9133cf 100644 --- a/addons/mail/tests/test_mail_template.py +++ b/addons/mail/tests/test_mail_template.py @@ -3,7 +3,7 @@ from markupsafe import Markup from odoo.addons.mail.tests.common import MailCommon -from odoo.exceptions import AccessError, UserError +from odoo.exceptions import AccessError, ValidationError, UserError from odoo.modules.module import get_module_resource from odoo.tests import Form, tagged, users from odoo.tools import convert_file @@ -50,6 +50,26 @@ class TestMailTemplate(MailCommon): self.assertEqual(values[self.partner_employee.id]['subject'], '6', 'We must trust mail template values') self.assertIn('13', values[self.partner_employee.id]['body_html'], 'We must trust mail template values') + @users('admin') + def test_mail_template_abstract_model(self): + """Check abstract models cannot be set on templates.""" + # create + with self.assertRaises(ValidationError), self.cr.savepoint(): + self.env['mail.template'].create({ + 'name': 'Test abstract template', + 'model_id': self.env['ir.model']._get('mail.thread').id, # abstract model + }) + # write + template = self.env['mail.template'].create({ + 'name': 'Test abstract template', + 'model_id': self.env['ir.model']._get('res.partner').id, + }) + with self.assertRaises(ValidationError), self.cr.savepoint(): + template.write({ + 'name': 'Test abstract template', + 'model_id': self.env['ir.model']._get('mail.thread').id, + }) + def test_mail_template_acl(self): # Sanity check self.assertTrue(self.user_admin.has_group('mail.group_mail_template_editor'))