-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwidgets.py
393 lines (348 loc) · 14.4 KB
/
widgets.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
# -*- coding: UTF-8 -*-
# Copyright (C) 2005 <jdavid@favela.(none)>
# Copyright (C) 2006 J. David Ibanez <[email protected]>
# Copyright (C) 2006 luis <[email protected]>
# Copyright (C) 2006-2010 Hervé Cauwelier <[email protected]>
# Copyright (C) 2008 Henry Obein <[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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Import from the Standard Library
from decimal import InvalidOperation
# Import from itools
from itools.core import is_prototype, merge_dicts
from itools.datatypes import XMLContent, XMLAttribute
from itools.gettext import MSG
from itools.log import log_warning
from itools.web import get_context
# Import from goodforms
from datatypes import Numeric, Text, EnumBoolean, SqlEnumerate, NumDecimal
from datatypes import NumDate, NumTime, FileImage
from schema import Expression
NBSP = u"\u00a0".encode('utf8')
FIELD_PREFIX = u"#"
def is_mandatory_filled(datatype, name, value, schema, fields, context):
if context.method != 'POST':
return True
if context.resource.is_disabled_by_dependency(name, schema, fields):
return True
if not datatype.mandatory:
return True
return value is not None
def make_element(tagname, attributes={}, content=u""):
if is_prototype(content, MSG):
content = content.gettext()
element = [u"<", tagname]
for key, value in attributes.iteritems():
if value is None:
continue
if type(value) is list:
value = u" ".join(value)
elif is_prototype(value, MSG):
value = value.gettext()
element.extend((u" ", key, u'="', value, u'"'))
if tagname in ('input', 'img'):
element.extend((u"/>", content))
else:
element.extend((u">", content, u"</", tagname, u">"))
return u"".join(element)
def check_errors(context, form, datatype, name, value, schema, fields,
readonly, attributes, tabindex=None):
attributes.setdefault(u"class", [])
if name in context.bad_types:
attributes[u"title"] = MSG(u"Bad value")
attributes[u"class"].append(u"badtype")
elif not is_mandatory_filled(datatype, name, value, schema, fields,
context):
attributes[u"title"] = MSG(u"Mandatory field")
attributes[u"class"].append(u"mandatory")
if form.is_disabled_by_dependency(name, schema, fields):
attributes[u"disabled"] = u"disabled"
#attributes[u"class"].append(u"disabled")
if tabindex:
attributes[u"tabindex"] = tabindex
def _choice_widget(context, form, datatype, name, value, schema, fields,
readonly, container, container_attributes, choice, choice_attributes,
choice_checked, multiline, tabindex):
html = []
# True -> '1' ; False -> '2'
data = datatype.encode(value)
if readonly:
for option in datatype.get_namespace(data):
attributes = merge_dicts(
choice_attributes,
value=option['name'],
disabled=u"disabled")
attributes.setdefault(u"class", []).append(u"disabled")
if option['selected']:
attributes[choice_checked] = choice_checked
value = option['value']
if is_prototype(value, MSG):
value = value.gettext()
content = XMLContent.encode(value)
input = make_element(choice, attributes, content)
if multiline is True:
# Une option par ligne
html.append(make_element(u"div", content=input))
else:
html.append(input)
else:
if type(data) == type("") or type(data) == type(u""):
try:
data_tmp = data.decode("utf-8").split('|')
except:
data_tmp = data
else:
data_tmp = data
for option in datatype.get_namespace(data_tmp):
js = []
opt_name = option['name']
attributes = merge_dicts(choice_attributes, value=opt_name)
if option['selected'] or data == option['name'].strip().encode('utf-8'):
attributes[choice_checked] = choice_checked
if form.is_disabled_by_dependency(name, schema, fields):
attributes[u"disabled"] = u"disabled"
attributes.setdefault(u"class", []).append(u"disabled")
# 0005970: Le Javascript pour (dés)activer les autres champs
dep_names = form.get_dep_names(name, schema)
for dep_name in dep_names:
dependency = schema[dep_name].dependency
if not dependency:
continue
# Add js action on simple boolean expressions. ie: "C11"
if not Expression.is_simple(dependency):
# Authorized operators are "==", "=" and "in"
# ie:"C11=='others'", "C11='others'", "'others' in C11"
if 'in' in dependency:
dep_value = dependency.split('in', 1)[0].strip()
elif '==' in dependency:
exclusive, dep_value = dependency.rsplit('==', 1)
elif '!=' in dependency:
exclusive, dep_value = dependency.rsplit('!=', 1)
else:
msg = 'K col operator unknown in "%s"' % dependency
log_warning(msg)
continue
if dep_value[:1] in ("u'" , 'u"'):
dep_value = dep_value[:2]
dep_value = (dep_value.replace("'", '').replace('"', ''))
#dep_value = checkid(dep_value)
else:
dep_value = 'false'
js_code = (
'$("input[name=\\"%s\\"][value=\\"%s\\"]")'
'.change(function(){switch_input($(this),"%s","%s");});'
% (name, opt_name, dep_name, dep_value))
js.append(js_code)
# 0005970 fin
value = option['value']
if is_prototype(value, MSG):
value = value.gettext()
content = XMLContent.encode(value)
input = make_element(choice, attributes, content)
if multiline is True:
# Une option par ligne
html.append(make_element(u"div", content=input))
else:
html.append(input)
# Append JS code
content = XMLContent.encode(u'\n'.join(js))
attributes = {u'type': "text/javascript"}
html.append(make_element(u'script', attributes, content))
attributes = merge_dicts(
container_attributes,
id=u"field_{name}".format(name=name))
check_errors(context, form, datatype, name, data, schema, fields,
readonly, attributes, tabindex=None)
return make_element(container, attributes, u"".join(html))
def radio_widget(context, form, datatype, name, value, schema, fields,
readonly, tabindex=None):
container = u"div"
container_attributes = {}
choice = u"input"
choice_attributes = {
u"id": u"field_{name}".format(name=name),
u"type": u"radio",
u"name": name}
choice_checked = u"checked"
# Oui/Non sur une seule ligne
multiline = not issubclass(datatype, EnumBoolean)
return _choice_widget(context, form, datatype, name, value, schema,
fields, readonly, container, container_attributes, choice,
choice_attributes, choice_checked, multiline, tabindex)
def checkbox_widget(context, form, datatype, name, value, schema, fields,
readonly, tabindex=None):
container = u"div"
container_attributes = {}
choice = u"input"
choice_attributes = {
u"id": u"field_{name}".format(name=name),
u"type": u"checkbox",
u"name": name}
choice_checked = u"checked"
# Oui/Non sur une seule ligne
multiline = not issubclass(datatype, EnumBoolean)
if type(value) == type([]):
value = " ".join(value)
return _choice_widget(context, form, datatype, name, value, schema,
fields, readonly, container, container_attributes, choice,
choice_attributes, choice_checked, multiline, tabindex)
def select_widget(context, form, datatype, name, value, schema, fields,
readonly, tabindex=None):
container = u"select"
container_attributes = {
u"name": name,
u"class": "chosen-select"}
choice = u"option"
choice_attributes = {
u"name": name}
choice_checked = u"selected"
multiline = False
return _choice_widget(context, form, datatype, name, value, schema,
fields, readonly, container, container_attributes, choice,
choice_attributes, choice_checked, multiline, tabindex)
def textarea_widget(context, form, datatype, name, value, schema, fields,
readonly, tabindex=None):
if readonly:
attributes = {
u"style": u"white-space: pre",
u"class": u"readonly"}
content = XMLContent.encode(value)
return make_element(u"div", attributes, content)
attributes = {
u"id": u"field_{name}".format(name=name),
u"name": name,
u"rows": str(datatype.size),
u"cols": str(datatype.length)}
# Check for errors
check_errors(context, form, datatype, name, value, schema, fields,
readonly, attributes, tabindex=None)
# special case for 'value'
content = XMLContent.encode(value)
return make_element(u"textarea", attributes, content)
def text_widget(context, form, datatype, name, value, schema, fields,
readonly, tabindex=None):
# Reçoit des int en GET
if not isinstance(value, basestring):
value = datatype.encode(value)
if isinstance(datatype, NumDecimal):
try:
value = context.format_number(value, places=datatype.decimals)
except InvalidOperation:
pass
# Reçoit des str en POST
if not type(value) is unicode:
value = unicode(value, 'utf8')
if readonly:
tagname = u"div"
attributes = {u"class": [u"readonly"]}
content = XMLContent.encode(value)
else:
tagname = u"input"
attributes = {
u"type": u"text",
u"id": u"field_{name}".format(name=name),
u"name": name, u"value": XMLAttribute.encode(value),
u"size": str(datatype.size),
u"maxlength": str(datatype.length)}
content = u""
# Right-align numeric fields
if issubclass(datatype, Numeric):
attributes.setdefault(u"class", []).append(u"num")
attributes.setdefault(u"style", []).append(u"text-align:right")
# Check for errors
check_errors(context, form, datatype, name, value, schema, fields,
readonly, attributes, tabindex=None)
return make_element(tagname, attributes, content)
def file_widget(context, form, datatype, name, value, schema, fields,
readonly, tabindex=None):
if readonly:
tagname = u"div"
attributes = {u"class": [u"readonly"]}
content = XMLContent.encode(value)
else:
tagname = u"input"
attributes = {
u"type": u"file",
u"id": u"field_{name}".format(name=name),
u"name": name, u"value": XMLAttribute.encode(value),
u"size": str(datatype.size),
u"maxlength": str(datatype.length)}
content = u""
# Check for errors
check_errors(context, form, datatype, name, value, schema, fields,
readonly, attributes, tabindex=None)
html = make_element(tagname, attributes, content)
# Preview
if not value:
return html
resource = form.parent.get_resource(value, soft=True)
if resource is None:
return html
context = get_context()
link = context.get_link(resource)
href = u"{0}/;download".format(link)
src = u"{0}/;thumb?width=128&height=128".format(link)
preview = make_element(u"a", {u"href": href, u"target": u"_new"},
make_element(u"img", {u"src": src}))
field_id = u"field_{name}_delete".format(name=name)
if readonly:
delete = u""
else:
delete = make_element(u"input", {
u"type": u"checkbox",
u"id": field_id,
u"name": u"{name}_delete".format(name=name),
u"value": u"1"},
make_element(u"label", {u"for": field_id}, MSG(u"Delete")))
html = html + preview + delete
return html
def get_input_widget(name, form, schema, fields, context, tabindex=None,
readonly=False, skip_print=False):
# Always take data from the handler, we store wrong values anyway
form = form.get_form()
handler = form.get_value('data')
if handler:
value = handler.get_value(name, schema)
else:
value = ''
datatype = schema[name]
readonly = readonly or datatype.readonly
widget = None
if isinstance(datatype, Numeric):
widget = text_widget
elif issubclass(datatype, FileImage):
widget = file_widget
elif issubclass(datatype, Text):
widget = textarea_widget
elif issubclass(datatype, (EnumBoolean, SqlEnumerate)):
representation = datatype.representation
widget = {
'select': select_widget,
'radio': radio_widget,
'checkbox': checkbox_widget}.get(representation)
if widget is None:
raise ValueError, representation
else:
widget = text_widget
html = widget(context, form, datatype, name, value, schema, fields,
readonly, tabindex)
if skip_print is False:
help = datatype.help
if not help and isinstance(datatype, (NumDate, NumTime)):
help = MSG(u"Format {format}").gettext(format=datatype.type)
if help:
html = make_element(u"div", {u"title": help, u"rel": u"tooltip"},
html)
return html