-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatatypes.py
486 lines (354 loc) · 12.5 KB
/
datatypes.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# -*- coding: UTF-8 -*-
# Copyright (C) 2009 Sylvain Taverne <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import from standard library
import datetime
import re
from decimal import Decimal as decimal
from operator import itemgetter
# Import from itools
from itools.datatypes import Boolean, Enumerate, PathDataType, String
from itools.datatypes import Date, Decimal, Integer, Unicode
from itools.gettext import MSG
from itools.uri import Path
from itools.web import get_context
# Import from ikaaro
from ikaaro.file import Image
# Import from shop
from registry import shop_skins, register_datatype
from utils import format_price
class RangeDatatype(object):
default = [None, None]
multiple = True
class IntegerRangeDatatype(RangeDatatype, Integer):
pass
class DecimalRangeDatatype(RangeDatatype, Decimal):
pass
class StringFixSize(String):
@classmethod
def is_valid(cls, value):
if not value:
return True
return len(value) == cls.size
class Civilite(Enumerate):
options = [
{'name': 'mister', 'value': MSG(u"M.")},
{'name': 'madam', 'value': MSG(u"Mme")},
{'name': 'miss', 'value': MSG(u"Mlle")}]
class ImagePathDataType(PathDataType):
"""
-> We check that the path correspond to an image
-> Default value is 'None' not '.'.
"""
default = None
@staticmethod
def is_valid(value):
if not value:
return True
context = get_context()
resource = context.resource
image = resource.get_resource(value, soft=True)
if image is None:
return False
if not isinstance(image, Image):
return False
return True
@staticmethod
def decode(value):
if not value:
return ''
return Path(value)
@staticmethod
def encode(value):
if not value:
return ''
return str(value)
class ProductPathDataType(PathDataType):
# XXX we have to implement is_valid
default = ''
class BigUnicode(Unicode):
pass
class UnicodeOnePerLine(Unicode):
@classmethod
def render(cls, value, context):
if value is None:
return None
return [x.strip() for x in value.split('\n') if x.strip()]
class AbsolutePathDataTypeEnumerate(Enumerate):
pass
class ImagesEnumerate(AbsolutePathDataTypeEnumerate):
@classmethod
def get_options(cls):
context = get_context()
resource = context.resource.get_resource('images', soft=True)
if resource is None:
resource = context.resource.parent.get_resource('images')
return [{'name': res.get_abspath(),
'link': context.get_link(res),
'value': res.get_title()}
for res in resource.get_resources()]
class DynamicEnumerate(AbsolutePathDataTypeEnumerate):
path = None
format = None
is_abspath = False
@classmethod
def get_options(cls):
context = get_context()
if cls.is_abspath is False:
resource = context.site_root.get_resource(cls.path)
resources = resource.search_resources(format=cls.format)
else:
root = context.root
resources = [root.get_resource(brain.abspath)
for brain in root.search(format=cls.format).get_documents()]
return [{'name': str(res.get_abspath()),
'value': res.get_title()}
for res in resources]
@classmethod
def get_value(cls, name, default=None):
if name is None:
return
context = get_context()
if cls.is_abspath is True:
path = name
else:
path = '%s/%s' % (cls.path, name)
resource = context.site_root.get_resource(path)
return resource.get_title()
class ShopBoolean(Boolean):
@classmethod
def render(cls, value, context):
if value:
return MSG(u'Yes')
return MSG(u'No')
class ThreeStateBoolean(Boolean):
default = ''
@staticmethod
def decode(value):
if value is '':
return None
return bool(int(value))
@staticmethod
def encode(value):
if value is True:
return '1'
elif value is False:
return '0'
return None
@staticmethod
def is_empty(value):
return False
class DatatypeCM_to_INCH(Decimal):
@classmethod
def render(cls, value, context):
if value is None:
return None
# Get informations
accept = context.accept_language
site_root = context.resource.get_site_root()
ws_languages = site_root.get_property('website_languages')
lang = accept.select_language(ws_languages)
# Render
mesure = u'cm'
if lang == 'en':
inch = decimal('2.54')
mesure = u'inch'
value = format_price(value/inch)
return u'%s %s' % (value, mesure)
class IntegerRange(Integer):
@staticmethod
def decode(value):
value1, value2 = value.split('@')
return int(value1) if value1 else None, int(value2) if value2 else None
@staticmethod
def encode(value):
value1, value2 = value
return '%s@%s' % (value1 if value1 else '', value2 if value2 else '')
class UserGroup_Enumerate(DynamicEnumerate):
format = 'user-group'
is_abspath = True
class Users_Enumerate(DynamicEnumerate):
format = 'user'
is_abspath = True
@classmethod
def get_options(cls):
context = get_context()
root = context.root
resources = [root.get_resource(brain.abspath)
for brain in root.search(format='user').get_documents()]
options = [{'name': res.name, 'value': res.get_title()}
for res in resources]
options.sort(key=itemgetter('value'))
return options
@classmethod
def get_value(cls, name, default=None):
context = get_context()
user = context.root.get_resource('users/%s' % name)
return user.get_title()
class SkinsEnumerate(Enumerate):
@classmethod
def get_options(cls):
options = shop_skins
if cls.all_skins is True:
return options
return [x for x in options if x['name'] == cls.value]
class FrenchDate(Date):
@staticmethod
def decode(data):
if not data:
return None
if '-' in data:
day, month, year = data.split('-')
if int(day) > 31:
# Format '%Y-%m-%d'
day, year = year, day
elif '/' in data:
day, month, year = data.split('/')
day, month, year = int(day), int(month), int(year)
return datetime.date(year, month, day)
@staticmethod
def encode(value):
if value is None:
return ''
return value.strftime('%d/%m/%Y')
@staticmethod
def is_valid(value):
if value and (value < datetime.date(1900,1,1)
or value > datetime.date(2100,1,1)):
return False
return True
class FrenchBirthday_Datatype(FrenchDate):
@staticmethod
def decode(value):
if not value:
return None
if value and value != '1':
return FrenchDate.decode(value)
context = get_context()
day = context.get_form_value('day', type=Integer)
month = context.get_form_value('month', type=Integer)
year = context.get_form_value('year', type=Integer)
return datetime.date(year, month, day)
@staticmethod
def encode(value):
if value:
return FrenchDate.encode(value)
context = get_context()
day = context.get_form_value('day', type=Integer)
month = context.get_form_value('month', type=Integer)
year = context.get_form_value('year', type=Integer)
if not (day and month and year):
return None
value = datetime.date(year, month, day)
return value.strftime('%d/%m/%Y')
@staticmethod
def is_valid(value):
return True
class PrettyFrenchDate(Date):
days = {1: 'Lundi', 2: 'Mardi', 3: 'Mercredi', 4: 'Jeudi', 5: 'Vendredi',
6: 'Samedi', 7: 'Dimanche'}
months = {1: u'Janvier', 2: u'Février', 3: u'Mars', 4: u'Avril', 5: u'Mai',
6: u'Juin', 7: u'Juillet', 8: u'Août', 9: u'Septembre',
10: u'Octobre', 11: u'Novembre', 12: u'Décembre'}
@classmethod
def render(cls, value, context):
return u' '.join([PrettyFrenchDate.days[value.isoweekday()],
str(value.day),
PrettyFrenchDate.months[value.month],
str(value.year)])
class SIRET_Datatype(String):
@classmethod
def is_valid(cls, value):
# Luhn Algo
# http://fr.wikipedia.org/wiki/Luhn
value = value.replace(" ", "")
if len(value) != 14:
return False
total = 0
for i, c in enumerate(value):
digit = int(c)
if i % 2 == 0:
# Pair
digit = digit * 2
if digit > 9:
digit = digit - 9
total += digit
return (total % 10) == 0
class UE_VAT_Datatype(String):
@classmethod
def is_valid(cls, value):
# http://ec.europa.eu/taxation_customs/vies/faqvies.do
value = value.replace(" ", "")
regexp = r"""^(
AT ?U[0-9]{7}| # Austria
BE ?[0-9]{10}| # Belgium
BG ?[0-9]{9,10}| # Bulgaria
CY ?[0-9]{8}[A-Z]{1}| # Cyprius
CZ ?[0-9]{8,10}| # Czech Republic
DE ?[0-9]{9}| # Germany
DK ?[0-9]{8}| # Denmark
EE ?[0-9]{9}| # Estoniag
EL ?[0-9]{9}| # Greece
ES ?[0-9A-Z]{9}| # Spain
FI ?[0-9]{8}| # Finland
FR ?[0-9A-Z]{11}| # France
GB ?[0-9]{9,12}|GB ?[0-9A-Z]{5}| # United Kingdom
HU ?[0-9]{8}| # Hungary
IE ?[0-9A-Z]{8}| # Ireland
IT ?[0-9]{11}| # Italy
LT ?([0-9]{9}|[0-9]{12})| # Lithuania
LU ?[0-9]{8}| # Luxembourg
LV ?[0-9]{11}| # Latvia
MT ?[0-9]{8}| # Malta
NL ?[0-9B]{12}| # The Netherlands
PL ?[0-9]{10}| # Poland
PT ?[0-9]{9}| # Portugal
RO ?[0-9]{2,10}| # Romania
SE ?[0-9]{12}| # Sweden
SI ?[0-9]{8}| # Slovenia
SK ?[0-9]{10} # Slovakia
)$"""
prog = re.compile(regexp, re.X) # Verbose
return prog.match(value) != None
class Days(Enumerate):
@classmethod
def get_options(cls):
options = []
for d in range(1, 32):
options.append({'name': d, 'value': str(d)})
return options
class Months(Enumerate):
options = [
{'name': 1, 'value': MSG(u'January')},
{'name': 2, 'value': MSG(u'February')},
{'name': 3, 'value': MSG(u'March')},
{'name': 4, 'value': MSG(u'April')},
{'name': 5, 'value': MSG(u'May')},
{'name': 6, 'value': MSG(u'June')},
{'name': 7, 'value': MSG(u'July')},
{'name': 8, 'value': MSG(u'August')},
{'name': 9, 'value': MSG(u'September')},
{'name': 10, 'value': MSG(u'October')},
{'name': 11, 'value': MSG(u'November')},
{'name': 12, 'value': MSG(u'December')}]
class Years(Enumerate):
@classmethod
def get_options(cls):
options = []
for d in range(1920, datetime.date.today().year):
options.append({'name': d, 'value': str(d)})
return options
register_datatype('siret', MSG(u'SIRET'), SIRET_Datatype)
register_datatype('ue_vat', MSG(u'UE VAT'), UE_VAT_Datatype)