Skip to content

Commit

Permalink
shopinvader: Fix product url computation triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
mmequignon committed May 17, 2024
1 parent 0b031e8 commit 59983ce
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions shopinvader/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
from . import shopinvader_variant
from . import res_config_settings
from . import res_partner
from . import ir_translation
37 changes: 37 additions & 0 deletions shopinvader/models/ir_translation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2024 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from itertools import groupby

from odoo import models


class IrTranslation(models.Model):
_inherit = "ir.translation"

def write(self, vals):
# Because updating translations from the wizard writes directly on
# ir.translation, updating a product's name translation does not trigger
# the computation of the url.
# To fix that, adding a hook here, filtering as soon and as much as possible
# translation records in order to avoid too much extra cost.
res = super().write(vals)
tmpl_name_records = self.filtered(
lambda record: record.name == "product.template,name"
)
if not tmpl_name_records:
return res
sorted_records = tmpl_name_records.sorted(lambda record: record.lang)
translation_groups = groupby(sorted_records, key=lambda record: record.lang)
for lang_name, translation_list in translation_groups:
lang = self.env["res.lang"]._lang_get(lang_name)
if not lang:
continue
templates = self.env["product.template"].browse(
[translation.res_id for translation in translation_list]
)
template_bindings = templates.shopinvader_bind_ids.filtered(
lambda tmpl: tmpl.lang_id == lang
)
template_bindings._compute_automatic_url_key()
return res
24 changes: 24 additions & 0 deletions shopinvader/tests/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,30 @@ def test_main_product_multi_language(self):
for shopinvader_variant in prod.shopinvader_bind_ids:
self.assertTrue(shopinvader_variant.main)

def test_translation_update_url(self):
lang_fr = self._install_lang("base.lang_fr")
lang_en = self.env.ref("base.lang_en")
self.backend.lang_ids |= lang_fr
product = self.env.ref("product.product_product_4")
product.name = "customizable desk"
product.with_context(lang=lang_fr.code).name = "bureau modifiable"
binding_fr = product.shopinvader_bind_ids.filtered(
lambda bind: bind.lang_id == lang_fr
)
binding_en = product.shopinvader_bind_ids.filtered(
lambda bind: bind.lang_id == lang_en
)
self.assertEqual(binding_en.automatic_url_key, "customizable-desk")
self.assertEqual(binding_fr.automatic_url_key, "bureau-modifiable")
translation_name_fr = self.env["ir.translation"].search(
[("name", "=", "product.template,name"), ("lang", "=", lang_fr.code)]
)
self.assertEqual(translation_name_fr.value, "bureau modifiable")
product.with_context(lang=lang_fr.code).name = "bureau customisable"
binding_fr.invalidate_cache()
product.invalidate_cache()
self.assertEqual(binding_fr.automatic_url_key, "bureau-customisable")

def test_create_shopinvader_category_from_product_category(self):
categ = self.env["product.category"].search([])[0]
lang = self.env["res.lang"]._lang_get(self.env.user.lang)
Expand Down

0 comments on commit 59983ce

Please sign in to comment.