author | sylvain.thenault@logilab.fr |
Thu, 19 Feb 2009 18:21:51 +0100 | |
branch | tls-sprint |
changeset 844 | 8ab6f64c3750 |
parent 765 | 8fda14081686 |
child 847 | 27c4ebe90d03 |
permissions | -rw-r--r-- |
0 | 1 |
"""abstract form classes for CubicWeb web client |
2 |
||
3 |
:organization: Logilab |
|
751 | 4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
9 |
from simplejson import dumps |
|
10 |
||
844 | 11 |
from logilab.common.compat import any |
0 | 12 |
from logilab.mtconverter import html_escape |
13 |
||
14 |
from cubicweb import typed_eid |
|
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
15 |
from cubicweb.selectors import match_form_params |
751 | 16 |
from cubicweb.view import NOINDEX, NOFOLLOW, View, EntityView, AnyRsetView |
0 | 17 |
from cubicweb.common.registerers import accepts_registerer |
18 |
from cubicweb.web import stdmsgs |
|
19 |
from cubicweb.web.httpcache import NoHTTPCacheManager |
|
20 |
from cubicweb.web.controller import redirect_params |
|
844 | 21 |
from cubicweb.web import eid_param |
0 | 22 |
|
23 |
||
24 |
def relation_id(eid, rtype, target, reid): |
|
25 |
if target == 'subject': |
|
26 |
return u'%s:%s:%s' % (eid, rtype, reid) |
|
27 |
return u'%s:%s:%s' % (reid, rtype, eid) |
|
844 | 28 |
|
29 |
def toggable_relation_link(eid, nodeid, label='x'): |
|
30 |
js = u"javascript: togglePendingDelete('%s', %s);" % (nodeid, html_escape(dumps(eid))) |
|
31 |
return u'[<a class="handle" href="%s" id="handle%s">%s</a>]' % (js, nodeid, label) |
|
0 | 32 |
|
33 |
||
34 |
class FormMixIn(object): |
|
35 |
"""abstract form mix-in""" |
|
36 |
category = 'form' |
|
37 |
controller = 'edit' |
|
38 |
domid = 'entityForm' |
|
39 |
||
40 |
http_cache_manager = NoHTTPCacheManager |
|
41 |
add_to_breadcrumbs = False |
|
42 |
skip_relations = set() |
|
43 |
||
44 |
def __init__(self, req, rset): |
|
45 |
super(FormMixIn, self).__init__(req, rset) |
|
46 |
self.maxrelitems = self.req.property_value('navigation.related-limit') |
|
47 |
self.maxcomboitems = self.req.property_value('navigation.combobox-limit') |
|
48 |
self.force_display = not not req.form.get('__force_display') |
|
49 |
# get validation session data which may have been previously set. |
|
50 |
# deleting validation errors here breaks form reloading (errors are |
|
51 |
# no more available), they have to be deleted by application's publish |
|
52 |
# method on successful commit |
|
53 |
formurl = req.url() |
|
54 |
forminfo = req.get_session_data(formurl) |
|
55 |
if forminfo: |
|
56 |
req.data['formvalues'] = forminfo['values'] |
|
57 |
req.data['formerrors'] = errex = forminfo['errors'] |
|
58 |
req.data['displayederrors'] = set() |
|
59 |
# if some validation error occured on entity creation, we have to |
|
60 |
# get the original variable name from its attributed eid |
|
61 |
foreid = errex.entity |
|
62 |
for var, eid in forminfo['eidmap'].items(): |
|
63 |
if foreid == eid: |
|
64 |
errex.eid = var |
|
65 |
break |
|
66 |
else: |
|
67 |
errex.eid = foreid |
|
68 |
||
69 |
def html_headers(self): |
|
70 |
"""return a list of html headers (eg something to be inserted between |
|
71 |
<head> and </head> of the returned page |
|
72 |
||
73 |
by default forms are neither indexed nor followed |
|
74 |
""" |
|
75 |
return [NOINDEX, NOFOLLOW] |
|
76 |
||
77 |
def linkable(self): |
|
78 |
"""override since forms are usually linked by an action, |
|
79 |
so we don't want them to be listed by appli.possible_views |
|
80 |
""" |
|
81 |
return False |
|
82 |
||
83 |
@property |
|
84 |
def limit(self): |
|
85 |
if self.force_display: |
|
86 |
return None |
|
87 |
return self.maxrelitems + 1 |
|
88 |
||
89 |
def need_multipart(self, entity, categories=('primary', 'secondary')): |
|
90 |
"""return a boolean indicating if form's enctype should be multipart |
|
91 |
""" |
|
92 |
for rschema, _, x in entity.relations_by_category(categories): |
|
93 |
if entity.get_widget(rschema, x).need_multipart: |
|
94 |
return True |
|
95 |
# let's find if any of our inlined entities needs multipart |
|
96 |
for rschema, targettypes, x in entity.relations_by_category('inlineview'): |
|
97 |
assert len(targettypes) == 1, \ |
|
98 |
"I'm not able to deal with several targets and inlineview" |
|
99 |
ttype = targettypes[0] |
|
100 |
inlined_entity = self.vreg.etype_class(ttype)(self.req, None, None) |
|
101 |
for irschema, _, x in inlined_entity.relations_by_category(categories): |
|
102 |
if inlined_entity.get_widget(irschema, x).need_multipart: |
|
103 |
return True |
|
104 |
return False |
|
105 |
||
106 |
def error_message(self): |
|
107 |
"""return formatted error message |
|
108 |
||
109 |
This method should be called once inlined field errors has been consumed |
|
110 |
""" |
|
111 |
errex = self.req.data.get('formerrors') |
|
112 |
# get extra errors |
|
113 |
if errex is not None: |
|
114 |
errormsg = self.req._('please correct the following errors:') |
|
115 |
displayed = self.req.data['displayederrors'] |
|
116 |
errors = sorted((field, err) for field, err in errex.errors.items() |
|
117 |
if not field in displayed) |
|
118 |
if errors: |
|
119 |
if len(errors) > 1: |
|
120 |
templstr = '<li>%s</li>\n' |
|
121 |
else: |
|
122 |
templstr = ' %s\n' |
|
123 |
for field, err in errors: |
|
124 |
if field is None: |
|
125 |
errormsg += templstr % err |
|
126 |
else: |
|
127 |
errormsg += templstr % '%s: %s' % (self.req._(field), err) |
|
128 |
if len(errors) > 1: |
|
129 |
errormsg = '<ul>%s</ul>' % errormsg |
|
130 |
return u'<div class="errorMessage">%s</div>' % errormsg |
|
131 |
return u'' |
|
132 |
||
133 |
def restore_pending_inserts(self, entity, cell=False): |
|
134 |
"""used to restore edition page as it was before clicking on |
|
135 |
'search for <some entity type>' |
|
136 |
|
|
137 |
""" |
|
138 |
eid = entity.eid |
|
139 |
cell = cell and "div_insert_" or "tr" |
|
140 |
pending_inserts = set(self.req.get_pending_inserts(eid)) |
|
141 |
for pendingid in pending_inserts: |
|
142 |
eidfrom, rtype, eidto = pendingid.split(':') |
|
143 |
if typed_eid(eidfrom) == entity.eid: # subject |
|
144 |
label = display_name(self.req, rtype, 'subject') |
|
145 |
reid = eidto |
|
146 |
else: |
|
147 |
label = display_name(self.req, rtype, 'object') |
|
148 |
reid = eidfrom |
|
149 |
jscall = "javascript: cancelPendingInsert('%s', '%s', null, %s);" \ |
|
150 |
% (pendingid, cell, eid) |
|
151 |
rset = self.req.eid_rset(reid) |
|
152 |
eview = self.view('text', rset, row=0) |
|
153 |
# XXX find a clean way to handle baskets |
|
154 |
if rset.description[0][0] == 'Basket': |
|
155 |
eview = '%s (%s)' % (eview, display_name(self.req, 'Basket')) |
|
156 |
yield rtype, pendingid, jscall, label, reid, eview |
|
157 |
||
158 |
||
159 |
def force_display_link(self): |
|
160 |
return (u'<span class="invisible">' |
|
161 |
u'[<a href="javascript: window.location.href+=\'&__force_display=1\'">%s</a>]' |
|
162 |
u'</span>' % self.req._('view all')) |
|
163 |
||
164 |
def relations_table(self, entity): |
|
165 |
"""yiels 3-tuples (rtype, target, related_list) |
|
166 |
where <related_list> itself a list of : |
|
167 |
- node_id (will be the entity element's DOM id) |
|
168 |
- appropriate javascript's togglePendingDelete() function call |
|
169 |
- status 'pendingdelete' or '' |
|
170 |
- oneline view of related entity |
|
171 |
""" |
|
172 |
eid = entity.eid |
|
173 |
pending_deletes = self.req.get_pending_deletes(eid) |
|
174 |
# XXX (adim) : quick fix to get Folder relations |
|
175 |
for label, rschema, target in entity.srelations_by_category(('generic', 'metadata'), 'add'): |
|
176 |
if rschema in self.skip_relations: |
|
177 |
continue |
|
178 |
relatedrset = entity.related(rschema, target, limit=self.limit) |
|
179 |
toggable_rel_link = self.toggable_relation_link_func(rschema) |
|
180 |
related = [] |
|
181 |
for row in xrange(relatedrset.rowcount): |
|
182 |
nodeid = relation_id(eid, rschema, target, relatedrset[row][0]) |
|
183 |
if nodeid in pending_deletes: |
|
184 |
status = u'pendingDelete' |
|
185 |
label = '+' |
|
186 |
else: |
|
187 |
status = u'' |
|
188 |
label = 'x' |
|
189 |
dellink = toggable_rel_link(eid, nodeid, label) |
|
190 |
eview = self.view('oneline', relatedrset, row=row) |
|
191 |
related.append((nodeid, dellink, status, eview)) |
|
192 |
yield (rschema, target, related) |
|
193 |
||
194 |
def toggable_relation_link_func(self, rschema): |
|
195 |
if not rschema.has_perm(self.req, 'delete'): |
|
196 |
return lambda x, y, z: u'' |
|
197 |
return toggable_relation_link |
|
198 |
||
199 |
||
200 |
def redirect_url(self, entity=None): |
|
201 |
"""return a url to use as next direction if there are some information |
|
202 |
specified in current form params, else return the result the reset_url |
|
203 |
method which should be defined in concrete classes |
|
204 |
""" |
|
205 |
rparams = redirect_params(self.req.form) |
|
206 |
if rparams: |
|
207 |
return self.build_url('view', **rparams) |
|
208 |
return self.reset_url(entity) |
|
209 |
||
210 |
def reset_url(self, entity): |
|
211 |
raise NotImplementedError('implement me in concrete classes') |
|
212 |
||
213 |
BUTTON_STR = u'<input class="validateButton" type="submit" name="%s" value="%s" tabindex="%s"/>' |
|
214 |
ACTION_SUBMIT_STR = u'<input class="validateButton" type="button" onclick="postForm(\'%s\', \'%s\', \'%s\')" value="%s" tabindex="%s"/>' |
|
215 |
||
216 |
def button_ok(self, label=None, tabindex=None): |
|
217 |
label = self.req._(label or stdmsgs.BUTTON_OK).capitalize() |
|
218 |
return self.BUTTON_STR % ('defaultsubmit', label, tabindex or 2) |
|
219 |
||
220 |
def button_apply(self, label=None, tabindex=None): |
|
221 |
label = self.req._(label or stdmsgs.BUTTON_APPLY).capitalize() |
|
222 |
return self.ACTION_SUBMIT_STR % ('__action_apply', label, self.domid, label, tabindex or 3) |
|
223 |
||
224 |
def button_delete(self, label=None, tabindex=None): |
|
225 |
label = self.req._(label or stdmsgs.BUTTON_DELETE).capitalize() |
|
226 |
return self.ACTION_SUBMIT_STR % ('__action_delete', label, self.domid, label, tabindex or 3) |
|
227 |
||
228 |
def button_cancel(self, label=None, tabindex=None): |
|
229 |
label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() |
|
230 |
return self.ACTION_SUBMIT_STR % ('__action_cancel', label, self.domid, label, tabindex or 4) |
|
231 |
||
232 |
def button_reset(self, label=None, tabindex=None): |
|
233 |
label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() |
|
234 |
return u'<input class="validateButton" type="reset" value="%s" tabindex="%s"/>' % ( |
|
235 |
label, tabindex or 4) |
|
844 | 236 |
|
237 |
||
238 |
############################################################################### |
|
239 |
||
240 |
from cubicweb.common import tags |
|
241 |
||
242 |
# widgets ############ |
|
243 |
||
244 |
class FieldWidget(object): |
|
245 |
def __init__(self, attrs=None): |
|
246 |
self.attrs = attrs or {} |
|
247 |
||
248 |
def render(self, form, field): |
|
249 |
raise NotImplementedError |
|
250 |
||
251 |
class Input(FieldWidget): |
|
252 |
type = None |
|
253 |
||
254 |
def render(self, form, field): |
|
255 |
name, value, attrs = self._render_attrs(form, field) |
|
256 |
if attrs is None: |
|
257 |
return tags.input(name=name, value=value) |
|
258 |
return tags.input(name=name, value=value, type=self.type, **attrs) |
|
259 |
||
260 |
def _render_attrs(self, form, field): |
|
261 |
name = form.context[field]['name'] # qualified name |
|
262 |
value = form.context[field]['value'] |
|
263 |
#fattrs = field.widget_attributes(self) |
|
264 |
attrs = self.attrs.copy() |
|
265 |
#attrs.update(fattrs) |
|
266 |
# XXX id |
|
267 |
return name, value, attrs |
|
268 |
||
269 |
class TextInput(Input): |
|
270 |
type = 'text' |
|
271 |
||
272 |
class PasswordInput(Input): |
|
273 |
type = 'password' |
|
274 |
||
275 |
class FileInput(Input): |
|
276 |
type = 'file' |
|
277 |
||
278 |
class HiddenInput(Input): |
|
279 |
type = 'hidden' |
|
280 |
||
281 |
class Button(Input): |
|
282 |
type = 'button' |
|
283 |
||
284 |
class TextArea(FieldWidget): |
|
285 |
def render(self, form, field): |
|
286 |
name, value, attrs = self._render_attrs(form, field) |
|
287 |
if attrs is None: |
|
288 |
return tags.textarea(value, name=name) |
|
289 |
return tags.textarea(value, name=name, **attrs) |
|
290 |
||
291 |
class Select: |
|
292 |
def render(self, form, field): |
|
293 |
name, value, attrs = self._render_attrs(form, field) |
|
294 |
if self.vocabulary: |
|
295 |
# static vocabulary defined in form definition |
|
296 |
vocab = self.vocabulary |
|
297 |
else: |
|
298 |
vocab = form.get_vocabulary(field) |
|
299 |
options = [] |
|
300 |
for label, value in vocab: |
|
301 |
options.append(tags.option(label, value=value)) |
|
302 |
if attrs is None: |
|
303 |
return tags.select(name=name, options=options) |
|
304 |
return tags.select(name=name, options=options, **attrs) |
|
305 |
||
306 |
class CheckBox: pass |
|
307 |
||
308 |
class Radio: pass |
|
309 |
||
310 |
class DateTimePicker: pass |
|
311 |
||
312 |
||
313 |
# fields ############ |
|
314 |
||
315 |
class Field(object): |
|
316 |
"""field class is introduced to control what's displayed in edition form |
|
317 |
""" |
|
318 |
widget = TextInput |
|
319 |
needs_multipart = False |
|
320 |
creation_rank = 0 |
|
321 |
||
322 |
def __init__(self, name=None, id=None, label=None, |
|
323 |
widget=None, required=False, initial=None, help=None, |
|
324 |
eidparam=True): |
|
325 |
self.required = required |
|
326 |
if widget is not None: |
|
327 |
self.widget = widget |
|
328 |
if isinstance(self.widget, type): |
|
329 |
self.widget = self.widget() |
|
330 |
self.name = name |
|
331 |
self.label = label or name |
|
332 |
self.id = id or name |
|
333 |
self.initial = initial |
|
334 |
self.help = help |
|
335 |
self.eidparam = eidparam |
|
336 |
# global fields ordering in forms |
|
337 |
Field.creation_rank += 1 |
|
338 |
||
339 |
def set_name(self, name): |
|
340 |
self.name = name |
|
341 |
if not self.id: |
|
342 |
self.id = name |
|
343 |
if not self.label: |
|
344 |
self.label = name |
|
345 |
||
346 |
def format_value(self, req, value): |
|
347 |
return unicode(value) |
|
348 |
||
349 |
def render(self, form): |
|
350 |
return self.widget.render(form, self) |
|
351 |
||
352 |
||
353 |
class StringField(Field): |
|
354 |
def __init__(self, max_length=None, **kwargs): |
|
355 |
super(StringField, self).__init__(**kwargs) |
|
356 |
self.max_length = max_length |
|
0 | 357 |
|
844 | 358 |
class TextField(Field): |
359 |
widget = TextArea |
|
360 |
def __init__(self, row=None, col=None, **kwargs): |
|
361 |
super(TextField, self).__init__(**kwargs) |
|
362 |
self.row = row |
|
363 |
self.col = col |
|
364 |
||
365 |
class RichTextField(Field): |
|
366 |
pass |
|
367 |
||
368 |
class IntField(Field): |
|
369 |
def __init__(self, min=None, max=None, **kwargs): |
|
370 |
super(IntField, self).__init__(**kwargs) |
|
371 |
self.min = min |
|
372 |
self.max = max |
|
373 |
||
374 |
class FloatField(IntField): |
|
375 |
||
376 |
def format_value(self, req, value): |
|
377 |
if value is not None: |
|
378 |
return ustrftime(value, req.property_value('ui.float-format')) |
|
379 |
return u'' |
|
380 |
||
381 |
class DateField(IntField): |
|
382 |
||
383 |
def format_value(self, req, value): |
|
384 |
return value and ustrftime(value, req.property_value('ui.date-format')) or u'' |
|
385 |
||
386 |
class DateTimeField(IntField): |
|
387 |
||
388 |
def format_value(self, req, value): |
|
389 |
return value and ustrftime(value, req.property_value('ui.datetime-format')) or u'' |
|
390 |
||
391 |
class FileField(IntField): |
|
392 |
needs_multipart = True |
|
393 |
||
394 |
# forms ############ |
|
395 |
class metafieldsform(type): |
|
396 |
def __new__(mcs, name, bases, classdict): |
|
397 |
allfields = [] |
|
398 |
for base in bases: |
|
399 |
if hasattr(base, '_fields_'): |
|
400 |
allfields += base._fields_ |
|
401 |
clsfields = (item for item in classdict.items() |
|
402 |
if isinstance(item[1], Field)) |
|
403 |
for name, field in sorted(clsfields, key=lambda x: x[1].creation_rank): |
|
404 |
if not field.name: |
|
405 |
field.set_name(name) |
|
406 |
allfields.append(field) |
|
407 |
classdict['_fields_'] = allfields |
|
408 |
return super(metafieldsform, mcs).__new__(mcs, name, bases, classdict) |
|
409 |
||
410 |
||
411 |
class FieldsForm(object): |
|
412 |
__metaclass__ = metafieldsform |
|
413 |
||
414 |
def __init__(self, req, id=None, title=None, action='edit', |
|
415 |
redirect_path=None): |
|
416 |
self.req = req |
|
417 |
self.id = id or 'form' |
|
418 |
self.title = title |
|
419 |
self.action = action |
|
420 |
self.redirect_path = None |
|
421 |
self.fields = list(self.__class__._fields_) |
|
422 |
self.context = {} |
|
423 |
||
424 |
@property |
|
425 |
def needs_multipart(self): |
|
426 |
return any(field.needs_multipart for field in self.fields) |
|
0 | 427 |
|
844 | 428 |
def render(self, **values): |
429 |
renderer = values.pop('renderer', FormRenderer()) |
|
430 |
self.build_context(values) |
|
431 |
return renderer.render(self) |
|
432 |
||
433 |
def build_context(self, values): |
|
434 |
self.context = context = {} |
|
435 |
for name, field in self.fields: |
|
436 |
value = values.get(field.name, field.initial) |
|
437 |
context[field] = {'value': field.format_value(self.req, value)} |
|
438 |
||
439 |
def get_vocabulary(self, field): |
|
440 |
raise NotImplementedError |
|
441 |
||
442 |
||
443 |
class EntityFieldsForm(FieldsForm): |
|
444 |
def __init__(self, *args, **kwargs): |
|
445 |
kwargs.setdefault('id', 'entityForm') |
|
446 |
super(EntityFieldsForm, self).__init__(*args, **kwargs) |
|
447 |
self.fields.append(TextField(name='__type', widget=HiddenInput)) |
|
448 |
self.fields.append(TextField(name='eid', widget=HiddenInput)) |
|
449 |
||
450 |
def render(self, entity, **values): |
|
451 |
self.entity = entity |
|
452 |
return super(EntityFieldsForm, self).render(**values) |
|
453 |
||
454 |
def build_context(self, values): |
|
455 |
self.context = context = {} |
|
456 |
for field in self.fields: |
|
457 |
try: |
|
458 |
value = values[field.name] |
|
459 |
except KeyError: |
|
460 |
value = getattr(self.entity, field.name, field.initial) |
|
461 |
if field.eidparam: |
|
462 |
name = eid_param(field.name, self.entity.eid) |
|
463 |
else: |
|
464 |
name = field.name |
|
465 |
context[field] = {'value': field.format_value(self.req, value), |
|
466 |
'name': name} |
|
467 |
||
468 |
def get_vocabulary(self, field): |
|
469 |
choices = self.vocabfunc(entity) |
|
470 |
if self.sort: |
|
471 |
choices = sorted(choices) |
|
472 |
if self.rschema.rproperty(self.subjtype, self.objtype, 'internationalizable'): |
|
473 |
return zip((entity.req._(v) for v in choices), choices) |
|
474 |
return zip(choices, choices) |
|
475 |
||
476 |
||
477 |
# form renderers ############ |
|
478 |
||
479 |
class FormRenderer(object): |
|
480 |
def render(self, form): |
|
481 |
data = [] |
|
482 |
w = data.append |
|
483 |
w(u'<form action="%s" onsubmit="return freezeFormButtons(\'%s\');" method="post" id="%s">' |
|
484 |
% (form.req.build_url(form.action), form.id, form.id)) |
|
485 |
w(u'<div id="progress">%s</div>' % _('validating...')) |
|
486 |
w(u'<fieldset>') |
|
487 |
w(tags.input(type='hidden', name='__form_id', value=form.id)) |
|
488 |
if form.redirect_path: |
|
489 |
w(tags.input(type='hidden', name='__redirect_path', value=form.redirect_path)) |
|
490 |
for field in form.fields: |
|
491 |
w(field.render(form)) |
|
492 |
for button in form.buttons(): |
|
493 |
w(button.render()) |
|
494 |
w(u'</fieldset>') |
|
495 |
w(u'</form>') |
|
496 |
return '\n'.join(data) |