Skip to content

Commit a08b627

Browse files
committed
fixup! [REF] util.helpers: introduce new resolve_model_fields_path helper
1 parent 82aa584 commit a08b627

File tree

3 files changed

+32
-40
lines changed

3 files changed

+32
-40
lines changed

src/base/tests/test_util.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -694,19 +694,16 @@ def test_resolve_model_fields_path(self):
694694
# test with provided paths
695695
model, path = "res.currency", ["rate_ids", "company_id", "user_ids", "partner_id"]
696696
expected_result = [
697-
FieldsPathPart(model, path, 1, "res.currency", "rate_ids", "res.currency.rate"),
698-
FieldsPathPart(model, path, 2, "res.currency.rate", "company_id", "res.company"),
699-
FieldsPathPart(model, path, 3, "res.company", "user_ids", "res.users"),
700-
FieldsPathPart(model, path, 4, "res.users", "partner_id", "res.partner"),
697+
FieldsPathPart("res.currency", "rate_ids", "res.currency.rate"),
698+
FieldsPathPart("res.currency.rate", "company_id", "res.company"),
699+
FieldsPathPart("res.company", "user_ids", "res.users"),
700+
FieldsPathPart("res.users", "partner_id", "res.partner"),
701701
]
702702
result = resolve_model_fields_path(cr, model, path)
703703
self.assertEqual(result, expected_result)
704704

705705
model, path = "res.users", ("partner_id", "removed_field", "user_id")
706-
expected_result = [
707-
FieldsPathPart(model, list(path), 1, "res.users", "partner_id", "res.partner"),
708-
FieldsPathPart(model, list(path), 2, "res.partner", "removed_field", None),
709-
]
706+
expected_result = [FieldsPathPart("res.users", "partner_id", "res.partner")]
710707
result = resolve_model_fields_path(cr, model, path)
711708
self.assertEqual(result, expected_result)
712709

src/util/domains.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,9 @@ def _model_of_path(cr, model, path):
164164
return model
165165
path = tuple(path)
166166
resolved_parts = resolve_model_fields_path(cr, model, path)
167-
if not resolved_parts:
168-
return None
169-
last_part = resolved_parts[-1]
170-
if last_part.part_index != len(last_part.path):
171-
return None
172-
return last_part.relation_model # could be None
167+
if len(resolved_parts) == len(path):
168+
return resolved_parts[-1].relation_model
169+
return None
173170

174171

175172
def _valid_path_to(cr, path, from_, to):

src/util/helpers.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,10 @@ def _get_theme_models():
217217
}
218218

219219

220-
FieldsPathPart = namedtuple(
221-
"FieldsPathPart",
222-
"model path part_index field_model field_name relation_model",
223-
)
220+
FieldsPathPart = namedtuple("FieldsPathPart", "field_model field_name relation_model")
224221
FieldsPathPart.__doc__ = """
225222
Encapsulate information about a field within a fields path.
226223
227-
:param str model: model to resolve the fields ``path`` from
228-
:param typing.Sequence[str] path: fields path starting from ``model``
229-
:param int part_index: index of this field in ``path``
230224
:param str field_model: model of the field
231225
:param str field_name: name of the field
232226
:param str relation_model: target model of the field, if relational, otherwise ``None``
@@ -239,8 +233,12 @@ def resolve_model_fields_path(cr, model, path):
239233
"""
240234
Resolve model fields paths.
241235
242-
This function returns a list of :class:`~odoo.upgrade.util.helpers.FieldsPathPart` where
243-
each item describes the field in ``path`` (in the same order).
236+
This function returns a list of :class:`~odoo.upgrade.util.helpers.FieldsPathPart`
237+
where each item describes a field in ``path`` (in the same order). The returned list
238+
could be shorter than the original ``path`` due to a missing field or model, or
239+
because there is a non-relational field in the path. The only non-relational field
240+
allowed in a fields path is the last one, in which case the returned list has the same
241+
length as the input ``path``.
244242
245243
.. example::
246244
@@ -261,35 +259,35 @@ def resolve_model_fields_path(cr, model, path):
261259
"""
262260
WITH RECURSIVE resolved_fields_path AS (
263261
-- non-recursive term
264-
SELECT p.model AS model,
265-
p.path AS path,
266-
1 AS part_index,
267-
p.model AS field_model,
268-
p.path[1] AS field_name,
269-
imf.relation AS relation_model
262+
SELECT imf.model AS field_model,
263+
imf.name AS field_name,
264+
imf.relation AS relation_model,
265+
p.path AS path,
266+
1 AS part_index
270267
FROM (VALUES (%(model)s, %(path)s)) p(model, path)
271-
LEFT JOIN ir_model_fields imf
268+
JOIN ir_model_fields imf
272269
ON imf.model = p.model
273270
AND imf.name = p.path[1]
274271
275272
UNION ALL
276273
277274
-- recursive term
278-
SELECT rfp.model,
279-
rfp.path,
280-
rfp.part_index + 1 AS part_index,
281-
rfp.relation_model AS field_model,
282-
rfp.path[rfp.part_index + 1] AS field_name,
283-
rimf.relation AS relation_model
275+
SELECT rimf.model AS field_model,
276+
rimf.name AS field_name,
277+
rimf.relation AS relation_model,
278+
rfp.path AS path,
279+
rfp.part_index + 1 AS part_index
284280
FROM resolved_fields_path rfp
285-
LEFT JOIN ir_model_fields rimf
281+
JOIN ir_model_fields rimf
286282
ON rimf.model = rfp.relation_model
287283
AND rimf.name = rfp.path[rfp.part_index + 1]
288284
WHERE cardinality(rfp.path) > rfp.part_index
289-
AND rfp.relation_model IS NOT NULL
290285
)
291-
SELECT * FROM resolved_fields_path
292-
ORDER BY model, path, part_index
286+
SELECT field_model,
287+
field_name,
288+
relation_model
289+
FROM resolved_fields_path
290+
ORDER BY part_index
293291
""",
294292
{"model": model, "path": list(path)},
295293
)

0 commit comments

Comments
 (0)