Skip to content

Commit e65d655

Browse files
committed
[IMP] util.edit_view: better default value for active
Only active views by default when selected by xmlid. Views edited by id are most of the time mass-edited to adapt css classes or widget options and should not be blindly activated. Also add tests. closes #87 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent d4a608c commit e65d655

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

src/base/tests/test_util.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,72 @@ def test_replace_record_references_batch__uniqueness(self):
10471047
self.assertEqual(count, 1)
10481048

10491049

1050+
class TestEditView(UnitTestCase):
1051+
@parametrize(
1052+
[
1053+
(True, True, True),
1054+
(False, True, False),
1055+
]
1056+
)
1057+
def test_active_auto(self, initial_value, by_xmlid, by_view_id):
1058+
cr = self.env.cr
1059+
xmlid = "base.view_view_form"
1060+
view_id = util.ref(cr, xmlid)
1061+
1062+
cr.execute("UPDATE ir_ui_view SET active = %s WHERE id = %s", [initial_value, view_id])
1063+
1064+
# call by xmlid
1065+
with util.edit_view(cr, xmlid=xmlid, skip_if_not_noupdate=False, active="auto"):
1066+
pass
1067+
1068+
cr.execute("SELECT active FROM ir_ui_view WHERE id = %s", [view_id])
1069+
self.assertEqual(cr.fetchone()[0], by_xmlid)
1070+
1071+
# reset value
1072+
cr.execute("UPDATE ir_ui_view SET active = %s WHERE id = %s", [initial_value, view_id])
1073+
1074+
# call by view_id
1075+
with util.edit_view(cr, view_id=view_id, active="auto"):
1076+
pass
1077+
1078+
cr.execute("SELECT active FROM ir_ui_view WHERE id = %s", [view_id])
1079+
self.assertEqual(cr.fetchone()[0], by_view_id)
1080+
1081+
@parametrize(
1082+
[
1083+
(True, True, True),
1084+
(True, False, False),
1085+
(True, None, True),
1086+
(False, True, True),
1087+
(False, False, False),
1088+
(False, None, False),
1089+
]
1090+
)
1091+
def test_active_explicit(self, initial_value, value, expected_value):
1092+
cr = self.env.cr
1093+
xmlid = "base.view_view_form"
1094+
view_id = util.ref(cr, xmlid)
1095+
1096+
cr.execute("UPDATE ir_ui_view SET active = %s WHERE id = %s", [initial_value, view_id])
1097+
1098+
# call by xmlid
1099+
with util.edit_view(cr, xmlid=xmlid, skip_if_not_noupdate=False, active=value):
1100+
pass
1101+
1102+
cr.execute("SELECT active FROM ir_ui_view WHERE id = %s", [view_id])
1103+
self.assertEqual(cr.fetchone()[0], expected_value)
1104+
1105+
# reset value
1106+
cr.execute("UPDATE ir_ui_view SET active = %s WHERE id = %s", [initial_value, view_id])
1107+
1108+
# call by view_id
1109+
with util.edit_view(cr, view_id=view_id, active=value):
1110+
pass
1111+
1112+
cr.execute("SELECT active FROM ir_ui_view WHERE id = %s", [view_id])
1113+
self.assertEqual(cr.fetchone()[0], expected_value)
1114+
1115+
10501116
class TestMisc(UnitTestCase):
10511117
@parametrize(
10521118
[

src/util/records.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def remove_view(cr, xml_id=None, view_id=None, silent=False, key=None):
158158

159159

160160
@contextmanager
161-
def edit_view(cr, xmlid=None, view_id=None, skip_if_not_noupdate=True, active=True):
161+
def edit_view(cr, xmlid=None, view_id=None, skip_if_not_noupdate=True, active="auto"):
162162
"""
163163
Context manager to edit a view's arch.
164164
@@ -181,23 +181,28 @@ def edit_view(cr, xmlid=None, view_id=None, skip_if_not_noupdate=True, active=Tr
181181
`skip_if_not_noupdate` is set to `False`. If `noupdate` is `False`, the view will be
182182
yielded for edit.
183183
184-
If the `active` argument is not `None`, the `active` flag of the view will be set
184+
If the `active` argument is `True` or `False`, the `active` flag of the view will be set
185185
accordingly.
186186
187-
.. warning::
188-
The default value of `active` is `True`, therefore views are always *activated* by
189-
default. To avoid inadvertently activating views, pass `None` as `active` parameter.
187+
.. note::
188+
If `active` is "auto" (default value), the view will be activated if selected
189+
via `xmlid` and left untouched if selected via `view_id`.
190190
191191
:param str xmlid: optional, xml_id of the view edit
192192
:param int view_id: optional, ID of the view to edit
193193
:param bool skip_if_not_noupdate: whether to force the edit of views requested via
194194
`xmlid` parameter even if they are flagged as
195195
`noupdate=True`, ignored if `view_id` is set
196-
:param bool or None active: active flag value to set, nothing is set when `None`
196+
:param bool or None or "auto" active: active flag value to set. Unchanged when `None`.
197197
:return: a context manager that yields the parsed arch, upon exit the context manager
198198
writes back the changes.
199199
"""
200200
assert bool(xmlid) ^ bool(view_id), "You Must specify either xmlid or view_id"
201+
if active not in (True, False, None, "auto"):
202+
raise ValueError("Invalid `active` value: {!r}".format(active))
203+
if active == "auto":
204+
active = True if xmlid else None
205+
201206
noupdate = True
202207
if xmlid:
203208
if "." not in xmlid:

0 commit comments

Comments
 (0)