Skip to content

Commit 1c27407

Browse files
committed
[IMP] util.rename_xmlid : Adapt t-call and t-name References
When renaming a view, we should also update its references in other views where it is used in t-call and t-name attributes. This ensures consistency and prevents broken references in dependent views. closes #116 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent 88dc6a6 commit 1c27407

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/base/tests/test_util.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,3 +1887,39 @@ def test_remove_view(self):
18871887
util.invalidate(test_view_3)
18881888
self.assertFalse(test_view_2.exists())
18891889
self.assertNotIn('t-call="base.test_view_2"', test_view_3.arch_db)
1890+
1891+
1892+
class TestRenameXMLID(UnitTestCase):
1893+
def test_rename_xmlid(self):
1894+
test_view_1 = self.env["ir.ui.view"].create(
1895+
{
1896+
"name": "test_view_1",
1897+
"type": "qweb",
1898+
"key": "base.test_view_1",
1899+
"arch": """
1900+
<t t-name="base.test_view_1">
1901+
<div>Test View 1 Content</div>
1902+
</t>
1903+
""",
1904+
}
1905+
)
1906+
self.env["ir.model.data"].create(
1907+
{"name": "test_view_1", "module": "base", "model": "ir.ui.view", "res_id": test_view_1.id}
1908+
)
1909+
test_view_2 = self.env["ir.ui.view"].create(
1910+
{
1911+
"name": "test_view_2",
1912+
"type": "qweb",
1913+
"key": "base.test_view_2",
1914+
"arch": """
1915+
<t t-name="base.test_view_2">
1916+
<t t-call="base.test_view_1"/>
1917+
<div>Test View 2 Content</div>
1918+
</t>
1919+
""",
1920+
}
1921+
)
1922+
util.rename_xmlid(self.env.cr, "base.test_view_1", "base.rename_view")
1923+
util.invalidate(test_view_2)
1924+
self.assertIn('t-call="base.rename_view"', test_view_2.arch_db)
1925+
self.assertIn('t-name="base.rename_view"', test_view_1.arch_db)

src/util/records.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,34 @@ def rename_xmlid(cr, old, new, noupdate=None, on_collision="fail"):
804804
# Don't change the view keys unconditionally to avoid changing unrelated views.
805805
cr.execute("UPDATE ir_ui_view SET key = %s WHERE key = %s", [new, old])
806806

807+
# Adapting t-call and t-name references in views
808+
search_pattern = r"""\yt-(call|name)=(["']){}\2""".format(re.escape(old))
809+
replace_pattern = r"t-\1=\2{}\2".format(new)
810+
if version_gte("16.0"):
811+
arch_col = get_value_or_en_translation(cr, "ir_ui_view", "arch_db")
812+
replace_in_all_jsonb_values(
813+
cr,
814+
"ir_ui_view",
815+
"arch_db",
816+
PGRegexp(search_pattern),
817+
replace_pattern,
818+
extra_filter=cr.mogrify("{} ~ %s".format(arch_col), [search_pattern]).decode(),
819+
)
820+
else:
821+
arch_col = "arch_db" if column_exists(cr, "ir_ui_view", "arch_db") else "arch"
822+
cr.execute(
823+
format_query(
824+
cr,
825+
"""
826+
UPDATE ir_ui_view
827+
SET {arch} = regexp_replace({arch}, %s, %s, 'g')
828+
WHERE {arch} ~ %s
829+
""",
830+
arch=arch_col,
831+
),
832+
[search_pattern, replace_pattern, search_pattern],
833+
)
834+
807835
if model == "ir.ui.menu" and column_exists(cr, "res_users_settings", "homemenu_config"):
808836
query = """
809837
UPDATE res_users_settings

0 commit comments

Comments
 (0)