[IMP] base: Forbid spaces in external IDs
This introduces a new constraint to the `ir.model.data` model, to disallow external IDs containing spaces. Those cases may cause issues and should be avoided. For more information about why an external ID containing spaces is a bad idea, see: - https://github.com/odoo/odoo/commit/3c568aec0b46 - https://github.com/odoo/odoo/issues/25408 closes odoo/odoo#38139 Signed-off-by: Xavier Morel (xmo) <xmo@odoo.com>
This commit is contained in:
@@ -1561,6 +1561,11 @@ class IrModelData(models.Model):
|
||||
date_init = fields.Datetime(string='Init Date', default=fields.Datetime.now)
|
||||
reference = fields.Char(string='Reference', compute='_compute_reference', readonly=True, store=False)
|
||||
|
||||
_sql_constraints = [
|
||||
('name_nospaces', "CHECK(name NOT LIKE '% %')",
|
||||
"External IDs cannot contain spaces"),
|
||||
]
|
||||
|
||||
@api.depends('module', 'name')
|
||||
def _compute_complete_name(self):
|
||||
for res in self:
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from psycopg2 import IntegrityError
|
||||
|
||||
from odoo.tests.common import TransactionCase
|
||||
from odoo.tools import mute_logger
|
||||
|
||||
|
||||
class TestXMLID(TransactionCase):
|
||||
@@ -155,3 +158,13 @@ class TestXMLID(TransactionCase):
|
||||
|
||||
self.assertEqual(self.get_data('test_convert.foo').noupdate, True)
|
||||
self.assertEqual(self.get_data('test_convert.bar').noupdate, True)
|
||||
|
||||
@mute_logger('odoo.sql_db', 'odoo.addons.base.models.ir_model')
|
||||
def test_create_external_id_with_space(self):
|
||||
model = self.env['res.partner.category']
|
||||
data_list = [{
|
||||
'xml_id': 'test_convert.category_with space',
|
||||
'values': {'name': 'Bar'},
|
||||
}]
|
||||
with self.assertRaisesRegex(IntegrityError, 'ir_model_data_name_nospaces'):
|
||||
model._load_records(data_list)
|
||||
|
||||
Reference in New Issue
Block a user