[FIX] mail: exclude abstract models from templates
Templates could previously be created for abstract models. The methods are not written with that in mind and most useful ones will raise an exception when calling them on that template. task-3162320 X-original-commit: 0d4b473ee54d2376f4f71efd864aa0e16822d4ba Part-of: odoo/odoo#118710
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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'))
|
||||
|
||||
Reference in New Issue
Block a user