From 77dbb6df2f6a726238dfa9a19626f66b6ca4994c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sun, 14 Feb 2021 15:56:18 +0100 Subject: [PATCH] Make ModelView.parse_view public issue10073 review333851028 --- CHANGELOG | 1 + doc/ref/models/models.rst | 11 +++++++++++ trytond/model/modelview.py | 28 +++++++++++++++++----------- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 0bd0ff6d3e..77e326bcb0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Make ModelView.parse_view public * Add help for each value to selection and multiselection * Use safe_join in SharedDataMiddlewareIndex (issue10068) * Use menu name for window opened from menu diff --git a/doc/ref/models/models.rst b/doc/ref/models/models.rst index 8952a114c1..105ca11c89 100644 --- a/doc/ref/models/models.rst +++ b/doc/ref/models/models.rst @@ -174,6 +174,17 @@ Class methods: encoded value. If the depends list is set its fields are added to the view if the xpath matches at least one element. + +.. classmethod:: ModelView.parse_view(tree, type[, field_children[, level[, view_depends]]]) + + Return the sanitized XML and the corresponding fields definition. + + +.. note:: + + This method is public mainly to allow modification the existing XML of the + view by code. + ============ ModelStorage ============ diff --git a/trytond/model/modelview.py b/trytond/model/modelview.py index eb3e9d28ee..4bdec8f67d 100644 --- a/trytond/model/modelview.py +++ b/trytond/model/modelview.py @@ -376,10 +376,8 @@ class ModelView(Model): # Update arch and compute fields from arch parser = etree.XMLParser(remove_blank_text=True) tree = etree.fromstring(result['arch'], parser) - xarch, xfields = cls._view_look_dom_arch( + result['arch'], result['fields'] = cls.parse_view( tree, result['type'], result['field_childs'], level=level) - result['arch'] = xarch - result['fields'] = xfields cls._fields_view_get_cache.set(key, result) return result @@ -427,13 +425,20 @@ class ModelView(Model): return [] @classmethod - def _view_look_dom_arch(cls, tree, type, field_children=None, level=0): + def parse_view( + cls, tree, type, field_children=None, level=0, view_depends=None): + """ + Return sanitized XML and the corresponding fields definition + """ pool = Pool() ModelAccess = pool.get('ir.model.access') FieldAccess = pool.get('ir.model.field.access') encoder = PYSONEncoder() - view_depends = [] + if view_depends is None: + view_depends = [] + else: + view_depends = view_depends.copy() for xpath, attribute, value, *extra in cls.view_attributes(): depends = [] if extra: @@ -510,8 +515,8 @@ class ModelView(Model): if viewtreewidth.width > 0: fields_width[viewtreewidth.field] = viewtreewidth.width - fields_def = cls.__view_look_dom(tree_root, type, - fields_width=fields_width) + fields_def = cls.__parse_fields( + tree_root, type, fields_width=fields_width) if hasattr(cls, 'active'): fields_def.setdefault('active', {'name': 'active'}) @@ -552,8 +557,8 @@ class ModelView(Model): return arch, fields2 @classmethod - def __view_look_dom(cls, element, type, fields_width=None, - _fields_attrs=None): + def __parse_fields( + cls, element, type, fields_width=None, _fields_attrs=None): pool = Pool() Translation = pool.get('ir.translation') ModelData = pool.get('ir.model.data') @@ -711,8 +716,9 @@ class ModelView(Model): fields_attrs.setdefault(element.get(attr), {}) for field in element: - fields_attrs = cls.__view_look_dom(field, type, - fields_width=fields_width, _fields_attrs=fields_attrs) + fields_attrs = cls.__parse_fields( + field, type, fields_width=fields_width, + _fields_attrs=fields_attrs) return fields_attrs @staticmethod