[FIX] odoo/fields.py: fix removing translations on writing ''
cache_value could be empty string rather than None. I'm not sure why it was
different before.
STEPS:
* install an app that adds menu Products (e.g. Sales)
* activate second language and switch to it
* create new product, set some value to field description ("Internal Notes"),
save
* click edit, remove description, save
closes odoo/odoo#64752
Before: the value is not removed
After: value is empty, translations are removed
X-original-commit: 3ef5e63f62f0f84d6e92cb55eb112ab2f2924faa
Signed-off-by: Ivan Yelizariev // IEL <yelizariev@users.noreply.github.com>
This commit is contained in:
@@ -503,7 +503,13 @@ class TestTranslationWrite(TransactionCase):
|
||||
{'src': 'None Name', 'value': 'French Name', 'lang': 'fr_FR'},
|
||||
])
|
||||
|
||||
def test_05_remove_multi(self):
|
||||
def test_05_remove_multi_empty_string(self):
|
||||
self._test_05_remove_multi("")
|
||||
|
||||
def test_05_remove_multi_false(self):
|
||||
self._test_05_remove_multi(False)
|
||||
|
||||
def _test_05_remove_multi(self, empty_value):
|
||||
self.env['res.lang']._activate_lang('fr_FR')
|
||||
|
||||
langs = self.env['res.lang'].get_installed()
|
||||
@@ -513,39 +519,27 @@ class TestTranslationWrite(TransactionCase):
|
||||
belgium = self.env.ref('base.be')
|
||||
# vat_label is translatable and not required
|
||||
belgium.with_context(lang='en_US').write({'vat_label': 'VAT'})
|
||||
belgium.with_context(lang='fr_FR').write({'vat_label': 'TVA'})
|
||||
|
||||
# create translations if not exists
|
||||
self.env['ir.translation']._upsert_translations([{
|
||||
'type': 'model',
|
||||
'name': 'res.country,vat_label',
|
||||
'lang': 'en_US',
|
||||
'res_id': belgium.id,
|
||||
'src': 'VAT',
|
||||
'value': 'VAT',
|
||||
'state': 'translated',
|
||||
}, {
|
||||
'type': 'model',
|
||||
'name': 'res.country,vat_label',
|
||||
'lang': 'fr_FR',
|
||||
'res_id': belgium.id,
|
||||
'src': 'VAT',
|
||||
'value': 'TVA',
|
||||
'state': 'translated',
|
||||
}])
|
||||
translations = self.env['ir.translation'].search([
|
||||
('name', '=', 'res.country,vat_label'),
|
||||
('res_id', '=', belgium.id),
|
||||
])
|
||||
self.assertEqual(len(translations), 2, "Translations are not created")
|
||||
|
||||
# remove the value
|
||||
belgium.with_context(lang='fr_FR').write({'vat_label': False})
|
||||
belgium.with_context(lang='fr_FR').write({'vat_label': empty_value})
|
||||
# should recover the initial value from db
|
||||
self.assertEqual(
|
||||
False, belgium.with_context(lang='fr_FR').vat_label,
|
||||
self.assertFalse(
|
||||
belgium.with_context(lang='fr_FR').vat_label,
|
||||
"Value was not reset"
|
||||
)
|
||||
self.assertEqual(
|
||||
False, belgium.with_context(lang='en_US').vat_label,
|
||||
self.assertFalse(
|
||||
belgium.with_context(lang='en_US').vat_label,
|
||||
"Value was not reset in other languages"
|
||||
)
|
||||
self.assertEqual(
|
||||
False, belgium.with_context(lang=None).vat_label,
|
||||
self.assertFalse(
|
||||
belgium.with_context(lang=None).vat_label,
|
||||
"Value was not reset on the field model"
|
||||
)
|
||||
|
||||
|
||||
+3
-3
@@ -1505,7 +1505,7 @@ class _String(Field):
|
||||
update_trans = True
|
||||
elif lang != 'en_US' and lang is not None:
|
||||
# update the translations only except if emptying
|
||||
update_column = cache_value is None
|
||||
update_column = not cache_value
|
||||
update_trans = True
|
||||
# else: lang = None
|
||||
|
||||
@@ -1515,7 +1515,7 @@ class _String(Field):
|
||||
for rid in real_recs._ids:
|
||||
# cache_value is already in database format
|
||||
towrite[rid][self.name] = cache_value
|
||||
if self.translate is True and cache_value is not None:
|
||||
if self.translate is True and cache_value:
|
||||
tname = "%s,%s" % (records._name, self.name)
|
||||
records.env['ir.translation']._set_source(tname, real_recs._ids, value)
|
||||
if self.translate:
|
||||
@@ -1538,7 +1538,7 @@ class _String(Field):
|
||||
source_recs[self.name] = value
|
||||
source_value = value
|
||||
tname = "%s,%s" % (self.model_name, self.name)
|
||||
if value is None:
|
||||
if not value:
|
||||
records.env['ir.translation'].search([
|
||||
('name', '=', tname),
|
||||
('type', '=', 'model'),
|
||||
|
||||
Reference in New Issue
Block a user