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