Skip to content

Commit 1c4642f

Browse files
diagnozaKangOl
authored andcommitted
[FIX] util/records: fix update from xml of menus
Updating menus that are a child of another menu and that do not have the field `parent_id`, would result in the loss of the link between the menu and its parent. In order to retain this link, we obtain the value for `parent_id` and then explicitly inject this field to the source xml. Part of odoo/upgrade#5163 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent b3823b0 commit 1c4642f

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/base/tests/test_util.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,23 @@ def test_update_record_from_xml(self):
625625
for key, value in data_before.items():
626626
self.assertEqual(record[key], value)
627627

628+
# reset all fields on a <menuitem>
629+
xmlid = "base.menu_security"
630+
data_after = {"name": "ATotallyValidSecurityMenu", "sequence": 112, "parent_id": self.env["ir.ui.menu"]}
631+
record = self.env.ref(xmlid)
632+
data_before = {key: record[key] for key in data_after}
633+
for key, value in data_after.items():
634+
record.write({key: value})
635+
self.assertEqual(record[key], value)
636+
637+
util.update_record_from_xml(self.env.cr, xmlid, ensure_references=True)
638+
if util.version_gte("16.0"):
639+
record.invalidate_recordset(["name"])
640+
else:
641+
record.invalidate_cache(["name"], record.ids)
642+
for key, value in data_before.items():
643+
self.assertEqual(record[key], value)
644+
628645
# reset all fields on a <template>
629646
template_xmlid = "base.contact_name"
630647
record = self.env.ref(template_xmlid)

src/util/records.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,18 @@ def add_ref(ref):
770770
with file_open(os.path.join(from_module, f)) as fp:
771771
doc = lxml.etree.parse(fp)
772772
for node in doc.xpath(xpath):
773+
parent = node.getparent()
773774
new_root[0].append(node)
775+
776+
if node.tag == "menuitem" and parent.tag == "menuitem" and "parent_id" not in node.attrib:
777+
new_root[0].append(
778+
lxml.builder.E.record(
779+
lxml.builder.E.field(name="parent_id", ref=parent.attrib["id"]),
780+
model="ir.ui.menu",
781+
id=node.attrib["id"],
782+
)
783+
)
784+
774785
if node.tag == "template":
775786
template = True
776787
if ensure_references:

0 commit comments

Comments
 (0)