[FIX] base: deactivate default language

When a language is deactivated, it may still be referred to in a default value.
A ir.value is created at the database setup to specify the default language.
If a user deactivate the default language but not the ir.value, the creation of
a res.partner would fail as trying to set an unknown value to a selection field.

This commit removes the default value when deactivating the linked language.

The patch is minimal for the bug. A more generic solution would be to verify the
ir.value when calling the default_get method.
This is however a risk of performance degradation (e.g. during a csv import) for
a rare scenario.
The dynamic selection scenario is probably on relevant to res.lang.
A similar problem can occur with many2one fields but is difficult to fix without
performance degradation.

Closes #16009
Fixes #15979
Fixes #3854
This commit is contained in:
Martin Trigaux
2017-03-23 14:38:05 +01:00
parent 1031a699b7
commit 71d86afb58
+10 -2
View File
@@ -216,8 +216,16 @@ class Lang(models.Model):
lang_codes = self.mapped('code')
if 'code' in vals and any(code != vals['code'] for code in lang_codes):
raise UserError(_("Language code cannot be modified."))
if vals.get('active') == False and self.env['res.users'].search([('lang', 'in', lang_codes)]):
raise UserError(_("Cannot unactivate a language that is currently used by users."))
if vals.get('active') == False:
if self.env['res.users'].search([('lang', 'in', lang_codes)]):
raise UserError(_("Cannot unactivate a language that is currently used by users."))
# delete linked ir.value specifying default partner's language
default_lang = self.env['ir.values'].search([
('key', '=', 'default'),
('name', '=', 'lang'),
('model', '=', 'res.partner')])
if default_lang and default_lang.value_unpickle in lang_codes:
default_lang.unlink()
self.clear_caches()
return super(Lang, self).write(vals)