author | sylvain.thenault@logilab.fr |
Thu, 19 Feb 2009 18:55:13 +0100 | |
branch | tls-sprint |
changeset 847 | 27c4ebe90d03 |
parent 844 | 8ab6f64c3750 |
child 848 | f5aca5c5f6ca |
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 |
|
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
425 |
def form_needs_multipart(self): |
844 | 426 |
return any(field.needs_multipart for field in self.fields) |
0 | 427 |
|
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
428 |
def form_render(self, **values): |
844 | 429 |
renderer = values.pop('renderer', FormRenderer()) |
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
430 |
self.form_build_context(values) |
844 | 431 |
return renderer.render(self) |
432 |
||
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
433 |
def form_build_context(self, values): |
844 | 434 |
self.context = context = {} |
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
435 |
for field in self.fields: |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
436 |
context[field] = {'value': self.form_field_value(field, values), |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
437 |
'name': self.form_field_name(field, values), |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
438 |
'id': self.form_field_id(field, values), |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
439 |
} |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
440 |
|
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
441 |
def form_field_value(self, field, values): |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
442 |
value = values.get(field.name, field.initial) |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
443 |
return field.format_value(self.req, value) |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
444 |
|
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
445 |
def form_field_name(self, field, values): |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
446 |
return field.name |
844 | 447 |
|
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
448 |
def form_field_id(self, field, values): |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
449 |
return field.id |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
450 |
|
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
451 |
def form_field_vocabulary(self, field): |
844 | 452 |
raise NotImplementedError |
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
453 |
|
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
454 |
BUTTON_STR = u'<input class="validateButton" type="submit" name="%s" value="%s" tabindex="%s"/>' |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
455 |
ACTION_SUBMIT_STR = u'<input class="validateButton" type="button" onclick="postForm(\'%s\', \'%s\', \'%s\')" value="%s" tabindex="%s"/>' |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
456 |
|
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
457 |
def button_ok(self, label=None, tabindex=None): |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
458 |
label = self.req._(label or stdmsgs.BUTTON_OK).capitalize() |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
459 |
return self.BUTTON_STR % ('defaultsubmit', label, tabindex or 2) |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
460 |
|
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
461 |
def button_apply(self, label=None, tabindex=None): |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
462 |
label = self.req._(label or stdmsgs.BUTTON_APPLY).capitalize() |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
463 |
return self.ACTION_SUBMIT_STR % ('__action_apply', label, self.id, label, tabindex or 3) |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
464 |
|
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
465 |
def button_delete(self, label=None, tabindex=None): |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
466 |
label = self.req._(label or stdmsgs.BUTTON_DELETE).capitalize() |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
467 |
return self.ACTION_SUBMIT_STR % ('__action_delete', label, self.id, label, tabindex or 3) |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
468 |
|
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
469 |
def button_cancel(self, label=None, tabindex=None): |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
470 |
label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
471 |
return self.ACTION_SUBMIT_STR % ('__action_cancel', label, self.id, label, tabindex or 4) |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
472 |
|
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
473 |
def button_reset(self, label=None, tabindex=None): |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
474 |
label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
475 |
return u'<input class="validateButton" type="reset" value="%s" tabindex="%s"/>' % ( |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
476 |
label, tabindex or 4) |
844 | 477 |
|
478 |
||
479 |
class EntityFieldsForm(FieldsForm): |
|
480 |
def __init__(self, *args, **kwargs): |
|
481 |
kwargs.setdefault('id', 'entityForm') |
|
482 |
super(EntityFieldsForm, self).__init__(*args, **kwargs) |
|
483 |
self.fields.append(TextField(name='__type', widget=HiddenInput)) |
|
484 |
self.fields.append(TextField(name='eid', widget=HiddenInput)) |
|
485 |
||
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
486 |
def form_render(self, entity, **values): |
844 | 487 |
self.entity = entity |
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
488 |
return super(EntityFieldsForm, self).form_render(**values) |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
489 |
|
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
490 |
def form_field_value(self, field, values): |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
491 |
try: |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
492 |
value = values[field.name] |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
493 |
except KeyError: |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
494 |
value = getattr(self.entity, field.name, field.initial) |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
495 |
return field.format_value(self.req, value) |
844 | 496 |
|
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
497 |
def form_field_name(self, field, values): |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
498 |
if field.eidparam: |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
499 |
return eid_param(field.name, self.entity.eid) |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
500 |
return field.name |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
501 |
|
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
502 |
def form_field_id(self, field, values): |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
503 |
if field.eidparam: |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
504 |
return eid_param(field.id, self.entity.eid) |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
505 |
return field.id |
844 | 506 |
|
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
507 |
def form_field_vocabulary(self, field): |
844 | 508 |
choices = self.vocabfunc(entity) |
509 |
if self.sort: |
|
510 |
choices = sorted(choices) |
|
511 |
if self.rschema.rproperty(self.subjtype, self.objtype, 'internationalizable'): |
|
512 |
return zip((entity.req._(v) for v in choices), choices) |
|
513 |
return zip(choices, choices) |
|
514 |
||
515 |
||
516 |
# form renderers ############ |
|
517 |
||
518 |
class FormRenderer(object): |
|
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
519 |
|
844 | 520 |
def render(self, form): |
521 |
data = [] |
|
522 |
w = data.append |
|
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
523 |
# XXX form_needs_multipart |
844 | 524 |
w(u'<form action="%s" onsubmit="return freezeFormButtons(\'%s\');" method="post" id="%s">' |
525 |
% (form.req.build_url(form.action), form.id, form.id)) |
|
526 |
w(u'<div id="progress">%s</div>' % _('validating...')) |
|
527 |
w(u'<fieldset>') |
|
528 |
w(tags.input(type='hidden', name='__form_id', value=form.id)) |
|
529 |
if form.redirect_path: |
|
530 |
w(tags.input(type='hidden', name='__redirect_path', value=form.redirect_path)) |
|
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
531 |
self.render_fields(w, form) |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
532 |
self.render_buttons(w, form) |
844 | 533 |
w(u'</fieldset>') |
534 |
w(u'</form>') |
|
535 |
return '\n'.join(data) |
|
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
536 |
|
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
537 |
def render_fields(self, w, form): |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
538 |
w(u'<table>') |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
539 |
for field in form.fields: |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
540 |
w(u'<tr>') |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
541 |
w('<th>%s</th>' % self.render_label(form, field)) |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
542 |
w(u'<td style="width:100%;">') |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
543 |
w(field.render(form)) |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
544 |
w(u'</td></tr>') |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
545 |
w(u'</table>') |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
546 |
|
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
547 |
def render_buttons(self, w, form): |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
548 |
for button in form.buttons(): |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
549 |
w(button) |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
550 |
|
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
551 |
def render_label(self, form, field): |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
552 |
label = form.req._(field.label) |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
553 |
attrs = {'for': form.context[field]['id']} |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
554 |
if field.required: |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
555 |
attrs['class'] = 'required' |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
556 |
return tags.label(label, **attrs) |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
557 |