Skip to content
Permalink
Browse files

[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:
- 3c568ae
- #25408

closes #38139

Signed-off-by: Xavier Morel (xmo) <xmo@odoo.com>
  • Loading branch information...
luisg123v committed Oct 7, 2019
1 parent dbeba8e commit 2b8f5dce061269bca48aad5a54e0f8fa767fe17d
Showing with 18 additions and 0 deletions.
  1. +5 −0 odoo/addons/base/models/ir_model.py
  2. +13 −0 odoo/addons/base/tests/test_ir_model.py
@@ -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 @@ def test_create_xmlids(self):

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)

0 comments on commit 2b8f5dc

Please sign in to comment.
You can’t perform that action at this time.