Skip to content

Commit

Permalink
[FIX] core: improve cache consistency of translated fields
Browse files Browse the repository at this point in the history
Co-authored-by: Christophe Simonis <[email protected]>
  • Loading branch information
rco-odoo and KangOl committed Apr 9, 2020
1 parent da0d9ef commit 98ff0ef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
25 changes: 18 additions & 7 deletions odoo/addons/base/tests/test_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ def test_106_en_us_translation(self):
self.assertEqual(cheese.with_context(lang='fr_FR').name, 'Fromage')
self.assertEqual(cheese.with_context(lang='en_US').name, 'The Cheese')
cheese.flush()
cheese.invalidate_cache()

# set a new master value
cheese.with_context(lang='en_US').write({'name': 'Delicious Cheese'})
Expand Down Expand Up @@ -683,7 +682,6 @@ def test_sync(self):
# modify source term in view (fixed type in 'cheeze')
terms_en = ('Bread and cheese',)
view.with_env(env_en).write({'arch_db': archf % terms_en})
view.invalidate_cache(fnames=['arch_db'], ids=view.ids)

# check whether translations have been synchronized
self.assertEqual(view.with_env(env_nolang).arch_db, archf % terms_en)
Expand All @@ -695,7 +693,6 @@ def test_sync(self):
# modify source term in view in another language with close term
new_terms_fr = ('Pains et fromage',)
view.with_env(env_fr).write({'arch_db': archf % new_terms_fr})
view.invalidate_cache(fnames=['arch_db'], ids=view.ids)

# check whether translations have been synchronized
self.assertEqual(view.with_env(env_nolang).arch_db, archf % new_terms_fr)
Expand All @@ -704,7 +701,7 @@ def test_sync(self):
self.assertEqual(view.with_env(env_nl).arch_db, archf % terms_nl)

def test_sync_update(self):
""" Check translations after minor change in source terms. """
""" Check translations after major changes in source terms. """
archf = '<form string="X"><div>%s</div><div>%s</div></form>'
terms_src = ('Subtotal', 'Subtotal:')
terms_en = ('', 'Sub total:')
Expand All @@ -718,9 +715,8 @@ def test_sync_update(self):
self.assertEqual(len(translations), 2)

# modifying the arch should sync existing translations without errors
view.write({
"arch": archf % ('Subtotal', 'Subtotal:<br/>')
})
new_arch = archf % ('Subtotal', 'Subtotal:<br/>')
view.write({"arch_db": new_arch})

translations = self.env['ir.translation'].search([
('type', '=', 'model_terms'),
Expand All @@ -730,3 +726,18 @@ def test_sync_update(self):
# 'Subtotal' being src==value, it will be discared
# 'Subtotal:' will be discarded as it match 'Subtotal' instead of 'Subtotal:<br/>'
self.assertEqual(len(translations), 0)

def test_cache_consistency(self):
view = self.env["ir.ui.view"].create({
"name": "test_translate_xml_cache_invalidation",
"model": "res.partner",
"arch": "<form><b>content</b></form>",
})
view_fr = view.with_context({"lang": "fr_FR"})
self.assertIn("<b>", view.arch_db)
self.assertIn("<b>", view_fr.arch_db)

# write with no lang, and check consistency in other languages
view.write({"arch_db": "<form><i>content</i></form>"})
self.assertIn("<i>", view.arch_db)
self.assertIn("<i>", view_fr.arch_db)
5 changes: 4 additions & 1 deletion odoo/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1443,7 +1443,10 @@ def write(self, records, value):
if self.translate is True and cache_value is not None:
tname = "%s,%s" % (records._name, self.name)
records.env['ir.translation']._set_source(tname, real_recs._ids, value)
records.invalidate_cache(fnames=[self.name], ids=records.ids)
if self.translate:
# invalidate the field in the other languages
cache.invalidate([(self, records.ids)])
cache.update(records, self, [cache_value] * len(records))

if update_trans:
if callable(self.translate):
Expand Down

0 comments on commit 98ff0ef

Please sign in to comment.