From 23c81ec015344255f56da3a005a416094013207f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 30 Jan 2019 09:36:21 +0100 Subject: [PATCH] Avoid to store in transactional cache former translation value Translated fields are stored in the transactional cache. Using the instance to get the source value when it is being set, store the former value into the cache and the source. issue7997 review58471002 --- trytond/ir/translation.py | 19 +++++++++++-------- trytond/tests/test_field_char.py | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/trytond/ir/translation.py b/trytond/ir/translation.py index 4f01863831..3e2e612abf 100644 --- a/trytond/ir/translation.py +++ b/trytond/ir/translation.py @@ -471,12 +471,16 @@ class Translation(ModelSQL, ModelView): to_save = [] for record, value in zip(records, values): translation = translations.get(get_name(record)) + if lang == 'en': + src = value + else: + src = getattr(record, field_name) if not translation: translation = cls() translation.name = name translation.lang = lang translation.type = ttype - translation.src = getattr(record, field_name) + translation.src = src translation.value = value translation.fuzzy = False to_save.append(translation) @@ -512,6 +516,10 @@ class Translation(ModelSQL, ModelView): to_save = [] for record, value in zip(records, values): translation = translations.get(record.id) + if lang == Config.get_language(): + src = value + else: + src = getattr(record, field_name) if not translation: translation = cls() translation.name = name @@ -519,19 +527,14 @@ class Translation(ModelSQL, ModelView): translation.type = ttype translation.res_id = record.id else: - cls.write([translation], { - 'value': value, - 'src': getattr(record, field_name), - 'fuzzy': False, - }) other_langs = other_translations.get(record.id) if other_langs: for other_lang in other_langs: - other_lang.src = getattr(record, field_name) + other_lang.src = src other_lang.fuzzy = True to_save.append(other_lang) translation.value = value - translation.src = getattr(record, field_name) + translation.src = src translation.fuzzy = False to_save.append(translation) cls.save(to_save) diff --git a/trytond/tests/test_field_char.py b/trytond/tests/test_field_char.py index d70a9b8a52..4497575c35 100644 --- a/trytond/tests/test_field_char.py +++ b/trytond/tests/test_field_char.py @@ -453,6 +453,24 @@ class FieldCharTranslatedTestCase(unittest.TestCase, CommonTestCaseMixin): def Char(self): return Pool().get('test.char_translate') + @with_transaction() + def test_translation_default_language_cache(self): + """Test set translation for default language does not fill transactional + cache with former value""" + pool = Pool() + Config = pool.get('ir.configuration') + Char = self.Char() + + with Transaction().set_context(language=Config.get_language()): + char, = Char.create([{ + 'char': "foo", + }]) + + char.char = "bar" + char.save() + + self.assertEqual(char.char, "bar") + class UnaccentedTestCase(unittest.TestCase): @classmethod