author | sylvain.thenault@logilab.fr |
Thu, 30 Apr 2009 00:41:52 +0200 | |
branch | tls-sprint |
changeset 1554 | 3a3263df6cdd |
parent 1360 | 13ae1121835e |
child 1641 | 2c80b09d8d86 |
permissions | -rw-r--r-- |
0 | 1 |
"""widgets for entity edition |
2 |
||
3 |
those are in cubicweb.common since we need to know available widgets at schema |
|
4 |
serialization time |
|
5 |
||
6 |
:organization: Logilab |
|
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
488
diff
changeset
|
7 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 8 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
9 |
""" |
|
10 |
__docformat__ = "restructuredtext en" |
|
11 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
891
diff
changeset
|
12 |
from datetime import datetime |
0 | 13 |
|
14 |
from logilab.mtconverter import html_escape |
|
15 |
||
16 |
from yams.constraints import SizeConstraint, StaticVocabularyConstraint |
|
17 |
||
18 |
from cubicweb.common.uilib import toggle_action |
|
19 |
from cubicweb.web import INTERNAL_FIELD_VALUE, eid_param |
|
20 |
||
21 |
def _format_attrs(kwattrs): |
|
22 |
"""kwattrs is the dictionary of the html attributes available for |
|
23 |
the edited element |
|
24 |
""" |
|
25 |
# sort for predictability (required for tests) |
|
26 |
return u' '.join(sorted(u'%s="%s"' % item for item in kwattrs.iteritems())) |
|
27 |
||
28 |
def _value_from_values(values): |
|
29 |
# take care, value may be 0, 0.0... |
|
30 |
if values: |
|
31 |
value = values[0] |
|
32 |
if value is None: |
|
33 |
value = u'' |
|
34 |
else: |
|
35 |
value = u'' |
|
36 |
return value |
|
37 |
||
38 |
def _eclass_eschema(eschema_or_eclass): |
|
39 |
try: |
|
40 |
return eschema_or_eclass, eschema_or_eclass.e_schema |
|
41 |
except AttributeError: |
|
42 |
return None, eschema_or_eclass |
|
43 |
||
44 |
def checkbox(name, value, attrs='', checked=None): |
|
45 |
if checked is None: |
|
46 |
checked = value |
|
47 |
checked = checked and 'checked="checked"' or '' |
|
48 |
return u'<input type="checkbox" name="%s" value="%s" %s %s />' % ( |
|
49 |
name, value, checked, attrs) |
|
50 |
||
51 |
def widget(vreg, subjschema, rschema, objschema, role='object'): |
|
52 |
"""get a widget to edit the given relation""" |
|
53 |
if rschema == 'eid': |
|
54 |
# return HiddenWidget(vreg, subjschema, rschema, objschema) |
|
55 |
return EidWidget(vreg, _eclass_eschema(subjschema)[1], rschema, objschema) |
|
56 |
return widget_factory(vreg, subjschema, rschema, objschema, role=role) |
|
57 |
||
58 |
||
59 |
class Widget(object): |
|
60 |
"""abstract widget class""" |
|
61 |
need_multipart = False |
|
62 |
# generate the "id" attribute with the same value as the "name" (html) attribute |
|
63 |
autoid = True |
|
64 |
html_attributes = set(('id', 'class', 'tabindex', 'accesskey', 'onchange', 'onkeypress')) |
|
65 |
cubicwebns_attributes = set() |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
66 |
|
0 | 67 |
def __init__(self, vreg, subjschema, rschema, objschema, |
68 |
role='subject', description=None, |
|
69 |
**kwattrs): |
|
70 |
self.vreg = vreg |
|
71 |
self.rschema = rschema |
|
72 |
self.subjtype = subjschema |
|
73 |
self.objtype = objschema |
|
74 |
self.role = role |
|
75 |
self.name = rschema.type |
|
76 |
self.description = description |
|
77 |
self.attrs = kwattrs |
|
78 |
# XXX accesskey may not be unique |
|
79 |
kwattrs['accesskey'] = self.name[0] |
|
80 |
||
81 |
def copy(self): |
|
82 |
"""shallow copy (useful when you need to modify self.attrs |
|
83 |
because widget instances are cached) |
|
84 |
""" |
|
85 |
# brute force copy (subclasses don't have the |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
86 |
# same __init__ prototype) |
0 | 87 |
widget = self.__new__(self.__class__) |
88 |
widget.__dict__ = dict(self.__dict__) |
|
89 |
widget.attrs = dict(widget.attrs) |
|
90 |
return widget |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
91 |
|
0 | 92 |
@staticmethod |
93 |
def size_constraint_attrs(attrs, maxsize): |
|
94 |
"""set html attributes in the attrs dict to consider maxsize""" |
|
95 |
pass |
|
96 |
||
97 |
def format_attrs(self): |
|
98 |
"""return a string with html attributes available for the edit input""" |
|
99 |
# sort for predictability (required for tests) |
|
100 |
attrs = [] |
|
101 |
for name, value in self.attrs.iteritems(): |
|
102 |
# namespace attributes have priority over standard xhtml ones |
|
103 |
if name in self.cubicwebns_attributes: |
|
104 |
attrs.append(u'cubicweb:%s="%s"' % (name, value)) |
|
105 |
elif name in self.html_attributes: |
|
106 |
attrs.append(u'%s="%s"' % (name, value)) |
|
107 |
return u' '.join(sorted(attrs)) |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
108 |
|
0 | 109 |
def required(self, entity): |
110 |
"""indicates if the widget needs a value to be filled in""" |
|
111 |
card = self.rschema.cardinality(self.subjtype, self.objtype, self.role) |
|
112 |
return card in '1+' |
|
113 |
||
114 |
def input_id(self, entity): |
|
115 |
try: |
|
116 |
return self.rname |
|
117 |
except AttributeError: |
|
118 |
return eid_param(self.name, entity.eid) |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
119 |
|
0 | 120 |
def render_label(self, entity, label=None): |
121 |
"""render widget's label""" |
|
122 |
label = label or self.rschema.display_name(entity.req, self.role) |
|
123 |
forid = self.input_id(entity) |
|
124 |
if forid: |
|
125 |
forattr = ' for="%s"' % forid |
|
126 |
else: |
|
127 |
forattr = '' |
|
128 |
if self.required(entity): |
|
129 |
label = u'<label class="required"%s>%s</label>' % (forattr, label) |
|
130 |
else: |
|
131 |
label = u'<label%s>%s</label>' % (forattr, label) |
|
132 |
return label |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
133 |
|
0 | 134 |
def render_error(self, entity): |
135 |
"""return validation error for widget's field of the given entity, if |
|
136 |
any |
|
137 |
""" |
|
138 |
errex = entity.req.data.get('formerrors') |
|
139 |
if errex and errex.eid == entity.eid and self.name in errex.errors: |
|
140 |
entity.req.data['displayederrors'].add(self.name) |
|
141 |
return u'<span class="error">%s</span>' % errex.errors[self.name] |
|
142 |
return u'' |
|
143 |
||
144 |
def render_help(self, entity): |
|
145 |
"""render a help message about the (edited) field""" |
|
146 |
req = entity.req |
|
147 |
help = [u'<br/>'] |
|
148 |
descr = self.description or self.rschema.rproperty(self.subjtype, self.objtype, 'description') |
|
149 |
if descr: |
|
150 |
help.append(u'<span class="helper">%s</span>' % req._(descr)) |
|
151 |
example = self.render_example(req) |
|
152 |
if example: |
|
153 |
help.append(u'<span class="helper">(%s: %s)</span>' |
|
154 |
% (req._('sample format'), example)) |
|
155 |
return u' '.join(help) |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
156 |
|
0 | 157 |
def render_example(self, req): |
158 |
return u'' |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
159 |
|
0 | 160 |
def render(self, entity): |
161 |
"""render the widget for a simple view""" |
|
162 |
if not entity.has_eid(): |
|
163 |
return u'' |
|
164 |
return entity.printable_value(self.name) |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
165 |
|
0 | 166 |
def edit_render(self, entity, tabindex=None, |
167 |
includehelp=False, useid=None, **kwargs): |
|
168 |
"""render the widget for edition""" |
|
169 |
# this is necessary to handle multiple edition |
|
170 |
self.rname = eid_param(self.name, entity.eid) |
|
171 |
if useid: |
|
172 |
self.attrs['id'] = useid |
|
173 |
elif self.autoid: |
|
174 |
self.attrs['id'] = self.rname |
|
175 |
if tabindex is not None: |
|
176 |
self.attrs['tabindex'] = tabindex |
|
177 |
else: |
|
178 |
self.attrs['tabindex'] = entity.req.next_tabindex() |
|
179 |
output = self._edit_render(entity, **kwargs) |
|
180 |
if includehelp: |
|
181 |
output += self.render_help(entity) |
|
182 |
return output |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
183 |
|
0 | 184 |
def _edit_render(self, entity): |
185 |
"""do the actual job to render the widget for edition""" |
|
186 |
raise NotImplementedError |
|
187 |
||
188 |
def current_values(self, entity): |
|
189 |
"""return the value of the field associated to this widget on the given |
|
190 |
entity. always return a list of values, which'll have size equal to 1 |
|
191 |
if the field is monovalued (like all attribute fields, but not all non |
|
192 |
final relation fields |
|
193 |
""" |
|
194 |
if self.rschema.is_final(): |
|
195 |
return entity.attribute_values(self.name) |
|
196 |
elif entity.has_eid(): |
|
197 |
return [row[0] for row in entity.related(self.name, self.role)] |
|
198 |
return () |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
199 |
|
0 | 200 |
def current_value(self, entity): |
201 |
return _value_from_values(self.current_values(entity)) |
|
202 |
||
203 |
def current_display_values(self, entity): |
|
204 |
"""same as .current_values but consider values stored in session in case |
|
205 |
of validation error |
|
206 |
""" |
|
207 |
values = entity.req.data.get('formvalues') |
|
208 |
if values is None: |
|
209 |
return self.current_values(entity) |
|
210 |
cdvalues = values.get(self.rname) |
|
211 |
if cdvalues is None: |
|
212 |
return self.current_values(entity) |
|
213 |
if not isinstance(cdvalues, (list, tuple)): |
|
214 |
cdvalues = (cdvalues,) |
|
215 |
return cdvalues |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
216 |
|
0 | 217 |
def current_display_value(self, entity): |
218 |
"""same as .current_value but consider values stored in session in case |
|
219 |
of validation error |
|
220 |
""" |
|
221 |
return _value_from_values(self.current_display_values(entity)) |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
222 |
|
0 | 223 |
def hidden_input(self, entity, qvalue): |
224 |
"""return an hidden field which |
|
225 |
1. indicates that a field is edited |
|
226 |
2. hold the old value to easily detect if the field has been modified |
|
227 |
||
228 |
`qvalue` is the html quoted old value |
|
229 |
""" |
|
230 |
if self.role == 'subject': |
|
231 |
editmark = 'edits' |
|
232 |
else: |
|
233 |
editmark = 'edito' |
|
234 |
if qvalue is None or not entity.has_eid(): |
|
235 |
qvalue = INTERNAL_FIELD_VALUE |
|
236 |
return u'<input type="hidden" name="%s-%s" value="%s"/>\n' % ( |
|
237 |
editmark, self.rname, qvalue) |
|
238 |
||
239 |
class InputWidget(Widget): |
|
240 |
"""abstract class for input generating a <input> tag""" |
|
241 |
input_type = None |
|
242 |
html_attributes = Widget.html_attributes | set(('type', 'name', 'value')) |
|
243 |
||
244 |
def _edit_render(self, entity): |
|
245 |
value = self.current_value(entity) |
|
246 |
dvalue = self.current_display_value(entity) |
|
247 |
if isinstance(value, basestring): |
|
248 |
value = html_escape(value) |
|
249 |
if isinstance(dvalue, basestring): |
|
250 |
dvalue = html_escape(dvalue) |
|
251 |
return u'%s<input type="%s" name="%s" value="%s" %s/>' % ( |
|
252 |
self.hidden_input(entity, value), self.input_type, |
|
253 |
self.rname, dvalue, self.format_attrs()) |
|
254 |
||
255 |
class HiddenWidget(InputWidget): |
|
256 |
input_type = 'hidden' |
|
257 |
autoid = False |
|
258 |
def __init__(self, vreg, subjschema, rschema, objschema, |
|
259 |
role='subject', **kwattrs): |
|
260 |
InputWidget.__init__(self, vreg, subjschema, rschema, objschema, |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
261 |
role='subject', |
0 | 262 |
**kwattrs) |
263 |
# disable access key |
|
264 |
del self.attrs['accesskey'] |
|
265 |
||
266 |
def current_value(self, entity): |
|
267 |
value = InputWidget.current_value(self, entity) |
|
268 |
return value or INTERNAL_FIELD_VALUE |
|
269 |
||
270 |
def current_display_value(self, entity): |
|
271 |
value = InputWidget.current_display_value(self, entity) |
|
272 |
return value or INTERNAL_FIELD_VALUE |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
273 |
|
0 | 274 |
def render_label(self, entity, label=None): |
275 |
"""render widget's label""" |
|
276 |
return u'' |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
277 |
|
0 | 278 |
def render_help(self, entity): |
279 |
return u'' |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
280 |
|
0 | 281 |
def hidden_input(self, entity, value): |
282 |
"""no hidden input for hidden input""" |
|
283 |
return '' |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
284 |
|
0 | 285 |
|
286 |
class EidWidget(HiddenWidget): |
|
287 |
||
288 |
def _edit_render(self, entity): |
|
289 |
return u'<input type="hidden" name="eid" value="%s" />' % entity.eid |
|
290 |
||
291 |
||
292 |
class StringWidget(InputWidget): |
|
293 |
input_type = 'text' |
|
294 |
html_attributes = InputWidget.html_attributes | set(('size', 'maxlength')) |
|
295 |
@staticmethod |
|
296 |
def size_constraint_attrs(attrs, maxsize): |
|
297 |
"""set html attributes in the attrs dict to consider maxsize""" |
|
298 |
attrs['size'] = min(maxsize, 40) |
|
299 |
attrs['maxlength'] = maxsize |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
300 |
|
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
301 |
|
0 | 302 |
class AutoCompletionWidget(StringWidget): |
303 |
cubicwebns_attributes = (StringWidget.cubicwebns_attributes | |
|
304 |
set(('accesskey', 'size', 'maxlength'))) |
|
305 |
attrs = () |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
306 |
|
0 | 307 |
wdgtype = 'SuggestField' |
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
308 |
|
0 | 309 |
def current_value(self, entity): |
310 |
value = StringWidget.current_value(self, entity) |
|
311 |
return value or INTERNAL_FIELD_VALUE |
|
312 |
||
313 |
def _get_url(self, entity): |
|
314 |
return entity.req.build_url('json', fname=entity.autocomplete_initfuncs[self.rschema], |
|
315 |
pageid=entity.req.pageid, mode='remote') |
|
316 |
||
317 |
def _edit_render(self, entity): |
|
318 |
req = entity.req |
|
319 |
req.add_js( ('cubicweb.widgets.js', 'jquery.autocomplete.js') ) |
|
320 |
req.add_css('jquery.autocomplete.css') |
|
321 |
value = self.current_value(entity) |
|
322 |
dvalue = self.current_display_value(entity) |
|
323 |
if isinstance(value, basestring): |
|
324 |
value = html_escape(value) |
|
325 |
if isinstance(dvalue, basestring): |
|
326 |
dvalue = html_escape(dvalue) |
|
327 |
iid = self.attrs.pop('id') |
|
328 |
if self.required(entity): |
|
329 |
cssclass = u' required' |
|
330 |
else: |
|
331 |
cssclass = u'' |
|
332 |
dataurl = self._get_url(entity) |
|
333 |
return (u'%(hidden)s<input type="text" name="%(iid)s" value="%(value)s" cubicweb:dataurl="%(url)s" class="widget%(required)s" id="%(iid)s" ' |
|
334 |
u'tabindex="%(tabindex)s" cubicweb:loadtype="auto" cubicweb:wdgtype="%(wdgtype)s" %(attrs)s />' % { |
|
335 |
'iid': iid, |
|
336 |
'hidden': self.hidden_input(entity, value), |
|
337 |
'wdgtype': self.wdgtype, |
|
338 |
'url': html_escape(dataurl), |
|
339 |
'tabindex': self.attrs.pop('tabindex'), |
|
340 |
'value': dvalue, |
|
341 |
'attrs': self.format_attrs(), |
|
342 |
'required' : cssclass, |
|
343 |
}) |
|
344 |
||
345 |
class StaticFileAutoCompletionWidget(AutoCompletionWidget): |
|
346 |
wdgtype = 'StaticFileSuggestField' |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
347 |
|
0 | 348 |
def _get_url(self, entity): |
349 |
return entity.req.datadir_url + entity.autocomplete_initfuncs[self.rschema] |
|
350 |
||
351 |
class RestrictedAutoCompletionWidget(AutoCompletionWidget): |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
352 |
wdgtype = 'RestrictedSuggestField' |
0 | 353 |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
354 |
|
0 | 355 |
class PasswordWidget(InputWidget): |
356 |
input_type = 'password' |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
357 |
|
0 | 358 |
def required(self, entity): |
359 |
if InputWidget.required(self, entity) and not entity.has_eid(): |
|
360 |
return True |
|
361 |
return False |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
362 |
|
0 | 363 |
def current_values(self, entity): |
364 |
# on existant entity, show password field has non empty (we don't have |
|
365 |
# the actual value |
|
366 |
if entity.has_eid(): |
|
367 |
return (INTERNAL_FIELD_VALUE,) |
|
368 |
return super(PasswordWidget, self).current_values(entity) |
|
369 |
||
370 |
def _edit_render(self, entity): |
|
371 |
html = super(PasswordWidget, self)._edit_render(entity) |
|
372 |
name = eid_param(self.name + '-confirm', entity.eid) |
|
373 |
return u'%s<br/>\n<input type="%s" name="%s" id="%s" tabindex="%s"/> <span class="emphasis">(%s)</span>' % ( |
|
374 |
html, self.input_type, name, name, entity.req.next_tabindex(), |
|
375 |
entity.req._('confirm password')) |
|
376 |
||
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
377 |
|
0 | 378 |
class TextWidget(Widget): |
379 |
html_attributes = Widget.html_attributes | set(('rows', 'cols')) |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
380 |
|
0 | 381 |
@staticmethod |
382 |
def size_constraint_attrs(attrs, maxsize): |
|
383 |
"""set html attributes in the attrs dict to consider maxsize""" |
|
384 |
if 256 < maxsize < 513: |
|
385 |
attrs['cols'], attrs['rows'] = 60, 5 |
|
386 |
else: |
|
387 |
attrs['cols'], attrs['rows'] = 80, 10 |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
388 |
|
0 | 389 |
def render(self, entity): |
390 |
if not entity.has_eid(): |
|
391 |
return u'' |
|
392 |
return entity.printable_value(self.name) |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
393 |
|
0 | 394 |
def _edit_render(self, entity, with_format=True): |
395 |
req = entity.req |
|
396 |
editor = self._edit_render_textarea(entity, with_format) |
|
397 |
value = self.current_value(entity) |
|
398 |
if isinstance(value, basestring): |
|
399 |
value = html_escape(value) |
|
400 |
return u'%s%s' % (self.hidden_input(entity, value), editor) |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
401 |
|
0 | 402 |
def _edit_render_textarea(self, entity, with_format): |
403 |
self.attrs.setdefault('cols', 80) |
|
404 |
self.attrs.setdefault('rows', 20) |
|
405 |
dvalue = self.current_display_value(entity) |
|
406 |
if isinstance(dvalue, basestring): |
|
407 |
dvalue = html_escape(dvalue) |
|
408 |
if entity.use_fckeditor(self.name): |
|
890
3530baff9120
make fckeditor actually optional, fix its config, avoid needs for a link to fckeditor.js
sylvain.thenault@logilab.fr
parents:
849
diff
changeset
|
409 |
entity.req.fckeditor_config() |
0 | 410 |
if with_format: |
411 |
if entity.has_eid(): |
|
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1325
diff
changeset
|
412 |
format = entity.attr_metadata(self.name, 'format') |
0 | 413 |
else: |
414 |
format = '' |
|
415 |
frname = eid_param(self.name + '_format', entity.eid) |
|
416 |
hidden = u'<input type="hidden" name="edits-%s" value="%s"/>\n'\ |
|
417 |
'<input type="hidden" name="%s" value="text/html"/>\n' % ( |
|
418 |
frname, format, frname) |
|
419 |
return u'%s<textarea cubicweb:type="wysiwyg" onkeypress="autogrow(this)" name="%s" %s>%s</textarea>' % ( |
|
420 |
hidden, self.rname, self.format_attrs(), dvalue) |
|
1107
961a478593a5
has_metadata is a schema method
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
421 |
if with_format and entity.e_schema.has_metadata(self.name, 'format'): |
0 | 422 |
fmtwdg = entity.get_widget(self.name + '_format') |
423 |
fmtwdgstr = fmtwdg.edit_render(entity, tabindex=self.attrs['tabindex']) |
|
424 |
self.attrs['tabindex'] = entity.req.next_tabindex() |
|
425 |
else: |
|
426 |
fmtwdgstr = '' |
|
427 |
return u'%s<br/><textarea onkeypress="autogrow(this)" name="%s" %s>%s</textarea>' % ( |
|
428 |
fmtwdgstr, self.rname, self.format_attrs(), dvalue) |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
429 |
|
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
430 |
|
0 | 431 |
class CheckBoxWidget(Widget): |
432 |
html_attributes = Widget.html_attributes | set(('checked', )) |
|
433 |
def _edit_render(self, entity): |
|
434 |
value = self.current_value(entity) |
|
435 |
dvalue = self.current_display_value(entity) |
|
436 |
return self.hidden_input(entity, value) + checkbox(self.rname, 'checked', self.format_attrs(), dvalue) |
|
437 |
||
438 |
def render(self, entity): |
|
439 |
if not entity.has_eid(): |
|
440 |
return u'' |
|
441 |
if getattr(entity, self.name): |
|
442 |
return entity.req._('yes') |
|
443 |
return entity.req._('no') |
|
444 |
||
445 |
||
446 |
class YesNoRadioWidget(CheckBoxWidget): |
|
1190
15fc369bc3ca
add 'disabled' in html_attributes for YesNoRadioWidget
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
488
diff
changeset
|
447 |
html_attributes = Widget.html_attributes | set(('disabled',)) |
0 | 448 |
def _edit_render(self, entity): |
449 |
value = self.current_value(entity) |
|
450 |
dvalue = self.current_display_value(entity) |
|
451 |
attrs1 = self.format_attrs() |
|
452 |
del self.attrs['id'] # avoid duplicate id for xhtml compliance |
|
453 |
attrs2 = self.format_attrs() |
|
454 |
if dvalue: |
|
455 |
attrs1 += ' checked="checked"' |
|
456 |
else: |
|
457 |
attrs2 += ' checked="checked"' |
|
458 |
wdgs = [self.hidden_input(entity, value), |
|
459 |
u'<input type="radio" name="%s" value="1" %s/>%s<br/>' % (self.rname, attrs1, entity.req._('yes')), |
|
460 |
u'<input type="radio" name="%s" value="" %s/>%s<br/>' % (self.rname, attrs2, entity.req._('no'))] |
|
461 |
return '\n'.join(wdgs) |
|
462 |
||
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
463 |
|
0 | 464 |
class FileWidget(Widget): |
465 |
need_multipart = True |
|
466 |
def _file_wdg(self, entity): |
|
467 |
wdgs = [u'<input type="file" name="%s" %s/>' % (self.rname, self.format_attrs())] |
|
468 |
req = entity.req |
|
1107
961a478593a5
has_metadata is a schema method
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
469 |
if (entity.e_schema.has_metadata(self.name, 'format') |
961a478593a5
has_metadata is a schema method
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
470 |
or entity.e_schema.has_metadata(self.name, 'encoding')): |
0 | 471 |
divid = '%s-%s-advanced' % (self.name, entity.eid) |
472 |
wdgs.append(u'<a href="%s" title="%s"><img src="%s" alt="%s"/></a>' % |
|
473 |
(html_escape(toggle_action(divid)), |
|
474 |
req._('show advanced fields'), |
|
475 |
html_escape(req.build_url('data/puce_down.png')), |
|
476 |
req._('show advanced fields'))) |
|
477 |
wdgs.append(u'<div id="%s" class="hidden">' % divid) |
|
478 |
for extraattr in ('_format', '_encoding'): |
|
479 |
if entity.e_schema.has_subject_relation('%s%s' % (self.name, extraattr)): |
|
480 |
ewdg = entity.get_widget(self.name + extraattr) |
|
481 |
wdgs.append(ewdg.render_label(entity)) |
|
482 |
wdgs.append(ewdg.edit_render(entity, includehelp=True)) |
|
483 |
wdgs.append(u'<br/>') |
|
484 |
wdgs.append(u'</div>') |
|
1211
90bb6e89e356
add the name of the currently attached file, if the file already exists (i.e in the edition form)
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
1190
diff
changeset
|
485 |
if entity.has_eid(): |
90bb6e89e356
add the name of the currently attached file, if the file already exists (i.e in the edition form)
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
1190
diff
changeset
|
486 |
if not self.required(entity): |
90bb6e89e356
add the name of the currently attached file, if the file already exists (i.e in the edition form)
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
1190
diff
changeset
|
487 |
# trick to be able to delete an uploaded file |
90bb6e89e356
add the name of the currently attached file, if the file already exists (i.e in the edition form)
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
1190
diff
changeset
|
488 |
wdgs.append(u'<br/>') |
90bb6e89e356
add the name of the currently attached file, if the file already exists (i.e in the edition form)
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
1190
diff
changeset
|
489 |
wdgs.append(checkbox(eid_param('__%s_detach' % self.rname, entity.eid), False)) |
90bb6e89e356
add the name of the currently attached file, if the file already exists (i.e in the edition form)
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
1190
diff
changeset
|
490 |
wdgs.append(req._('detach attached file %s' % entity.dc_title())) |
90bb6e89e356
add the name of the currently attached file, if the file already exists (i.e in the edition form)
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
1190
diff
changeset
|
491 |
else: |
90bb6e89e356
add the name of the currently attached file, if the file already exists (i.e in the edition form)
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
1190
diff
changeset
|
492 |
wdgs.append(u'<br/>') |
90bb6e89e356
add the name of the currently attached file, if the file already exists (i.e in the edition form)
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
1190
diff
changeset
|
493 |
wdgs.append(req._('currently attached file: %s' % entity.dc_title())) |
0 | 494 |
return '\n'.join(wdgs) |
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
495 |
|
0 | 496 |
def _edit_render(self, entity): |
497 |
return self.hidden_input(entity, None) + self._file_wdg(entity) |
|
498 |
||
499 |
||
500 |
class TextFileWidget(FileWidget): |
|
501 |
def _edit_msg(self, entity): |
|
502 |
if entity.has_eid() and not self.required(entity): |
|
503 |
msg = entity.req._( |
|
504 |
'You can either submit a new file using the browse button above' |
|
505 |
', or choose to remove already uploaded file by checking the ' |
|
506 |
'"detach attached file" check-box, or edit file content online ' |
|
507 |
'with the widget below.') |
|
508 |
else: |
|
509 |
msg = entity.req._( |
|
510 |
'You can either submit a new file using the browse button above' |
|
511 |
', or edit file content online with the widget below.') |
|
512 |
return msg |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
513 |
|
0 | 514 |
def _edit_render(self, entity): |
515 |
wdgs = [self._file_wdg(entity)] |
|
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1325
diff
changeset
|
516 |
if entity.attr_metadata(self.name, 'format') in ('text/plain', 'text/html', 'text/rest'): |
0 | 517 |
msg = self._edit_msg(entity) |
518 |
wdgs.append(u'<p><b>%s</b></p>' % msg) |
|
519 |
twdg = TextWidget(self.vreg, self.subjtype, self.rschema, self.objtype) |
|
520 |
twdg.rname = self.rname |
|
521 |
data = getattr(entity, self.name) |
|
522 |
if data: |
|
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1325
diff
changeset
|
523 |
encoding = entity.attr_metadata(self.name, 'encoding') |
0 | 524 |
try: |
525 |
entity[self.name] = unicode(data.getvalue(), encoding) |
|
526 |
except UnicodeError: |
|
527 |
pass |
|
528 |
else: |
|
529 |
wdgs.append(twdg.edit_render(entity, with_format=False)) |
|
530 |
entity[self.name] = data # restore Binary value |
|
531 |
wdgs.append(u'<br/>') |
|
532 |
return '\n'.join(wdgs) |
|
533 |
||
534 |
||
535 |
class ComboBoxWidget(Widget): |
|
536 |
html_attributes = Widget.html_attributes | set(('multiple', 'size')) |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
537 |
|
0 | 538 |
def __init__(self, vreg, subjschema, rschema, objschema, |
539 |
multiple=False, **kwattrs): |
|
540 |
super(ComboBoxWidget, self).__init__(vreg, subjschema, rschema, objschema, |
|
541 |
**kwattrs) |
|
542 |
if multiple: |
|
543 |
self.attrs['multiple'] = 'multiple' |
|
544 |
if not 'size' in self.attrs: |
|
545 |
self.attrs['size'] = '5' |
|
546 |
# disable access key (dunno why but this is not allowed by xhtml 1.0) |
|
547 |
del self.attrs['accesskey'] |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
548 |
|
0 | 549 |
def vocabulary(self, entity): |
550 |
raise NotImplementedError() |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
551 |
|
0 | 552 |
def form_value(self, entity, value, values): |
553 |
if value in values: |
|
554 |
flag = 'selected="selected"' |
|
555 |
else: |
|
556 |
flag = '' |
|
557 |
return value, flag |
|
558 |
||
559 |
def _edit_render(self, entity): |
|
560 |
values = self.current_values(entity) |
|
561 |
if values: |
|
562 |
res = [self.hidden_input(entity, v) for v in values] |
|
563 |
else: |
|
564 |
res = [self.hidden_input(entity, INTERNAL_FIELD_VALUE)] |
|
565 |
dvalues = self.current_display_values(entity) |
|
566 |
res.append(u'<select name="%s" %s>' % (self.rname, self.format_attrs())) |
|
567 |
for label, value in self.vocabulary(entity): |
|
568 |
if value is None: |
|
569 |
# handle separator |
|
570 |
res.append(u'<optgroup label="%s"/>' % (label or '')) |
|
571 |
else: |
|
572 |
value, flag = self.form_value(entity, value, dvalues) |
|
573 |
res.append(u'<option value="%s" %s>%s</option>' % (value, flag, html_escape(label))) |
|
574 |
res.append(u'</select>') |
|
575 |
return '\n'.join(res) |
|
576 |
||
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
577 |
|
0 | 578 |
class StaticComboBoxWidget(ComboBoxWidget): |
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
579 |
|
0 | 580 |
def __init__(self, vreg, subjschema, rschema, objschema, |
581 |
vocabfunc, multiple=False, sort=False, **kwattrs): |
|
582 |
super(StaticComboBoxWidget, self).__init__(vreg, subjschema, rschema, objschema, |
|
583 |
multiple, **kwattrs) |
|
584 |
self.sort = sort |
|
585 |
self.vocabfunc = vocabfunc |
|
586 |
||
587 |
def vocabulary(self, entity): |
|
1227
e8b7c7407edf
fix : vocabulary method in StaticVocabularyConstraint class (yams.constraint.py) takes only keyword arguments
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
1211
diff
changeset
|
588 |
choices = self.vocabfunc(entity=entity) |
0 | 589 |
if self.sort: |
590 |
choices = sorted(choices) |
|
591 |
if self.rschema.rproperty(self.subjtype, self.objtype, 'internationalizable'): |
|
592 |
return zip((entity.req._(v) for v in choices), choices) |
|
593 |
return zip(choices, choices) |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
594 |
|
0 | 595 |
|
596 |
class EntityLinkComboBoxWidget(ComboBoxWidget): |
|
597 |
"""to be used be specific forms""" |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
598 |
|
0 | 599 |
def current_values(self, entity): |
600 |
if entity.has_eid(): |
|
601 |
return [r[0] for r in entity.related(self.name, self.role)] |
|
602 |
defaultmeth = 'default_%s_%s' % (self.role, self.name) |
|
603 |
if hasattr(entity, defaultmeth): |
|
604 |
return getattr(entity, defaultmeth)() |
|
605 |
return () |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
606 |
|
0 | 607 |
def vocabulary(self, entity): |
608 |
return [('', INTERNAL_FIELD_VALUE)] + entity.vocabulary(self.rschema, self.role) |
|
609 |
||
610 |
||
611 |
class RawDynamicComboBoxWidget(EntityLinkComboBoxWidget): |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
612 |
|
0 | 613 |
def vocabulary(self, entity, limit=None): |
614 |
req = entity.req |
|
615 |
# first see if its specified by __linkto form parameters |
|
616 |
linkedto = entity.linked_to(self.name, self.role) |
|
617 |
if linkedto: |
|
618 |
entities = (req.eid_rset(eid).get_entity(0, 0) for eid in linkedto) |
|
619 |
return [(entity.view('combobox'), entity.eid) for entity in entities] |
|
620 |
# it isn't, check if the entity provides a method to get correct values |
|
621 |
if not self.required(entity): |
|
622 |
res = [('', INTERNAL_FIELD_VALUE)] |
|
623 |
else: |
|
624 |
res = [] |
|
625 |
# vocabulary doesn't include current values, add them |
|
626 |
if entity.has_eid(): |
|
627 |
rset = entity.related(self.name, self.role) |
|
628 |
relatedvocab = [(e.view('combobox'), e.eid) for e in rset.entities()] |
|
629 |
else: |
|
630 |
relatedvocab = [] |
|
631 |
return res + entity.vocabulary(self.rschema, self.role) + relatedvocab |
|
632 |
||
633 |
||
634 |
class DynamicComboBoxWidget(RawDynamicComboBoxWidget): |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
635 |
|
0 | 636 |
def vocabulary(self, entity, limit=None): |
637 |
return sorted(super(DynamicComboBoxWidget, self).vocabulary(entity, limit)) |
|
638 |
||
639 |
||
640 |
class AddComboBoxWidget(DynamicComboBoxWidget): |
|
641 |
def _edit_render(self, entity): |
|
642 |
req = entity.req |
|
643 |
req.add_js( ('cubicweb.ajax.js', 'jquery.js', 'cubicweb.widgets.js') ) |
|
644 |
values = self.current_values(entity) |
|
645 |
if values: |
|
646 |
res = [self.hidden_input(entity, v) for v in values] |
|
647 |
else: |
|
648 |
res = [self.hidden_input(entity, INTERNAL_FIELD_VALUE)] |
|
649 |
dvalues = self.current_display_values(entity) |
|
650 |
etype_from = entity.e_schema.subject_relation(self.name).objects(entity.e_schema)[0] |
|
651 |
res.append(u'<select class="widget" cubicweb:etype_to="%s" cubicweb:etype_from="%s" cubicweb:loadtype="auto" cubicweb:wdgtype="AddComboBox" name="%s" %s>' |
|
652 |
% (entity.e_schema, etype_from, self.rname, self.format_attrs())) |
|
653 |
for label, value in self.vocabulary(entity): |
|
654 |
if value is None: |
|
655 |
# handle separator |
|
656 |
res.append(u'<optgroup label="%s"/>' % (label or '')) |
|
657 |
else: |
|
658 |
value, flag = self.form_value(entity, value, dvalues) |
|
659 |
res.append(u'<option value="%s" %s>%s</option>' % (value, flag, html_escape(label))) |
|
660 |
res.append(u'</select>') |
|
661 |
res.append(u'<div id="newvalue">') |
|
662 |
res.append(u'<input type="text" id="newopt" />') |
|
663 |
res.append(u'<a href="javascript:noop()" id="add_newopt"> </a></div>') |
|
664 |
return '\n'.join(res) |
|
665 |
||
891 | 666 |
|
0 | 667 |
class IntegerWidget(StringWidget): |
668 |
def __init__(self, vreg, subjschema, rschema, objschema, **kwattrs): |
|
669 |
kwattrs['size'] = 5 |
|
670 |
kwattrs['maxlength'] = 15 |
|
671 |
StringWidget.__init__(self, vreg, subjschema, rschema, objschema, **kwattrs) |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
672 |
|
0 | 673 |
def render_example(self, req): |
674 |
return '23' |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
675 |
|
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
676 |
|
0 | 677 |
class FloatWidget(StringWidget): |
678 |
def __init__(self, vreg, subjschema, rschema, objschema, **kwattrs): |
|
679 |
kwattrs['size'] = 5 |
|
680 |
kwattrs['maxlength'] = 15 |
|
681 |
StringWidget.__init__(self, vreg, subjschema, rschema, objschema, **kwattrs) |
|
682 |
||
683 |
def render_example(self, req): |
|
684 |
formatstr = req.property_value('ui.float-format') |
|
685 |
return formatstr % 1.23 |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
686 |
|
0 | 687 |
def current_values(self, entity): |
688 |
values = entity.attribute_values(self.name) |
|
689 |
if values: |
|
690 |
formatstr = entity.req.property_value('ui.float-format') |
|
691 |
value = values[0] |
|
692 |
if value is not None: |
|
693 |
value = float(value) |
|
694 |
else: |
|
695 |
return () |
|
696 |
return [formatstr % value] |
|
697 |
return () |
|
698 |
||
891 | 699 |
|
0 | 700 |
class DecimalWidget(StringWidget): |
701 |
def __init__(self, vreg, subjschema, rschema, objschema, **kwattrs): |
|
702 |
kwattrs['size'] = 5 |
|
703 |
kwattrs['maxlength'] = 15 |
|
704 |
StringWidget.__init__(self, vreg, subjschema, rschema, objschema, **kwattrs) |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
705 |
|
0 | 706 |
def render_example(self, req): |
707 |
return '345.0300' |
|
708 |
||
709 |
||
710 |
class DateWidget(StringWidget): |
|
711 |
format_key = 'ui.date-format' |
|
891 | 712 |
monthnames = ('january', 'february', 'march', 'april', |
713 |
'may', 'june', 'july', 'august', |
|
714 |
'september', 'october', 'november', 'december') |
|
715 |
daynames = ('monday', 'tuesday', 'wednesday', 'thursday', |
|
716 |
'friday', 'saturday', 'sunday') |
|
717 |
||
718 |
@classmethod |
|
719 |
def add_localized_infos(cls, req): |
|
720 |
"""inserts JS variables defining localized months and days""" |
|
721 |
# import here to avoid dependancy from cubicweb-common to simplejson |
|
722 |
_ = req._ |
|
723 |
monthnames = [_(mname) for mname in cls.monthnames] |
|
724 |
daynames = [_(dname) for dname in cls.daynames] |
|
725 |
req.html_headers.define_var('MONTHNAMES', monthnames) |
|
726 |
req.html_headers.define_var('DAYNAMES', daynames) |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
727 |
|
0 | 728 |
def __init__(self, vreg, subjschema, rschema, objschema, **kwattrs): |
729 |
kwattrs.setdefault('size', 10) |
|
730 |
kwattrs.setdefault('maxlength', 10) |
|
731 |
StringWidget.__init__(self, vreg, subjschema, rschema, objschema, **kwattrs) |
|
732 |
||
733 |
def current_values(self, entity): |
|
734 |
values = entity.attribute_values(self.name) |
|
735 |
if values and hasattr(values[0], 'strftime'): |
|
736 |
formatstr = entity.req.property_value(self.format_key) |
|
737 |
return [values[0].strftime(formatstr)] |
|
738 |
return values |
|
739 |
||
740 |
def render_example(self, req): |
|
741 |
formatstr = req.property_value(self.format_key) |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
891
diff
changeset
|
742 |
return datetime.now().strftime(formatstr) |
0 | 743 |
|
744 |
||
745 |
def _edit_render(self, entity): |
|
746 |
wdg = super(DateWidget, self)._edit_render(entity) |
|
747 |
cal_button = self.render_calendar_popup(entity) |
|
748 |
return wdg+cal_button |
|
749 |
||
750 |
def render_help(self, entity): |
|
751 |
"""calendar popup widget""" |
|
752 |
req = entity.req |
|
753 |
help = [ u'<br/>' ] |
|
754 |
descr = self.rschema.rproperty(self.subjtype, self.objtype, 'description') |
|
755 |
if descr: |
|
756 |
help.append('<span class="helper">%s</span>' % req._(descr)) |
|
757 |
example = self.render_example(req) |
|
758 |
if example: |
|
759 |
help.append('<span class="helper">(%s: %s)</span>' |
|
760 |
% (req._('sample format'), example)) |
|
761 |
return u' '.join(help) |
|
762 |
||
763 |
def render_calendar_popup(self, entity): |
|
764 |
"""calendar popup widget""" |
|
765 |
req = entity.req |
|
766 |
self.add_localized_infos(req) |
|
767 |
req.add_js(('cubicweb.ajax.js', 'cubicweb.calendar.js',)) |
|
768 |
req.add_css(('cubicweb.calendar_popup.css',)) |
|
769 |
inputid = self.attrs.get('id', self.rname) |
|
770 |
helperid = "%shelper" % inputid |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
891
diff
changeset
|
771 |
_today = datetime.now() |
0 | 772 |
year = int(req.form.get('year', _today.year)) |
773 |
month = int(req.form.get('month', _today.month)) |
|
774 |
||
775 |
return (u"""<a onclick="toggleCalendar('%s', '%s', %s, %s);" class="calhelper"> |
|
776 |
<img src="%s" title="%s" alt="" /></a><div class="calpopup hidden" id="%s"></div>""" |
|
777 |
% (helperid, inputid, year, month, |
|
778 |
req.external_resource('CALENDAR_ICON'), req._('calendar'), helperid) ) |
|
779 |
||
780 |
class DateTimeWidget(DateWidget): |
|
781 |
format_key = 'ui.datetime-format' |
|
891 | 782 |
|
783 |
def __init__(self, vreg, subjschema, rschema, objschema, **kwattrs): |
|
784 |
kwattrs['size'] = 16 |
|
785 |
kwattrs['maxlength'] = 16 |
|
786 |
DateWidget.__init__(self, vreg, subjschema, rschema, objschema, **kwattrs) |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
787 |
|
0 | 788 |
def render_example(self, req): |
789 |
formatstr1 = req.property_value('ui.datetime-format') |
|
790 |
formatstr2 = req.property_value('ui.date-format') |
|
62
ef06f71533d9
use named substitutions in i18n strings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
791 |
return req._('%(fmt1)s, or without time: %(fmt2)s') % { |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
891
diff
changeset
|
792 |
'fmt1': datetime.now().strftime(formatstr1), |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
891
diff
changeset
|
793 |
'fmt2': datetime.now().strftime(formatstr2), |
62
ef06f71533d9
use named substitutions in i18n strings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
794 |
} |
0 | 795 |
|
796 |
||
797 |
class TimeWidget(StringWidget): |
|
798 |
format_key = 'ui.time-format' |
|
799 |
def __init__(self, vreg, subjschema, rschema, objschema, **kwattrs): |
|
800 |
kwattrs['size'] = 5 |
|
801 |
kwattrs['maxlength'] = 5 |
|
802 |
StringWidget.__init__(self, vreg, subjschema, rschema, objschema, **kwattrs) |
|
803 |
||
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
804 |
|
0 | 805 |
class EmailWidget(StringWidget): |
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
806 |
|
0 | 807 |
def render(self, entity): |
808 |
email = getattr(entity, self.name) |
|
809 |
if not email: |
|
810 |
return u'' |
|
811 |
return u'<a href="mailto:%s">%s</a>' % (email, email) |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
812 |
|
0 | 813 |
class URLWidget(StringWidget): |
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
814 |
|
0 | 815 |
def render(self, entity): |
816 |
url = getattr(entity, self.name) |
|
817 |
if not url: |
|
818 |
return u'' |
|
819 |
url = html_escape(url) |
|
820 |
return u'<a href="%s">%s</a>' % (url, url) |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
821 |
|
0 | 822 |
class EmbededURLWidget(StringWidget): |
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
823 |
|
0 | 824 |
def render(self, entity): |
825 |
url = getattr(entity, self.name) |
|
826 |
if not url: |
|
827 |
return u'' |
|
828 |
aurl = html_escape(entity.build_url('embed', url=url)) |
|
829 |
return u'<a href="%s">%s</a>' % (aurl, url) |
|
830 |
||
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
831 |
|
0 | 832 |
|
833 |
def widget_factory(vreg, subjschema, rschema, objschema, role='subject', |
|
834 |
**kwargs): |
|
835 |
"""return the most adapated widget to edit the relation |
|
836 |
'subjschema rschema objschema' according to information found in the schema |
|
837 |
""" |
|
838 |
if role == 'subject': |
|
839 |
eclass, subjschema = _eclass_eschema(subjschema) |
|
840 |
else: |
|
841 |
eclass, objschema = _eclass_eschema(objschema) |
|
1325
cf79af56fed0
widgets has been dropped from base class
sylvain.thenault@logilab.fr
parents:
1309
diff
changeset
|
842 |
if eclass is not None and rschema in getattr(eclass, 'widgets', ()): |
0 | 843 |
wcls = WIDGETS[eclass.widgets[rschema]] |
844 |
elif not rschema.is_final(): |
|
845 |
card = rschema.rproperty(subjschema, objschema, 'cardinality') |
|
846 |
if role == 'object': |
|
847 |
multiple = card[1] in '+*' |
|
848 |
else: #if role == 'subject': |
|
849 |
multiple = card[0] in '+*' |
|
850 |
return DynamicComboBoxWidget(vreg, subjschema, rschema, objschema, |
|
851 |
role=role, multiple=multiple) |
|
852 |
else: |
|
853 |
wcls = None |
|
854 |
factory = FACTORIES.get(objschema, _default_widget_factory) |
|
855 |
return factory(vreg, subjschema, rschema, objschema, wcls=wcls, |
|
856 |
role=role, **kwargs) |
|
857 |
||
858 |
||
859 |
# factories to find the most adapated widget according to a type and other constraints |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
860 |
|
0 | 861 |
def _string_widget_factory(vreg, subjschema, rschema, objschema, wcls=None, **kwargs): |
862 |
w = None |
|
863 |
for c in rschema.rproperty(subjschema, objschema, 'constraints'): |
|
864 |
if isinstance(c, StaticVocabularyConstraint): |
|
865 |
# may have been set by a previous SizeConstraint but doesn't make sense |
|
866 |
# here (even doesn't have the same meaning on a combobox actually) |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
867 |
kwargs.pop('size', None) |
0 | 868 |
return (wcls or StaticComboBoxWidget)(vreg, subjschema, rschema, objschema, |
869 |
vocabfunc=c.vocabulary, **kwargs) |
|
870 |
if isinstance(c, SizeConstraint) and c.max is not None: |
|
871 |
# don't return here since a StaticVocabularyConstraint may |
|
872 |
# follow |
|
873 |
if wcls is None: |
|
874 |
if c.max < 257: |
|
875 |
_wcls = StringWidget |
|
876 |
else: |
|
877 |
_wcls = TextWidget |
|
878 |
else: |
|
879 |
_wcls = wcls |
|
880 |
_wcls.size_constraint_attrs(kwargs, c.max) |
|
881 |
w = _wcls(vreg, subjschema, rschema, objschema, **kwargs) |
|
882 |
if w is None: |
|
883 |
w = (wcls or TextWidget)(vreg, subjschema, rschema, objschema, **kwargs) |
|
884 |
return w |
|
885 |
||
886 |
def _default_widget_factory(vreg, subjschema, rschema, objschema, wcls=None, **kwargs): |
|
887 |
if wcls is None: |
|
888 |
wcls = _WFACTORIES[objschema] |
|
889 |
return wcls(vreg, subjschema, rschema, objschema, **kwargs) |
|
890 |
||
891 |
FACTORIES = { |
|
892 |
'String' : _string_widget_factory, |
|
893 |
'Boolean': _default_widget_factory, |
|
894 |
'Bytes': _default_widget_factory, |
|
895 |
'Date': _default_widget_factory, |
|
896 |
'Datetime': _default_widget_factory, |
|
897 |
'Float': _default_widget_factory, |
|
898 |
'Decimal': _default_widget_factory, |
|
899 |
'Int': _default_widget_factory, |
|
900 |
'Password': _default_widget_factory, |
|
901 |
'Time': _default_widget_factory, |
|
902 |
} |
|
903 |
||
904 |
# default widget by entity's type |
|
905 |
_WFACTORIES = { |
|
906 |
'Boolean': YesNoRadioWidget, |
|
907 |
'Bytes': FileWidget, |
|
908 |
'Date': DateWidget, |
|
909 |
'Datetime': DateTimeWidget, |
|
910 |
'Int': IntegerWidget, |
|
911 |
'Float': FloatWidget, |
|
912 |
'Decimal': DecimalWidget, |
|
913 |
'Password': PasswordWidget, |
|
914 |
'String' : StringWidget, |
|
915 |
'Time': TimeWidget, |
|
916 |
} |
|
1554
3a3263df6cdd
new primary view using uicfg.rdisplay (major api cleanup)
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
917 |
|
0 | 918 |
# widgets registry |
919 |
WIDGETS = {} |
|
920 |
def register(widget_list): |
|
921 |
for obj in widget_list: |
|
922 |
if isinstance(obj, type) and issubclass(obj, Widget): |
|
923 |
if obj is Widget or obj is ComboBoxWidget: |
|
924 |
continue |
|
925 |
WIDGETS[obj.__name__] = obj |
|
926 |
||
927 |
register(globals().values()) |