forked from hforge/ikaaro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datatypes.py
301 lines (212 loc) · 7.58 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
# -*- coding: UTF-8 -*-
# Copyright (C) 2008 Juan David Ibáñez Palomar <[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
from datetime import date
# Import from the Standard Library
from base64 import decodestring, encodestring
from datetime import timedelta
from marshal import dumps, loads
from urllib import quote, unquote
from zlib import compress, decompress
# Import from itools
from itools.core import freeze, guess_type
from itools.datatypes import DataType, Date, Enumerate, String
from itools.fs import FileName
from itools.gettext import MSG
from itools.html import stream_to_str_as_xhtml, stream_to_str_as_html
from itools.html import xhtml_doctype, sanitize_stream, stream_is_empty
from itools.xml import XMLParser, is_xml_stream
"""This module defines some datatypes used in ikaaro, whose inclusion in
itools is not yet clear.
"""
encoding_map = {
'gzip': 'application/x-gzip',
'bzip2': 'application/x-bzip2'}
def guess_mimetype(filename, default):
"""Override itools function 'guess_type' to intercept the encoding.
"""
name, extension, language = FileName.decode(filename)
filename = FileName.encode((name, extension, None))
mimetype, encoding = guess_type(filename)
return encoding_map.get(encoding, mimetype or default)
class FileDataType(DataType):
"""FIXME This datatype is special in that it does not deserializes from
a byte string, but from a tuple. Some day we should find a correct
solution.
"""
@staticmethod
def encode(value):
"""Cannot preload anything in a file input.
"""
return None
@staticmethod
def decode(data):
"""Find out the resource class (the mimetype sent by the browser can be
minimalistic).
"""
filename, mimetype, body = data
mimetype = guess_mimetype(filename, mimetype)
return filename, mimetype, body
class Password_Datatype(DataType):
@staticmethod
def decode(data):
return decodestring(unquote(data))
@staticmethod
def encode(value):
return quote(encodestring(value))
class ChoosePassword_Datatype(String):
@staticmethod
def is_valid(value):
return len(value) >= 4
class CopyCookie(DataType):
default = None, freeze([])
@staticmethod
def encode(value):
return quote(compress(dumps(value), 9))
@staticmethod
def decode(str):
return loads(decompress(unquote(str)))
class ExpireValue(DataType):
@staticmethod
def decode(value):
return timedelta(minutes=int(value))
@staticmethod
def encode(value):
return str(int(value.total_seconds() / 60))
class BirthDate(Date):
pass
class HexadecimalColor(String):
@staticmethod
def is_valid(value):
return value.startswith('#') and len(value) == 7
###########################################################################
# HTML
###########################################################################
xhtml_namespaces = {None: 'http://www.w3.org/1999/xhtml'}
class XHTMLBody(DataType):
"""Read and write XHTML.
"""
sanitize_html = True
def decode(cls, data):
events = XMLParser(data, namespaces=xhtml_namespaces,
doctype=xhtml_doctype)
if cls.sanitize_html is True:
events = sanitize_stream(events)
return list(events)
@staticmethod
def encode(value):
if value is None:
return ''
return stream_to_str_as_xhtml(value)
@staticmethod
def is_empty(value):
return stream_is_empty(value)
class HTMLBody(XHTMLBody):
"""TinyMCE specifics: read as XHTML, rendered as HTML.
"""
@staticmethod
def encode(value):
if value is None:
return ''
if type(value) is unicode:
return value.encode('utf-8')
if not is_xml_stream(value):
value = value.get_body().get_content_elements()
return stream_to_str_as_html(value)
###########################################################################
# Enumerates
###########################################################################
days = {
0: MSG(u'Monday'),
1: MSG(u'Tuesday'),
2: MSG(u'Wednesday'),
3: MSG(u'Thursday'),
4: MSG(u'Friday'),
5: MSG(u'Saturday'),
6: MSG(u'Sunday')}
class DaysOfWeek(Enumerate):
options = [
{'name':'1', 'value': MSG(u'Monday'), 'shortname': 'MO'},
{'name':'2', 'value': MSG(u'Tuesday'), 'shortname': 'TU'},
{'name':'3', 'value': MSG(u'Wednesday'), 'shortname': 'WE'},
{'name':'4', 'value': MSG(u'Thursday'), 'shortname': 'TH'},
{'name':'5', 'value': MSG(u'Friday'), 'shortname': 'FR'},
{'name':'6', 'value': MSG(u'Saturday'), 'shortname': 'SA'},
{'name':'7', 'value': MSG(u'Sunday'), 'shortname': 'SU'}]
@classmethod
def get_shortname(cls, name):
for option in cls.options:
if option['name'] == name:
return option['shortname']
@classmethod
def get_name_by_shortname(cls, shortname):
for option in cls.options:
if option['shortname'] == shortname:
return option['name']
class Boolean3(Enumerate):
""" Boolean 3 states : Yes/No/Any useful on search form."""
default = ''
options = [
#{'name': '', 'value': u''},
{'name': '1', 'value': MSG(u'Yes')},
{'name': '0', 'value': MSG(u'No')}]
@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_valid(value):
return value in (True, False, None)
def get_namespace(cls, name):
if name in (True, False, None):
name = Boolean3.encode(name)
return Enumerate(options=cls.get_options()).get_namespace(name)
class IntegerRange(Enumerate):
count = 4
@classmethod
def get_options(cls):
return [
{'name': str(i), 'value': str(i)} for i in range(1, cls.count) ]
class Days(IntegerRange):
count = 32
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):
start = 1900
@classmethod
def get_options(cls):
return [ {'name': str(d), 'value': str(d)}
for d in range(cls.start, date.today().year) ]