-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
88 lines (66 loc) · 2.48 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright 2023 - today Numigi (tm) and all its contributors (https://bit.ly/numigiens)
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
import json
STANDARD_MODIFIERS = (
"invisible",
"column_invisible",
"readonly",
"required",
)
def set_custom_modifiers_on_fields(modifiers, fields):
"""
:param modifiers: list[dict] of modifiers to apply on the fields
:param fields: dict[str, dict] of model's fields and their attributes.
:return:
"""
_hide_selection_items(modifiers, fields)
def _hide_selection_items(modifiers, fields):
hidden_items = (
m
for m in modifiers
if m["type_"] == "field" and m["modifier"] == "selection_hide"
)
for item in hidden_items:
_hide_single_selection_item(item, fields)
def _hide_single_selection_item(modifier, fields):
field = fields.get(modifier["reference"])
if field and "selection" in field:
field["selection"] = [
(k, v) for k, v in field["selection"] if k != modifier["key"]
]
def add_custom_modifiers_to_view_arch(modifiers, arch):
"""Add custom modifiers to the given view architecture."""
for modifier in modifiers:
_add_custom_modifier_to_view_tree(modifier, arch)
return arch
def _add_custom_modifier_to_view_tree(modifier, tree):
"""Add a custom modifier to the given view architecture."""
xpath_expr = modifier["reference"]
if modifier["type_"] == "field":
xpath_expr = (
"//field[@name='{field_name}'] | //modifier[@for='{field_name}']".format(
field_name=modifier["reference"]
)
)
for node in tree.xpath(xpath_expr):
_add_custom_modifier_to_node(node, modifier)
def _add_custom_modifier_to_node(node, modifier):
key = modifier["modifier"]
if key == "widget":
node.attrib["widget"] = modifier["key"]
if key == "optional":
node.attrib["optional"] = modifier["key"]
elif key == "force_save":
node.attrib["force_save"] = "1"
elif key == "limit":
node.attrib["limit"] = modifier["key"]
elif key in STANDARD_MODIFIERS:
node.set(key, "1")
modifiers = _get_node_modifiers(node)
modifiers[key] = True
_set_node_modifiers(modifiers, node)
def _get_node_modifiers(node):
modifiers = node.get("modifiers")
return json.loads(modifiers) if modifiers else {}
def _set_node_modifiers(modifiers, node):
node.set("modifiers", json.dumps(modifiers))