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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
###############################################################################
#
# Copyright (C) 2007-TODAY Tiny ERP Pvt Ltd. All Rights Reserved.
#
# $Id$
#
# Developed by Tiny (http://openerp.com) and Axelor (http://axelor.com).
#
# The OpenERP web client is distributed under the "OpenERP Public License".
# It's based on Mozilla Public License Version (MPL) 1.1 with following
# restrictions:
#
# - All names, links and logos of Tiny, Open ERP and Axelor must be
# kept as in original distribution without any changes in all software
# screens, especially in start-up page and the software header, even if
# the application source code has been changed or updated or code has been
# added.
#
# - All distributions of the software must keep source code with OEPL.
#
# - All integrations to any other software must keep source code with OEPL.
#
# If you need commercial licence to remove this kind of restriction please
# contact us.
#
# You can see the MPL licence at: http://www.mozilla.org/MPL/MPL-1.1.html
#
###############################################################################
import re
import cgi
import math
import time
import base64
import locale
from openobject.i18n import format
from openobject.validators import *
class String(BaseValidator):
if_empty = False
def _to_python(self, value, state):
if isinstance(value, unicode):
return value.encode('utf-8')
return value
def _from_python(self, value, state):
return ustr(value or '')
class Bool(BaseValidator):
values = ['1', 'true']
if_empty = False
def _to_python(self, value, state):
value = value or False
if value:
value = str(value).lower() in self.values
return value
def _from_python(self, value, state):
return (value or '') and 1
class Int(Int):
if_empty = False
class Float(Number):
if_empty = False
digit = 2
def _from_python(self, value, state):
return format.format_decimal(float(value) or 0.0, self.digit)
def _to_python(self, value, state):
try:
value = format.parse_decimal(value)
except ValueError:
pass
return Number.to_python(value, state)
class FloatTime(Float):
if_empty = False
def _from_python(self, value, state):
val = value or 0.0
t = '%02d:%02d' % (math.floor(abs(val)),round(abs(val)%1+0.01,2) * 60)
if val < 0:
t = '-' + t
return t
def _to_python(self, value, state):
try:
if value and ':' in value:
return round(int(value.split(':')[0]) + int(value.split(':')[1]) / 60.0, 2)
else:
return locale.atof(value)
except:
pass
return 0.0
class DateTime(BaseValidator):
if_empty = False
kind = "datetime"
def __init__(self, kind="datetime", *args, **kwargs):
super(DateTime, self).__init__(*args, **kwargs)
self.format = format.get_datetime_format(kind)
self.kind = kind
def _to_python(self, value, state):
# do validation
try:
res = time.strptime(value, self.format)
except ValueError:
raise Invalid(_('Invalid datetime format'), value, state)
# return str instead of real datetime object
try:
return format.parse_datetime(value, kind=self.kind)
except ValueError:
raise Invalid(_('Invalid datetime format'), value, state)
def _from_python(self, value, state):
return format.format_datetime(value, kind=self.kind)
class Selection(BaseValidator):
if_empty = False
def _to_python(self, value, state):
if isinstance(value, basestring):
if re.match('True|False|None', value):
return eval(value)
if re.match('^\-+|\d+$', value):
return int(value)
if re.match('^\-+|\d+(\.\d+)$', value):
return float(value)
return value
class Reference(BaseValidator):
if_empty = False
def _to_python(self, value, state):
if isinstance(value, basestring):
value = eval(value)
id, ref = value
if ref and id:
return "%s,%d"%(ref, int(id))
return False
class Binary(BaseValidator):
if_empty = False
def _to_python(self, value, state):
if isinstance(value, cgi.FieldStorage):
if value.filename:
return base64.encodestring(value.file.read())
elif self.not_empty:
raise Invalid(_('Please select a file.'), value, state)
return self.if_empty
class URL(URL):
if_empty = False
require_tld = False
url_re = re.compile(r'''
^(http|https|ftp|sftp|file):// # protocol
(?:[%:\w]*@)? # authenticator
(?P<domain>[a-z0-9][a-z0-9\-]{1,62}\.)* # (sub)domain - alpha followed by 62max chars (63 total)
(?P<tld>[a-z]{2,})? # TLD
(?::[0-9]+)? # port
# files/delims/etc
(?P<path>/[a-z0-9\-\._~:/\?#\[\]@!%\$&\'\(\)\*\+,;=]*)?
$
''', re.I | re.VERBOSE)
def _from_python(self, value, state):
return value or ''
class Email(Email):
if_empty = False
def _from_python(self, value, state):
return value or ''
class many2many(BaseValidator):
if_empty = [(6, 0, [])]
def _to_python(self, value, state):
if isinstance(value, basestring):
value = eval(value)
if not isinstance(value, (tuple, list)):
value = (value or []) and [value]
return [(6, 0, [int(id) for id in value if id])]
class many2one(BaseValidator):
if_empty = False
def _to_python(self, value, state):
if isinstance(value, (list, tuple)):
value = (len(value) or False) and value[0]
try:
return int(value)
except:
return False
def _from_python(self, value, state):
if isinstance(value, (list, tuple)):
value = (len(value) or False) and value[0]
return value or ''
# Let some FormEncode strings goes into message catalog.
__email_messages = {
'empty': _('Please enter an email address'),
'noAt': _('An email address must contain a single @'),
'badUsername': _('The username portion of the email address is invalid (the portion before the @: %(username)s)'),
'badDomain': _('The domain portion of the email address is invalid (the portion after the @: %(domain)s)'),
}
__url_messages = {
'noScheme': _('You must start your URL with http://, https://, etc'),
'badURL': _('That is not a valid URL'),
'noTLD': _('You must provide a full domain name (like %(domain)s.com)'),
}
__type_messages = {
'integer': _("Please enter an integer value"),
'number': _("Please enter a number"),
'badFormat': _('Invalid datetime format'),
}
# vim: ts=4 sts=4 sw=4 si et
|