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