Skip to content

Commit 32b3f49

Browse files
committed
fixup! [REF] util.domains: DRY model fields path code and helpers
1 parent 546b5e7 commit 32b3f49

File tree

4 files changed

+40
-18
lines changed

4 files changed

+40
-18
lines changed

src/base/tests/test_util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -728,8 +728,8 @@ def test_model_table_convertion(self):
728728
self.assertEqual(table, self.env[model]._table)
729729
self.assertEqual(util.model_of_table(cr, table), model)
730730

731-
def test_resolve_model_fields_path(self):
732-
from odoo.addons.base.maintenance.migrations.util.helpers import FieldsPathPart, _resolve_model_fields_path
731+
def testresolve_model_fields_path(self):
732+
from odoo.addons.base.maintenance.migrations.util.helpers import FieldsPathPart, resolve_model_fields_path
733733

734734
cr = self.env.cr
735735

@@ -741,15 +741,15 @@ def test_resolve_model_fields_path(self):
741741
FieldsPathPart(model, path, 3, "res.company", "user_ids", "res.users"),
742742
FieldsPathPart(model, path, 4, "res.users", "partner_id", "res.partner"),
743743
]
744-
result = _resolve_model_fields_path(cr, model, path)
744+
result = resolve_model_fields_path(cr, model, path)
745745
self.assertEqual(result, expected_result)
746746

747747
model, path = "res.users", ("partner_id", "removed_field", "user_id")
748748
expected_result = [
749749
FieldsPathPart(model, list(path), 1, "res.users", "partner_id", "res.partner"),
750750
FieldsPathPart(model, list(path), 2, "res.partner", "removed_field", None),
751751
]
752-
result = _resolve_model_fields_path(cr, model, path)
752+
result = resolve_model_fields_path(cr, model, path)
753753
self.assertEqual(result, expected_result)
754754

755755

src/util/domains.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from openerp.tools.safe_eval import safe_eval
3434

3535
from .const import NEARLYWARN
36-
from .helpers import _dashboard_actions, _resolve_model_fields_path, _validate_model
36+
from .helpers import _dashboard_actions, _validate_model, resolve_model_fields_path
3737
from .inherit import for_each_inherit
3838
from .misc import SelfPrintEvalContext
3939
from .pg import column_exists, get_value_or_en_translation, table_exists
@@ -163,7 +163,7 @@ def _model_of_path(cr, model, path):
163163
if not path:
164164
return model
165165
path = tuple(path)
166-
resolved_parts = _resolve_model_fields_path(cr, model, path)
166+
resolved_parts = resolve_model_fields_path(cr, model, path)
167167
if not resolved_parts:
168168
return None
169169
last_part = resolved_parts[-1]

src/util/fields.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def make_index_name(table_name, column_name):
4040
from .const import ENVIRON
4141
from .domains import _adapt_one_domain, _replace_path, _valid_path_to, adapt_domains
4242
from .exceptions import SleepyDeveloperError
43-
from .helpers import _dashboard_actions, _resolve_model_fields_path, _validate_model, table_of_model
43+
from .helpers import _dashboard_actions, _validate_model, resolve_model_fields_path, table_of_model
4444
from .inherit import for_each_inherit
4545
from .misc import SelfPrintEvalContext, log_progress, version_gte
4646
from .orm import env, invalidate
@@ -79,7 +79,7 @@ def make_index_name(table_name, column_name):
7979
)
8080

8181

82-
def _get_resolved_ir_exports(cr, models=None, fields=None):
82+
def _getresolved_ir_exports(cr, models=None, fields=None):
8383
"""
8484
Return a list of ir.exports.line records which models or fields match the given arguments.
8585
@@ -122,7 +122,7 @@ def _get_resolved_ir_exports(cr, models=None, fields=None):
122122
# Resolve intermediate models for all model fields paths, filter only matching paths parts
123123
matching_paths_parts = {}
124124
for model, path in paths_to_line_ids:
125-
resolved_paths = _resolve_model_fields_path(cr, model, path)
125+
resolved_paths = resolve_model_fields_path(cr, model, path)
126126
if fields:
127127
matching_parts = [p for p in resolved_paths if (p.field_model, p.field_name) in fields]
128128
else:
@@ -151,7 +151,7 @@ def rename_ir_exports_fields(cr, models_fields_map):
151151
152152
:meta private: exclude from online docs
153153
"""
154-
matching_exports = _get_resolved_ir_exports(
154+
matching_exports = _getresolved_ir_exports(
155155
cr,
156156
fields=[(model, field) for model, fields_map in models_fields_map.items() for field in fields_map],
157157
)
@@ -192,7 +192,7 @@ def remove_ir_exports_lines(cr, models=None, fields=None):
192192
193193
:meta private: exclude from online docs
194194
"""
195-
matching_exports = _get_resolved_ir_exports(cr, models=models, fields=fields)
195+
matching_exports = _getresolved_ir_exports(cr, models=models, fields=fields)
196196
if not matching_exports:
197197
return
198198
_logger.debug("Deleting %d export template lines with removed models/fields", len(matching_exports))

src/util/helpers.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,40 @@ def _get_theme_models():
221221
"FieldsPathPart",
222222
"model path part_index field_model field_name relation_model",
223223
)
224+
FieldsPathPart.__doc__ = """
225+
Encapsulate information about a field within a fields path.
224226
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``
230+
:param str field_model: model of the field
231+
:param str field_name: name of the field
232+
:param str relation_model: target model of the field, if relational, otherwise ``None``
233+
"""
234+
for _f in FieldsPathPart._fields:
235+
getattr(FieldsPathPart, _f).__doc__ = None
225236

226-
def _resolve_model_fields_path(cr, model, path):
237+
238+
def resolve_model_fields_path(cr, model, path):
227239
"""
228-
Resolve model fields paths (e.g. `hr.appraisal` `['employee_id', 'user_id', 'partner_id']`).
240+
Resolve model fields paths.
241+
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).
244+
245+
.. example::
246+
247+
To get the information about the fields path ``user_ids.partner_id.title_id``
248+
from model `res.users`, we can call this function as
249+
250+
.. code-block:: python
229251
230-
:param str model: the model to resolve the fields ``path`` from.
231-
:param typing.Sequence[str] path: the fields path starting from ``model``.
232-
:return: a list of the resolved fields path parts through their relation models.
233-
:rtype: list[FieldsPathPart]
252+
resolve_model_fields_path(cr, "res.users", "user_ids.partner_id.title_id".split("."))
234253
235-
:meta private: exclude from online docs
254+
:param str model: model to resolve the fields ``path`` from
255+
:param typing.Sequence[str] path: fields path starting from ``model``
256+
:return: list of resolved fields path parts
257+
:rtype: list(:class:`~odoo.upgrade.util.helpers.FieldsPathPart`)
236258
"""
237259
path = list(path)
238260
cr.execute(

0 commit comments

Comments
 (0)