author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Wed, 07 Apr 2010 11:56:30 +0200 | |
branch | stable |
changeset 5171 | 1c2567af37dd |
parent 5038 | 90493551b1eb |
child 5368 | d321e4b62a10 |
permissions | -rw-r--r-- |
1147 | 1 |
"""form renderers, responsible to layout a form to html |
2 |
||
3 |
:organization: Logilab |
|
4278 | 4 |
:copyright: 2009-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
1147 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1967
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
1147 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
3478
f043f71017a3
handle display_fields in the form, not in the renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3477
diff
changeset
|
10 |
from warnings import warn |
f043f71017a3
handle display_fields in the form, not in the renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3477
diff
changeset
|
11 |
|
1147 | 12 |
from logilab.common import dictattr |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2247
diff
changeset
|
13 |
from logilab.mtconverter import xml_escape |
1147 | 14 |
|
15 |
from simplejson import dumps |
|
16 |
||
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3629
diff
changeset
|
17 |
from cubicweb import tags |
2656
a93ae0f6c0ad
R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2586
diff
changeset
|
18 |
from cubicweb.appobject import AppObject |
4075
e710f4052bd6
use implements instead of entity_implements
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4073
diff
changeset
|
19 |
from cubicweb.selectors import implements, yes |
2819
b864288fd316
remove more 3.2 deprecated code, reintroduce checkbox used by formrenderers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
20 |
from cubicweb.web import eid_param, formwidgets as fwdgs |
1147 | 21 |
|
3478
f043f71017a3
handle display_fields in the form, not in the renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3477
diff
changeset
|
22 |
|
2819
b864288fd316
remove more 3.2 deprecated code, reintroduce checkbox used by formrenderers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
23 |
def checkbox(name, value, attrs='', checked=None): |
b864288fd316
remove more 3.2 deprecated code, reintroduce checkbox used by formrenderers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
24 |
if checked is None: |
b864288fd316
remove more 3.2 deprecated code, reintroduce checkbox used by formrenderers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
25 |
checked = value |
b864288fd316
remove more 3.2 deprecated code, reintroduce checkbox used by formrenderers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
26 |
checked = checked and 'checked="checked"' or '' |
b864288fd316
remove more 3.2 deprecated code, reintroduce checkbox used by formrenderers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
27 |
return u'<input type="checkbox" name="%s" value="%s" %s %s />' % ( |
b864288fd316
remove more 3.2 deprecated code, reintroduce checkbox used by formrenderers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
28 |
name, value, checked, attrs) |
1147 | 29 |
|
4313
5d8196a105a7
move base form renderer label handling into a function to reuse it in CompositeFormRenderer and avoid seeing a tuple in column headers...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
30 |
def field_label(form, field): |
5d8196a105a7
move base form renderer label handling into a function to reuse it in CompositeFormRenderer and avoid seeing a tuple in column headers...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
31 |
# XXX with 3.6 we can now properly rely on 'if field.role is not None' and |
5d8196a105a7
move base form renderer label handling into a function to reuse it in CompositeFormRenderer and avoid seeing a tuple in column headers...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
32 |
# stop having a tuple for label |
5d8196a105a7
move base form renderer label handling into a function to reuse it in CompositeFormRenderer and avoid seeing a tuple in column headers...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
33 |
if isinstance(field.label, tuple): # i.e. needs contextual translation |
4314 | 34 |
return form._cw.pgettext(*field.label) |
35 |
return form._cw._(field.label) |
|
4313
5d8196a105a7
move base form renderer label handling into a function to reuse it in CompositeFormRenderer and avoid seeing a tuple in column headers...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
36 |
|
5d8196a105a7
move base form renderer label handling into a function to reuse it in CompositeFormRenderer and avoid seeing a tuple in column headers...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
37 |
|
1319 | 38 |
|
2656
a93ae0f6c0ad
R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2586
diff
changeset
|
39 |
class FormRenderer(AppObject): |
1147 | 40 |
"""basic renderer displaying fields in a two columns table label | value |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
41 |
|
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
42 |
+--------------+--------------+ |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
43 |
| field1 label | field1 input | |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
44 |
+--------------+--------------+ |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
45 |
| field1 label | field2 input | |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
46 |
+--------------+--------------+ |
4743
026a89520184
[book] a few autoclasses for renderers, misc tweaks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4668
diff
changeset
|
47 |
|
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
48 |
+---------+ |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
49 |
| buttons | |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
50 |
+---------+ |
1147 | 51 |
""" |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
52 |
__registry__ = 'formrenderers' |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
53 |
__regid__ = 'default' |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
54 |
|
3478
f043f71017a3
handle display_fields in the form, not in the renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3477
diff
changeset
|
55 |
_options = ('display_label', 'display_help', |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
56 |
'display_progress_div', 'table_class', 'button_bar_class', |
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
57 |
# add entity since it may be given to select the renderer |
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
58 |
'entity') |
1362
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
59 |
display_label = True |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
60 |
display_help = True |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
61 |
display_progress_div = True |
1710
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1691
diff
changeset
|
62 |
table_class = u'attributeForm' |
1147 | 63 |
button_bar_class = u'formButtonBar' |
1436 | 64 |
|
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
65 |
def __init__(self, req=None, rset=None, row=None, col=None, **kwargs): |
2890
fdcb8a2bb6eb
fix __init__ parameters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2819
diff
changeset
|
66 |
super(FormRenderer, self).__init__(req, rset=rset, row=row, col=col) |
1362
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
67 |
if self._set_options(kwargs): |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
68 |
raise ValueError('unconsumed arguments %s' % kwargs) |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
69 |
|
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
70 |
def _set_options(self, kwargs): |
1434
42e57dbbc585
new display_relations_form option on EntityFormRender
sylvain.thenault@logilab.fr
parents:
1412
diff
changeset
|
71 |
for key in self._options: |
1362
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
72 |
try: |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
73 |
setattr(self, key, kwargs.pop(key)) |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
74 |
except KeyError: |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
75 |
continue |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
76 |
return kwargs |
1436 | 77 |
|
1147 | 78 |
# renderer interface ###################################################### |
1436 | 79 |
|
1147 | 80 |
def render(self, form, values): |
1362
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
81 |
self._set_options(values) |
1147 | 82 |
form.add_media() |
83 |
data = [] |
|
84 |
w = data.append |
|
85 |
w(self.open_form(form, values)) |
|
1362
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
86 |
if self.display_progress_div: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3398
diff
changeset
|
87 |
w(u'<div id="progress">%s</div>' % self._cw._('validating...')) |
1147 | 88 |
w(u'<fieldset>') |
89 |
self.render_fields(w, form, values) |
|
90 |
self.render_buttons(w, form) |
|
91 |
w(u'</fieldset>') |
|
4573
fc6bb020e60e
use a close_form method for consistency w/ open_form and allow overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4380
diff
changeset
|
92 |
w(self.close_form(form, values)) |
1147 | 93 |
errormsg = self.error_message(form) |
94 |
if errormsg: |
|
1436 | 95 |
data.insert(0, errormsg) |
1147 | 96 |
return '\n'.join(data) |
97 |
||
98 |
def render_label(self, form, field): |
|
2521
782360fa6cc0
take care to None labels
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2520
diff
changeset
|
99 |
if field.label is None: |
782360fa6cc0
take care to None labels
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2520
diff
changeset
|
100 |
return u'' |
4313
5d8196a105a7
move base form renderer label handling into a function to reuse it in CompositeFormRenderer and avoid seeing a tuple in column headers...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
101 |
label = field_label(form, field) |
4156
1bbb0ee42c8e
drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4083
diff
changeset
|
102 |
attrs = {'for': field.dom_id(form)} |
1147 | 103 |
if field.required: |
104 |
attrs['class'] = 'required' |
|
105 |
return tags.label(label, **attrs) |
|
106 |
||
107 |
def render_help(self, form, field): |
|
1946
d60ddfc4bb09
don't use tab
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1863
diff
changeset
|
108 |
help = [] |
1147 | 109 |
descr = field.help |
2093
e3aaf61f8c66
field's help may now be a callable taking the form as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2006
diff
changeset
|
110 |
if callable(descr): |
e3aaf61f8c66
field's help may now be a callable taking the form as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2006
diff
changeset
|
111 |
descr = descr(form) |
1147 | 112 |
if descr: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3398
diff
changeset
|
113 |
help.append('<div class="helper">%s</div>' % self._cw._(descr)) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3398
diff
changeset
|
114 |
example = field.example_format(self._cw) |
1147 | 115 |
if example: |
1863
9a02979a2c00
do not use <br> in widgets
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
1757
diff
changeset
|
116 |
help.append('<div class="helper">(%s: %s)</div>' |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3398
diff
changeset
|
117 |
% (self._cw._('sample format'), example)) |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2656
diff
changeset
|
118 |
return u' '.join(help) |
1147 | 119 |
|
120 |
# specific methods (mostly to ease overriding) ############################# |
|
121 |
||
122 |
def error_message(self, form): |
|
123 |
"""return formatted error message |
|
124 |
||
125 |
This method should be called once inlined field errors has been consumed |
|
126 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3398
diff
changeset
|
127 |
req = self._cw |
1710
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1691
diff
changeset
|
128 |
errex = form.form_valerror |
1147 | 129 |
# get extra errors |
130 |
if errex is not None: |
|
131 |
errormsg = req._('please correct the following errors:') |
|
4224
5998df006968
refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4181
diff
changeset
|
132 |
errors = form.remaining_errors() |
1147 | 133 |
if errors: |
134 |
if len(errors) > 1: |
|
4783
6dc34d4cf892
[F] views: fix 2 unicode errors
Julien Jehannet <Julien Jehannet <julien.jehannet@logilab.fr>>
parents:
4743
diff
changeset
|
135 |
templstr = u'<li>%s</li>\n' |
1147 | 136 |
else: |
4783
6dc34d4cf892
[F] views: fix 2 unicode errors
Julien Jehannet <Julien Jehannet <julien.jehannet@logilab.fr>>
parents:
4743
diff
changeset
|
137 |
templstr = u' %s\n' |
1147 | 138 |
for field, err in errors: |
139 |
if field is None: |
|
140 |
errormsg += templstr % err |
|
141 |
else: |
|
142 |
errormsg += templstr % '%s: %s' % (req._(field), err) |
|
143 |
if len(errors) > 1: |
|
144 |
errormsg = '<ul>%s</ul>' % errormsg |
|
145 |
return u'<div class="errorMessage">%s</div>' % errormsg |
|
146 |
return u'' |
|
1436 | 147 |
|
1147 | 148 |
def open_form(self, form, values): |
4164
119a374c5eb4
form_add_hidden -> add_hidden
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4156
diff
changeset
|
149 |
if form.needs_multipart: |
1147 | 150 |
enctype = 'multipart/form-data' |
151 |
else: |
|
152 |
enctype = 'application/x-www-form-urlencoded' |
|
153 |
if form.action is None: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3398
diff
changeset
|
154 |
action = self._cw.build_url('edit') |
1147 | 155 |
else: |
156 |
action = form.action |
|
1274
b730932a79b7
we don't want dom id systematically
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
157 |
tag = ('<form action="%s" method="post" enctype="%s"' % ( |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2247
diff
changeset
|
158 |
xml_escape(action or '#'), enctype)) |
1274
b730932a79b7
we don't want dom id systematically
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
159 |
if form.domid: |
b730932a79b7
we don't want dom id systematically
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
160 |
tag += ' id="%s"' % form.domid |
1147 | 161 |
if form.onsubmit: |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2247
diff
changeset
|
162 |
tag += ' onsubmit="%s"' % xml_escape(form.onsubmit % dictattr(form)) |
1147 | 163 |
if form.cssstyle: |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2247
diff
changeset
|
164 |
tag += ' style="%s"' % xml_escape(form.cssstyle) |
1147 | 165 |
if form.cssclass: |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2247
diff
changeset
|
166 |
tag += ' class="%s"' % xml_escape(form.cssclass) |
1147 | 167 |
if form.cwtarget: |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2247
diff
changeset
|
168 |
tag += ' cubicweb:target="%s"' % xml_escape(form.cwtarget) |
1147 | 169 |
return tag + '>' |
1436 | 170 |
|
4573
fc6bb020e60e
use a close_form method for consistency w/ open_form and allow overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4380
diff
changeset
|
171 |
def close_form(self, form, values): |
4743
026a89520184
[book] a few autoclasses for renderers, misc tweaks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4668
diff
changeset
|
172 |
"""seems dumb but important for consistency w/ close form, and necessary |
4573
fc6bb020e60e
use a close_form method for consistency w/ open_form and allow overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4380
diff
changeset
|
173 |
for form renderers overriding open_form to use something else or more than |
fc6bb020e60e
use a close_form method for consistency w/ open_form and allow overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4380
diff
changeset
|
174 |
and <form> |
fc6bb020e60e
use a close_form method for consistency w/ open_form and allow overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4380
diff
changeset
|
175 |
""" |
fc6bb020e60e
use a close_form method for consistency w/ open_form and allow overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4380
diff
changeset
|
176 |
return '</form>' |
fc6bb020e60e
use a close_form method for consistency w/ open_form and allow overriding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4380
diff
changeset
|
177 |
|
1147 | 178 |
def render_fields(self, w, form, values): |
179 |
fields = self._render_hidden_fields(w, form) |
|
180 |
if fields: |
|
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1401
diff
changeset
|
181 |
self._render_fields(fields, w, form) |
1147 | 182 |
self.render_child_forms(w, form, values) |
1436 | 183 |
|
1147 | 184 |
def render_child_forms(self, w, form, values): |
1436 | 185 |
# render |
1147 | 186 |
for childform in getattr(form, 'forms', []): |
187 |
self.render_fields(w, childform, values) |
|
188 |
||
189 |
def _render_hidden_fields(self, w, form): |
|
190 |
fields = form.fields[:] |
|
191 |
for field in form.fields: |
|
3478
f043f71017a3
handle display_fields in the form, not in the renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3477
diff
changeset
|
192 |
if not field.is_visible(): |
1147 | 193 |
w(field.render(form, self)) |
194 |
fields.remove(field) |
|
195 |
return fields |
|
1436 | 196 |
|
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1401
diff
changeset
|
197 |
def _render_fields(self, fields, w, form): |
2520
8c5cf48ae9ea
new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
198 |
byfieldset = {} |
1147 | 199 |
for field in fields: |
2520
8c5cf48ae9ea
new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
200 |
byfieldset.setdefault(field.fieldset, []).append(field) |
2574
37154aa90947
[form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2561
diff
changeset
|
201 |
if form.fieldsets_in_order: |
37154aa90947
[form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2561
diff
changeset
|
202 |
fieldsets = form.fieldsets_in_order |
37154aa90947
[form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2561
diff
changeset
|
203 |
else: |
37154aa90947
[form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2561
diff
changeset
|
204 |
fieldsets = byfieldset.keys() |
37154aa90947
[form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2561
diff
changeset
|
205 |
for fieldset in fieldsets: |
37154aa90947
[form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2561
diff
changeset
|
206 |
try: |
37154aa90947
[form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2561
diff
changeset
|
207 |
fields = byfieldset.pop(fieldset) |
37154aa90947
[form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2561
diff
changeset
|
208 |
except KeyError: |
37154aa90947
[form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2561
diff
changeset
|
209 |
self.warning('no such fieldset: %s (%s)', fieldset, form) |
37154aa90947
[form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2561
diff
changeset
|
210 |
continue |
2520
8c5cf48ae9ea
new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
211 |
w(u'<fieldset class="%s">' % (fieldset or u'default')) |
8c5cf48ae9ea
new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
212 |
if fieldset: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3398
diff
changeset
|
213 |
w(u'<legend>%s</legend>' % self._cw._(fieldset)) |
2520
8c5cf48ae9ea
new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
214 |
w(u'<table class="%s">' % self.table_class) |
8c5cf48ae9ea
new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
215 |
for field in fields: |
2561
c6f0634e2d77
[F xhml] use a class to avoid duplicated ids when editing several entities at once
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2550
diff
changeset
|
216 |
w(u'<tr class="%s_%s_row">' % (field.name, field.role)) |
4277
35cd057339b2
turn all the stuff used to handle 'generic relations' in forms into proper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
217 |
if self.display_label and field.label is not None: |
2520
8c5cf48ae9ea
new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
218 |
w(u'<th class="labelCol">%s</th>' % self.render_label(form, field)) |
4277
35cd057339b2
turn all the stuff used to handle 'generic relations' in forms into proper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
219 |
w('<td') |
35cd057339b2
turn all the stuff used to handle 'generic relations' in forms into proper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
220 |
if field.label is None: |
35cd057339b2
turn all the stuff used to handle 'generic relations' in forms into proper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
221 |
w(' colspan="2"') |
4224
5998df006968
refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4181
diff
changeset
|
222 |
error = form.field_error(field) |
2520
8c5cf48ae9ea
new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
223 |
if error: |
4277
35cd057339b2
turn all the stuff used to handle 'generic relations' in forms into proper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
224 |
w(u' class="error"') |
35cd057339b2
turn all the stuff used to handle 'generic relations' in forms into proper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
225 |
w(u'>') |
2520
8c5cf48ae9ea
new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
226 |
w(field.render(form, self)) |
5038
90493551b1eb
[form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4936
diff
changeset
|
227 |
if error: |
90493551b1eb
[form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4936
diff
changeset
|
228 |
self.render_error(w, error) |
2520
8c5cf48ae9ea
new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
229 |
if self.display_help: |
8c5cf48ae9ea
new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
230 |
w(self.render_help(form, field)) |
8c5cf48ae9ea
new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
231 |
w(u'</td></tr>') |
8c5cf48ae9ea
new fieldset attribute on field, use to group fields by the default form renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
232 |
w(u'</table></fieldset>') |
2574
37154aa90947
[form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2561
diff
changeset
|
233 |
if byfieldset: |
37154aa90947
[form renderer] consider form.fieldsets_in_order if specified. Warn about unexistant or unused fieldsets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2561
diff
changeset
|
234 |
self.warning('unused fieldsets: %s', ', '.join(byfieldset)) |
1147 | 235 |
|
236 |
def render_buttons(self, w, form): |
|
2148
2b2b9626a98f
do not write -invalid- empty table row when form has no buttons
Florent <florent@secondweb.fr>
parents:
2093
diff
changeset
|
237 |
if not form.form_buttons: |
2b2b9626a98f
do not write -invalid- empty table row when form has no buttons
Florent <florent@secondweb.fr>
parents:
2093
diff
changeset
|
238 |
return |
1147 | 239 |
w(u'<table class="%s">\n<tr>\n' % self.button_bar_class) |
1304 | 240 |
for button in form.form_buttons: |
241 |
w(u'<td>%s</td>\n' % button.render(form)) |
|
1147 | 242 |
w(u'</tr></table>') |
243 |
||
4224
5998df006968
refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4181
diff
changeset
|
244 |
def render_error(self, w, err): |
5998df006968
refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4181
diff
changeset
|
245 |
"""return validation error for widget's field, if any""" |
5038
90493551b1eb
[form] fix validation error handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4936
diff
changeset
|
246 |
w(u'<span class="errorMsg">%s</span>' % err) |
4224
5998df006968
refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4181
diff
changeset
|
247 |
|
5998df006968
refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4181
diff
changeset
|
248 |
|
1147 | 249 |
|
2005
e8032965f37a
turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1995
diff
changeset
|
250 |
class BaseFormRenderer(FormRenderer): |
2198
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
251 |
"""use form_renderer_id = 'base' if you want base FormRenderer layout even |
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
252 |
when selected for an entity |
2005
e8032965f37a
turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1995
diff
changeset
|
253 |
""" |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
254 |
__regid__ = 'base' |
2198
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
255 |
|
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
256 |
|
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
257 |
|
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
258 |
class HTableFormRenderer(FormRenderer): |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
259 |
"""display fields horizontally in a table |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
260 |
|
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
261 |
+--------------+--------------+---------+ |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
262 |
| field1 label | field2 label | | |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
263 |
+--------------+--------------+---------+ |
4936
a4b772a0d801
Fixed some of the documentation warnings when building the book with sphinx.
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4783
diff
changeset
|
264 |
| field1 input | field2 input | buttons | |
1436 | 265 |
+--------------+--------------+---------+ |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
266 |
""" |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
267 |
__regid__ = 'htable' |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
268 |
|
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
269 |
display_help = False |
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1401
diff
changeset
|
270 |
def _render_fields(self, fields, w, form): |
3572
606ce53dc557
[forms] some style for htable renderer layout
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3519
diff
changeset
|
271 |
w(u'<table border="0" class="htableForm">') |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
272 |
w(u'<tr>') |
1436 | 273 |
for field in fields: |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
274 |
if self.display_label: |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
275 |
w(u'<th class="labelCol">%s</th>' % self.render_label(form, field)) |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
276 |
if self.display_help: |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
277 |
w(self.render_help(form, field)) |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
278 |
# empty slot for buttons |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2656
diff
changeset
|
279 |
w(u'<th class="labelCol"> </th>') |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
280 |
w(u'</tr>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
281 |
w(u'<tr>') |
1436 | 282 |
for field in fields: |
4224
5998df006968
refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4181
diff
changeset
|
283 |
error = form.field_error(field) |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
284 |
if error: |
1757 | 285 |
w(u'<td class="error">') |
4224
5998df006968
refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4181
diff
changeset
|
286 |
self.render_error(w, error) |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
287 |
else: |
1757 | 288 |
w(u'<td>') |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
289 |
w(field.render(form, self)) |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
290 |
w(u'</td>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
291 |
w(u'<td>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
292 |
for button in form.form_buttons: |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
293 |
w(button.render(form)) |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
294 |
w(u'</td>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
295 |
w(u'</tr>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
296 |
w(u'</table>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
297 |
|
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
298 |
def render_buttons(self, w, form): |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
299 |
pass |
1436 | 300 |
|
301 |
||
1147 | 302 |
class EntityCompositeFormRenderer(FormRenderer): |
303 |
"""specific renderer for multiple entities edition form (muledit)""" |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
304 |
__regid__ = 'composite' |
1436 | 305 |
|
2575
e033939d5664
[form renderer] fix composite form renderer in case the main form has some visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2574
diff
changeset
|
306 |
_main_display_fields = None |
e033939d5664
[form renderer] fix composite form renderer in case the main form has some visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2574
diff
changeset
|
307 |
|
1147 | 308 |
def render_fields(self, w, form, values): |
3513
c002f6488631
[form] replace is_subform by parent_form, carrying more information at the same cost
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3510
diff
changeset
|
309 |
if form.parent_form is None: |
1147 | 310 |
w(u'<table class="listing">') |
2586
879912fe94e1
[xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2577
diff
changeset
|
311 |
subfields = [field for field in form.forms[0].fields |
4076
2e8637a97b89
no more handled by the renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4075
diff
changeset
|
312 |
if field.is_visible()] |
2586
879912fe94e1
[xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2577
diff
changeset
|
313 |
if subfields: |
879912fe94e1
[xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2577
diff
changeset
|
314 |
# main form, display table headers |
879912fe94e1
[xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2577
diff
changeset
|
315 |
w(u'<tr class="header">') |
879912fe94e1
[xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2577
diff
changeset
|
316 |
w(u'<th align="left">%s</th>' % |
879912fe94e1
[xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2577
diff
changeset
|
317 |
tags.input(type='checkbox', |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3398
diff
changeset
|
318 |
title=self._cw._('toggle check boxes'), |
2586
879912fe94e1
[xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2577
diff
changeset
|
319 |
onclick="setCheckboxesState('eid', this.checked)")) |
879912fe94e1
[xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2577
diff
changeset
|
320 |
for field in subfields: |
4313
5d8196a105a7
move base form renderer label handling into a function to reuse it in CompositeFormRenderer and avoid seeing a tuple in column headers...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
321 |
w(u'<th>%s</th>' % field_label(form, field)) |
2586
879912fe94e1
[xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2577
diff
changeset
|
322 |
w(u'</tr>') |
1147 | 323 |
super(EntityCompositeFormRenderer, self).render_fields(w, form, values) |
3513
c002f6488631
[form] replace is_subform by parent_form, carrying more information at the same cost
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3510
diff
changeset
|
324 |
if form.parent_form is None: |
1147 | 325 |
w(u'</table>') |
2575
e033939d5664
[form renderer] fix composite form renderer in case the main form has some visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2574
diff
changeset
|
326 |
if self._main_display_fields: |
e033939d5664
[form renderer] fix composite form renderer in case the main form has some visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2574
diff
changeset
|
327 |
super(EntityCompositeFormRenderer, self)._render_fields( |
e033939d5664
[form renderer] fix composite form renderer in case the main form has some visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2574
diff
changeset
|
328 |
self._main_display_fields, w, form) |
1436 | 329 |
|
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1401
diff
changeset
|
330 |
def _render_fields(self, fields, w, form): |
3513
c002f6488631
[form] replace is_subform by parent_form, carrying more information at the same cost
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3510
diff
changeset
|
331 |
if form.parent_form is not None: |
1147 | 332 |
entity = form.edited_entity |
1710
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1691
diff
changeset
|
333 |
values = form.form_previous_values |
1147 | 334 |
qeid = eid_param('eid', entity.eid) |
2577 | 335 |
cbsetstate = "setCheckboxesState2('eid', %s, 'checked')" % \ |
336 |
xml_escape(dumps(entity.eid)) |
|
3467
a6405235aac6
[tests] make unittest_views_editforms pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
337 |
w(u'<tr class="%s">' % (entity.cw_row % 2 and u'even' or u'odd')) |
1147 | 338 |
# XXX turn this into a widget used on the eid field |
2577 | 339 |
w(u'<td>%s</td>' % checkbox('eid', entity.eid, |
340 |
checked=qeid in values)) |
|
1147 | 341 |
for field in fields: |
4224
5998df006968
refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4181
diff
changeset
|
342 |
error = form.field_error(field) |
1147 | 343 |
if error: |
344 |
w(u'<td class="error">') |
|
4224
5998df006968
refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4181
diff
changeset
|
345 |
self.render_error(w, error) |
1147 | 346 |
else: |
347 |
w(u'<td>') |
|
2577 | 348 |
if isinstance(field.widget, (fwdgs.Select, fwdgs.CheckBox, |
349 |
fwdgs.Radio)): |
|
1147 | 350 |
field.widget.attrs['onchange'] = cbsetstate |
351 |
elif isinstance(field.widget, fwdgs.Input): |
|
352 |
field.widget.attrs['onkeypress'] = cbsetstate |
|
2577 | 353 |
# XXX else |
1147 | 354 |
w(u'<div>%s</div>' % field.render(form, self)) |
2586
879912fe94e1
[xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2577
diff
changeset
|
355 |
w(u'</td>\n') |
879912fe94e1
[xhtml form renderer] fix html structure, move header where we are sure to display it when necesary (_render_fields not displayed if main form has no visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2577
diff
changeset
|
356 |
w(u'</tr>') |
1147 | 357 |
else: |
2575
e033939d5664
[form renderer] fix composite form renderer in case the main form has some visible fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2574
diff
changeset
|
358 |
self._main_display_fields = fields |
1436 | 359 |
|
360 |
||
3478
f043f71017a3
handle display_fields in the form, not in the renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3477
diff
changeset
|
361 |
class EntityFormRenderer(BaseFormRenderer): |
1147 | 362 |
"""specific renderer for entity edition form (edition)""" |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
363 |
__regid__ = 'default' |
2198
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
364 |
# needs some additional points in some case (XXX explain cases) |
4075
e710f4052bd6
use implements instead of entity_implements
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4073
diff
changeset
|
365 |
__select__ = implements('Any') & yes() |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
366 |
|
4293
0c4bdb3d93fd
delete remaining stuff w/ handling of the generic relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4278
diff
changeset
|
367 |
_options = FormRenderer._options + ('main_form_title',) |
3592
4c08780e54ed
[i18n] msgid tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3572
diff
changeset
|
368 |
main_form_title = _('main informations') |
1436 | 369 |
|
1181
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
370 |
def render(self, form, values): |
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
371 |
rendered = super(EntityFormRenderer, self).render(form, values) |
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
372 |
return rendered + u'</div>' # close extra div introducted by open_form |
1436 | 373 |
|
1147 | 374 |
def open_form(self, form, values): |
3507
ff945c7013d9
[form] make EntityFormRenderer a little bit easier to override
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
3360
diff
changeset
|
375 |
attrs_fs_label = '' |
ff945c7013d9
[form] make EntityFormRenderer a little bit easier to override
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
3360
diff
changeset
|
376 |
if self.main_form_title: |
ff945c7013d9
[form] make EntityFormRenderer a little bit easier to override
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
3360
diff
changeset
|
377 |
attrs_fs_label += ('<div class="iformTitle"><span>%s</span></div>' |
4073
03681ba6da0b
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4023
diff
changeset
|
378 |
% self._cw._(self.main_form_title)) |
1147 | 379 |
attrs_fs_label += '<div class="formBody">' |
1181
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
380 |
return attrs_fs_label + super(EntityFormRenderer, self).open_form(form, values) |
1147 | 381 |
|
382 |
def render_buttons(self, w, form): |
|
1304 | 383 |
if len(form.form_buttons) == 3: |
1147 | 384 |
w("""<table width="100%%"> |
385 |
<tbody> |
|
386 |
<tr><td align="center"> |
|
387 |
%s |
|
388 |
</td><td style="align: right; width: 50%%;"> |
|
389 |
%s |
|
390 |
%s |
|
391 |
</td></tr> |
|
392 |
</tbody> |
|
1304 | 393 |
</table>""" % tuple(button.render(form) for button in form.form_buttons)) |
1147 | 394 |
else: |
395 |
super(EntityFormRenderer, self).render_buttons(w, form) |
|
396 |
||
1436 | 397 |
|
1147 | 398 |
class EntityInlinedFormRenderer(EntityFormRenderer): |
399 |
"""specific renderer for entity inlined edition form |
|
400 |
(inline-[creation|edition]) |
|
401 |
""" |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
402 |
__regid__ = 'inline' |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
403 |
|
1147 | 404 |
def render(self, form, values): |
405 |
form.add_media() |
|
406 |
data = [] |
|
407 |
w = data.append |
|
408 |
try: |
|
409 |
w(u'<div id="div-%(divid)s" onclick="%(divonclick)s">' % values) |
|
410 |
except KeyError: |
|
411 |
w(u'<div id="div-%(divid)s">' % values) |
|
412 |
else: |
|
413 |
w(u'<div id="notice-%s" class="notice">%s</div>' % ( |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3398
diff
changeset
|
414 |
values['divid'], self._cw._('click on the box to cancel the deletion'))) |
1147 | 415 |
w(u'<div class="iformBody">') |
3327
44efba78afac
fix/enhance i18n context usage for inlined forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
416 |
eschema = form.edited_entity.e_schema |
4380
5613d7c06339
simplify messages used for inlined forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4378
diff
changeset
|
417 |
values['removemsg'] = self._cw._('remove-inlined-entity-form') |
1147 | 418 |
w(u'<div class="iformTitle"><span>%(title)s</span> ' |
2247
9dbbe6a4c9b0
use a counter of displayed inlined forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2198
diff
changeset
|
419 |
'#<span class="icounter">%(counter)s</span> ' |
1147 | 420 |
'[<a href="javascript: %(removejs)s;noop();">%(removemsg)s</a>]</div>' |
421 |
% values) |
|
1964
b60719223965
need to cleanup values if we don't want them to be interpreted by fields (title is a common field name for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1946
diff
changeset
|
422 |
# cleanup values |
b60719223965
need to cleanup values if we don't want them to be interpreted by fields (title is a common field name for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1946
diff
changeset
|
423 |
for key in ('title', 'removejs', 'removemsg'): |
b60719223965
need to cleanup values if we don't want them to be interpreted by fields (title is a common field name for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1946
diff
changeset
|
424 |
values.pop(key) |
1147 | 425 |
self.render_fields(w, form, values) |
1293 | 426 |
w(u'</div></div>') |
1147 | 427 |
return '\n'.join(data) |
1436 | 428 |
|
1147 | 429 |
def render_fields(self, w, form, values): |
430 |
w(u'<fieldset id="fs-%(divid)s">' % values) |
|
431 |
fields = self._render_hidden_fields(w, form) |
|
432 |
w(u'</fieldset>') |
|
433 |
w(u'<fieldset class="subentity">') |
|
434 |
if fields: |
|
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1401
diff
changeset
|
435 |
self._render_fields(fields, w, form) |
1147 | 436 |
self.render_child_forms(w, form, values) |
437 |
w(u'</fieldset>') |
|
1436 | 438 |