Skip to content

Commit 2e2137d

Browse files
committed
[ADD] util.inherits_parent
Function that yield inherit*s* parent and the delegate m2o field.
1 parent e01bb0d commit 2e2137d

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ extend-ignore-re = [
138138
[tool.typos.default.extend-identifiers]
139139
inh = "inh"
140140
_inh = "_inh"
141+
grand_inh = "grand_inh"
141142
ressource_type_id = "ressource_type_id"
142143
# Used as alias in SQL queries.
143144
fpt = "fpt"

src/base/tests/test_util.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,12 @@ def test_direct_inherit_parents(self):
10921092
self.assertTrue(all(inh.model == "product.product" for inh in inhs))
10931093
self.assertEqual([inh.via for inh in inhs], [None, None, "product_tmpl_id"])
10941094

1095+
def test_inherits_parents(self):
1096+
self.assertEqual(
1097+
list(util.inherits_parent(self.env.cr, "crm.team")),
1098+
[("mail.alias", "alias_id")],
1099+
)
1100+
10951101

10961102
class TestNamedCursors(UnitTestCase):
10971103
@staticmethod

src/util/inherit.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def for_each_inherit(cr, model, skip=(), interval="[)"):
9595

9696

9797
def direct_inherit_parents(cr, model, skip=(), interval="[)"):
98-
"""Yield the *direct* inherits parents."""
98+
"""Yield the *direct* inherit parents."""
9999
if skip == "*":
100100
return
101101
skip = set(skip)
@@ -122,3 +122,19 @@ def inherit_parents(cr, model, skip=(), interval="[)"):
122122
for grand_parent in inherit_parents(cr, parent, skip=skip, interval=interval):
123123
yield grand_parent
124124
skip.add(grand_parent)
125+
126+
127+
def inherits_parents(cr, model, skip=(), interval="[)"):
128+
"""Yield the inherit*s* parents."""
129+
if skip == "*":
130+
return
131+
skip = set(skip)
132+
133+
for parent, inh in direct_inherit_parents(cr, model, skip, interval):
134+
if inh.via:
135+
yield parent, inh.via
136+
skip.add(parent)
137+
else:
138+
for grand_parent, grand_inh in inherits_parents(cr, parent, skip, interval):
139+
yield grand_parent, grand_inh.via
140+
skip.add(grand_parent)

0 commit comments

Comments
 (0)