-
-
Notifications
You must be signed in to change notification settings - Fork 808
/
Copy pathir_qweb.py
72 lines (65 loc) · 2.9 KB
/
ir_qweb.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
# Copyright 2017 Creu Blanca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import _, models
from odoo.exceptions import ValidationError
class IrQWeb(models.AbstractModel):
_inherit = "ir.qweb"
def check_length(self, value, min_length=False, max_length=False): # noqa
"""No use staticmethod because self is needed to translate exception messages"""
if min_length and len(value) < min_length:
raise ValidationError(_("Length cannot be less than %s") % str(min_length))
if max_length and len(value) > max_length:
raise ValidationError(_("Length cannot be more than %s") % str(max_length))
return value
def _compile_directive_esc(self, el, compile_context, level):
min_value = el.attrib.pop("t-minlength", False)
max_value = el.attrib.pop("t-maxlength", False)
if min_value or max_value:
el.attrib["t-esc"] = (
'docs.env["ir.qweb"].check_length('
+ el.attrib["t-esc"]
+ ", "
+ (min_value or "False")
+ ", "
+ (max_value or "False")
+ ")"
)
if "t-length" in el.attrib:
tlength = el.attrib.pop("t-length")
el.attrib["t-esc"] = "(" + el.attrib["t-esc"] + ")[:" + tlength + "]"
return super()._compile_directive_esc(el, compile_context, level)
def _compile_directive_out(self, el, compile_context, level):
min_value = el.attrib.pop("t-minlength", False)
max_value = el.attrib.pop("t-maxlength", False)
if min_value or max_value:
el.attrib["t-out"] = (
'docs.env["ir.qweb"].check_length('
+ el.attrib["t-out"]
+ ", "
+ (min_value or "False")
+ ", "
+ (max_value or "False")
+ ")"
)
if "t-length" in el.attrib:
tlength = el.attrib.pop("t-length")
el.attrib["t-out"] = el.attrib["t-out"] + "[:" + tlength + "]"
return super()._compile_directive_out(el, compile_context, level)
def _compile_directive_raw(self, el, compile_context, level):
# TODO: t-raw is deprecated, can it be removed?
min_value = el.attrib.pop("t-minlength", False)
max_value = el.attrib.pop("t-maxlength", False)
if min_value or max_value:
el.attrib["t-raw"] = (
'docs.env["ir.qweb"].check_length('
+ el.attrib["t-raw"]
+ ", "
+ (min_value or "False")
+ ", "
+ (max_value or "False")
+ ")"
)
if "t-length" in el.attrib:
tlength = el.attrib.pop("t-length")
el.attrib["t-raw"] = el.attrib["t-raw"] + "[:" + tlength + "]"
return super()._compile_directive_raw(el, compile_context, level)