author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 21 Jan 2010 11:07:32 +0100 | |
branch | stable |
changeset 4313 | 5d8196a105a7 |
parent 4212 | ab6573088b4a |
child 4314 | e12ee4a009bc |
permissions | -rw-r--r-- |
1147 | 1 |
"""form renderers, responsible to layout a form to html |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4097
diff
changeset
|
4 |
:copyright: 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 |
||
10 |
from logilab.common import dictattr |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2247
diff
changeset
|
11 |
from logilab.mtconverter import xml_escape |
1147 | 12 |
|
13 |
from simplejson import dumps |
|
14 |
||
15 |
from cubicweb.common import tags |
|
2656
a93ae0f6c0ad
R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2586
diff
changeset
|
16 |
from cubicweb.appobject import AppObject |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
17 |
from cubicweb.selectors import entity_implements, yes |
1147 | 18 |
from cubicweb.web import eid_param |
19 |
from cubicweb.web import formwidgets as fwdgs |
|
20 |
from cubicweb.web.widgets import checkbox |
|
1601
171afa270086
hidden input fields : compute displayability using the associated visible field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1576
diff
changeset
|
21 |
from cubicweb.web.formfields import HiddenInitialValueField |
1147 | 22 |
|
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
|
23 |
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
|
24 |
# 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
|
25 |
# 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
|
26 |
if isinstance(field.label, tuple): # i.e. needs contextual translation |
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
|
27 |
return form.req.pgettext(*field.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
|
28 |
return form.req._(field.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
|
29 |
|
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 |
|
1319 | 31 |
|
2656
a93ae0f6c0ad
R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2586
diff
changeset
|
32 |
class FormRenderer(AppObject): |
1147 | 33 |
"""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
|
34 |
|
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
35 |
+--------------+--------------+ |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
36 |
| field1 label | field1 input | |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
37 |
+--------------+--------------+ |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
38 |
| field1 label | field2 input | |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
39 |
+--------------+--------------+ |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
40 |
+---------+ |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
41 |
| buttons | |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
42 |
+---------+ |
1147 | 43 |
""" |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
44 |
__registry__ = 'formrenderers' |
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
45 |
id = 'default' |
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
46 |
|
1434
42e57dbbc585
new display_relations_form option on EntityFormRender
sylvain.thenault@logilab.fr
parents:
1412
diff
changeset
|
47 |
_options = ('display_fields', 'display_label', 'display_help', |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
48 |
'display_progress_div', 'table_class', 'button_bar_class', |
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
49 |
# 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
|
50 |
'entity') |
1362
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
51 |
display_fields = None # None -> all fields |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
52 |
display_label = True |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
53 |
display_help = True |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
54 |
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
|
55 |
table_class = u'attributeForm' |
1147 | 56 |
button_bar_class = u'formButtonBar' |
1436 | 57 |
|
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
58 |
def __init__(self, req=None, rset=None, row=None, col=None, **kwargs): |
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
59 |
super(FormRenderer, self).__init__(req, rset, row, col) |
1362
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
60 |
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
|
61 |
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
|
62 |
|
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
63 |
def _set_options(self, kwargs): |
1434
42e57dbbc585
new display_relations_form option on EntityFormRender
sylvain.thenault@logilab.fr
parents:
1412
diff
changeset
|
64 |
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
|
65 |
try: |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
66 |
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
|
67 |
except KeyError: |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
68 |
continue |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
69 |
return kwargs |
1436 | 70 |
|
1147 | 71 |
# renderer interface ###################################################### |
1436 | 72 |
|
1147 | 73 |
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
|
74 |
self._set_options(values) |
1147 | 75 |
form.add_media() |
76 |
data = [] |
|
77 |
w = data.append |
|
78 |
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
|
79 |
if self.display_progress_div: |
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
80 |
w(u'<div id="progress">%s</div>' % self.req._('validating...')) |
1147 | 81 |
w(u'<fieldset>') |
82 |
w(tags.input(type=u'hidden', name=u'__form_id', |
|
83 |
value=values.get('formvid', form.id))) |
|
84 |
if form.redirect_path: |
|
85 |
w(tags.input(type='hidden', name='__redirectpath', value=form.redirect_path)) |
|
86 |
self.render_fields(w, form, values) |
|
87 |
self.render_buttons(w, form) |
|
88 |
w(u'</fieldset>') |
|
89 |
w(u'</form>') |
|
90 |
errormsg = self.error_message(form) |
|
91 |
if errormsg: |
|
1436 | 92 |
data.insert(0, errormsg) |
1147 | 93 |
return '\n'.join(data) |
94 |
||
95 |
def render_label(self, form, field): |
|
2521
782360fa6cc0
take care to None labels
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2520
diff
changeset
|
96 |
if field.label is None: |
782360fa6cc0
take care to None labels
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2520
diff
changeset
|
97 |
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
|
98 |
label = field_label(form, field) |
1147 | 99 |
attrs = {'for': form.context[field]['id']} |
100 |
if field.required: |
|
101 |
attrs['class'] = 'required' |
|
102 |
return tags.label(label, **attrs) |
|
103 |
||
104 |
def render_help(self, form, field): |
|
1946
d60ddfc4bb09
don't use tab
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1863
diff
changeset
|
105 |
help = [] |
1147 | 106 |
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
|
107 |
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
|
108 |
descr = descr(form) |
1147 | 109 |
if descr: |
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
110 |
help.append('<div class="helper">%s</div>' % self.req._(descr)) |
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
111 |
example = field.example_format(self.req) |
1147 | 112 |
if example: |
1863
9a02979a2c00
do not use <br> in widgets
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
1757
diff
changeset
|
113 |
help.append('<div class="helper">(%s: %s)</div>' |
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
114 |
% (self.req._('sample format'), example)) |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2656
diff
changeset
|
115 |
return u' '.join(help) |
1147 | 116 |
|
117 |
# specific methods (mostly to ease overriding) ############################# |
|
118 |
||
119 |
def error_message(self, form): |
|
120 |
"""return formatted error message |
|
121 |
||
122 |
This method should be called once inlined field errors has been consumed |
|
123 |
""" |
|
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
124 |
req = self.req |
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
|
125 |
errex = form.form_valerror |
1147 | 126 |
# get extra errors |
127 |
if errex is not None: |
|
128 |
errormsg = req._('please correct the following errors:') |
|
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
|
129 |
displayed = form.form_displayed_errors |
1147 | 130 |
errors = sorted((field, err) for field, err in errex.errors.items() |
131 |
if not field in displayed) |
|
132 |
if errors: |
|
133 |
if len(errors) > 1: |
|
1436 | 134 |
templstr = '<li>%s</li>\n' |
1147 | 135 |
else: |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2656
diff
changeset
|
136 |
templstr = ' %s\n' |
1147 | 137 |
for field, err in errors: |
138 |
if field is None: |
|
139 |
errormsg += templstr % err |
|
140 |
else: |
|
141 |
errormsg += templstr % '%s: %s' % (req._(field), err) |
|
142 |
if len(errors) > 1: |
|
143 |
errormsg = '<ul>%s</ul>' % errormsg |
|
144 |
return u'<div class="errorMessage">%s</div>' % errormsg |
|
145 |
return u'' |
|
1436 | 146 |
|
1147 | 147 |
def open_form(self, form, values): |
148 |
if form.form_needs_multipart: |
|
149 |
enctype = 'multipart/form-data' |
|
150 |
else: |
|
151 |
enctype = 'application/x-www-form-urlencoded' |
|
152 |
if form.action is None: |
|
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
153 |
action = self.req.build_url('edit') |
1147 | 154 |
else: |
155 |
action = form.action |
|
1274
b730932a79b7
we don't want dom id systematically
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
156 |
tag = ('<form action="%s" method="post" enctype="%s"' % ( |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2247
diff
changeset
|
157 |
xml_escape(action or '#'), enctype)) |
1274
b730932a79b7
we don't want dom id systematically
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
158 |
if form.domid: |
b730932a79b7
we don't want dom id systematically
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
159 |
tag += ' id="%s"' % form.domid |
1147 | 160 |
if form.onsubmit: |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2247
diff
changeset
|
161 |
tag += ' onsubmit="%s"' % xml_escape(form.onsubmit % dictattr(form)) |
1147 | 162 |
if form.cssstyle: |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2247
diff
changeset
|
163 |
tag += ' style="%s"' % xml_escape(form.cssstyle) |
1147 | 164 |
if form.cssclass: |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2247
diff
changeset
|
165 |
tag += ' class="%s"' % xml_escape(form.cssclass) |
1147 | 166 |
if form.cwtarget: |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2247
diff
changeset
|
167 |
tag += ' cubicweb:target="%s"' % xml_escape(form.cwtarget) |
1147 | 168 |
return tag + '>' |
1436 | 169 |
|
1147 | 170 |
def display_field(self, form, field): |
1601
171afa270086
hidden input fields : compute displayability using the associated visible field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1576
diff
changeset
|
171 |
if isinstance(field, HiddenInitialValueField): |
171afa270086
hidden input fields : compute displayability using the associated visible field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1576
diff
changeset
|
172 |
field = field.visible_field |
1147 | 173 |
return (self.display_fields is None |
1628 | 174 |
or field.name in form.internal_fields |
1576
3bfcf1e4eb26
display_fields should search for name+role
sylvain.thenault@logilab.fr
parents:
1436
diff
changeset
|
175 |
or (field.name, field.role) in self.display_fields |
3bfcf1e4eb26
display_fields should search for name+role
sylvain.thenault@logilab.fr
parents:
1436
diff
changeset
|
176 |
or (field.name, field.role) in form.internal_fields) |
1436 | 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: |
|
192 |
if not self.display_field(form, field): |
|
193 |
fields.remove(field) |
|
194 |
elif not field.is_visible(): |
|
195 |
w(field.render(form, self)) |
|
196 |
fields.remove(field) |
|
197 |
return fields |
|
1436 | 198 |
|
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1401
diff
changeset
|
199 |
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
|
200 |
byfieldset = {} |
1147 | 201 |
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
|
202 |
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
|
203 |
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
|
204 |
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
|
205 |
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
|
206 |
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
|
207 |
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
|
208 |
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
|
209 |
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
|
210 |
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
|
211 |
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
|
212 |
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
|
213 |
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
|
214 |
if fieldset: |
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 |
w(u'<legend>%s</legend>' % self.req._(fieldset)) |
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
|
216 |
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
|
217 |
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
|
218 |
w(u'<tr class="%s_%s_row">' % (field.name, field.role)) |
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
|
219 |
if self.display_label: |
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
|
220 |
w(u'<th class="labelCol">%s</th>' % self.render_label(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
|
221 |
error = form.form_field_error(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
|
222 |
if error: |
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 |
w(u'<td class="error">') |
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
|
224 |
w(error) |
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
|
225 |
else: |
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(u'<td>') |
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
|
227 |
w(field.render(form, self)) |
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
|
228 |
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
|
229 |
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
|
230 |
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
|
231 |
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
|
232 |
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
|
233 |
self.warning('unused fieldsets: %s', ', '.join(byfieldset)) |
1147 | 234 |
|
235 |
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
|
236 |
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
|
237 |
return |
1147 | 238 |
w(u'<table class="%s">\n<tr>\n' % self.button_bar_class) |
1304 | 239 |
for button in form.form_buttons: |
240 |
w(u'<td>%s</td>\n' % button.render(form)) |
|
1147 | 241 |
w(u'</tr></table>') |
242 |
||
243 |
||
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
|
244 |
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
|
245 |
"""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
|
246 |
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
|
247 |
""" |
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
|
248 |
id = 'base' |
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
|
249 |
|
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 |
|
2198
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
251 |
class EntityBaseFormRenderer(BaseFormRenderer): |
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
252 |
"""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
|
253 |
when selected for an entity |
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
254 |
""" |
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
255 |
__select__ = entity_implements('Any') |
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 |
def display_field(self, form, field): |
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
258 |
if not super(EntityBaseFormRenderer, self).display_field(form, field): |
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
259 |
if isinstance(field, HiddenInitialValueField): |
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
260 |
field = field.visible_field |
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
261 |
ismeta = form.edited_entity.e_schema.is_metadata(field.name) |
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
262 |
return ismeta is not None and ( |
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
263 |
ismeta[0] in self.display_fields or |
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
264 |
(ismeta[0], 'subject') in self.display_fields) |
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
265 |
return True |
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
266 |
|
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
267 |
|
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
268 |
class HTableFormRenderer(FormRenderer): |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
269 |
"""display fields horizontally in a table |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
270 |
|
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
271 |
+--------------+--------------+---------+ |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
272 |
| field1 label | field2 label | | |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
273 |
+--------------+--------------+---------+ |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
274 |
| field1 input | field2 input | buttons |
1436 | 275 |
+--------------+--------------+---------+ |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
276 |
""" |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
277 |
id = 'htable' |
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
278 |
|
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
279 |
display_help = False |
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1401
diff
changeset
|
280 |
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
|
281 |
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
|
282 |
w(u'<tr>') |
1436 | 283 |
for field in fields: |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
284 |
if self.display_label: |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
285 |
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
|
286 |
if self.display_help: |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
287 |
w(self.render_help(form, field)) |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
288 |
# empty slot for buttons |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2656
diff
changeset
|
289 |
w(u'<th class="labelCol"> </th>') |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
290 |
w(u'</tr>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
291 |
w(u'<tr>') |
1436 | 292 |
for field in fields: |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
293 |
error = form.form_field_error(field) |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
294 |
if error: |
1757 | 295 |
w(u'<td class="error">') |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
296 |
w(error) |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
297 |
else: |
1757 | 298 |
w(u'<td>') |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
299 |
w(field.render(form, self)) |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
300 |
w(u'</td>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
301 |
w(u'<td>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
302 |
for button in form.form_buttons: |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
303 |
w(button.render(form)) |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
304 |
w(u'</td>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
305 |
w(u'</tr>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
306 |
w(u'</table>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
307 |
|
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
308 |
def render_buttons(self, w, form): |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
309 |
pass |
1436 | 310 |
|
311 |
||
1147 | 312 |
class EntityCompositeFormRenderer(FormRenderer): |
313 |
"""specific renderer for multiple entities edition form (muledit)""" |
|
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
314 |
id = 'composite' |
1436 | 315 |
|
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
|
316 |
_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
|
317 |
|
1147 | 318 |
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
|
319 |
if form.parent_form is None: |
1147 | 320 |
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
|
321 |
subfields = [field for field in form.forms[0].fields |
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 |
if self.display_field(form, field) |
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
|
323 |
and field.is_visible()] |
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
|
324 |
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
|
325 |
# 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
|
326 |
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
|
327 |
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
|
328 |
tags.input(type='checkbox', |
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
|
329 |
title=self.req._('toggle check boxes'), |
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
|
330 |
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
|
331 |
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
|
332 |
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
|
333 |
w(u'</tr>') |
1147 | 334 |
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
|
335 |
if form.parent_form is None: |
1147 | 336 |
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
|
337 |
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
|
338 |
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
|
339 |
self._main_display_fields, w, form) |
1436 | 340 |
|
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1401
diff
changeset
|
341 |
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
|
342 |
if form.parent_form is not None: |
1147 | 343 |
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
|
344 |
values = form.form_previous_values |
1147 | 345 |
qeid = eid_param('eid', entity.eid) |
2577 | 346 |
cbsetstate = "setCheckboxesState2('eid', %s, 'checked')" % \ |
347 |
xml_escape(dumps(entity.eid)) |
|
1147 | 348 |
w(u'<tr class="%s">' % (entity.row % 2 and u'even' or u'odd')) |
349 |
# XXX turn this into a widget used on the eid field |
|
2577 | 350 |
w(u'<td>%s</td>' % checkbox('eid', entity.eid, |
351 |
checked=qeid in values)) |
|
1147 | 352 |
for field in fields: |
353 |
error = form.form_field_error(field) |
|
354 |
if error: |
|
355 |
w(u'<td class="error">') |
|
356 |
w(error) |
|
357 |
else: |
|
358 |
w(u'<td>') |
|
2577 | 359 |
if isinstance(field.widget, (fwdgs.Select, fwdgs.CheckBox, |
360 |
fwdgs.Radio)): |
|
1147 | 361 |
field.widget.attrs['onchange'] = cbsetstate |
362 |
elif isinstance(field.widget, fwdgs.Input): |
|
363 |
field.widget.attrs['onkeypress'] = cbsetstate |
|
2577 | 364 |
# XXX else |
1147 | 365 |
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
|
366 |
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
|
367 |
w(u'</tr>') |
1147 | 368 |
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
|
369 |
self._main_display_fields = fields |
1436 | 370 |
|
371 |
||
2198
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
372 |
class EntityFormRenderer(EntityBaseFormRenderer): |
1147 | 373 |
"""specific renderer for entity edition form (edition)""" |
2198
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
374 |
id = 'default' |
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
375 |
# needs some additional points in some case (XXX explain cases) |
ac45d4dbaf76
close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2148
diff
changeset
|
376 |
__select__ = EntityBaseFormRenderer.__select__ & yes() |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
377 |
|
3507
ff945c7013d9
[form] make EntityFormRenderer a little bit easier to override
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
3360
diff
changeset
|
378 |
_options = FormRenderer._options + ('display_relations_form', 'main_form_title') |
1434
42e57dbbc585
new display_relations_form option on EntityFormRender
sylvain.thenault@logilab.fr
parents:
1412
diff
changeset
|
379 |
display_relations_form = True |
3592
4c08780e54ed
[i18n] msgid tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3572
diff
changeset
|
380 |
main_form_title = _('main informations') |
1436 | 381 |
|
1181
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
382 |
def render(self, form, values): |
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
383 |
rendered = super(EntityFormRenderer, self).render(form, values) |
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
384 |
return rendered + u'</div>' # close extra div introducted by open_form |
1436 | 385 |
|
1147 | 386 |
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
|
387 |
attrs_fs_label = '' |
ff945c7013d9
[form] make EntityFormRenderer a little bit easier to override
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
3360
diff
changeset
|
388 |
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
|
389 |
attrs_fs_label += ('<div class="iformTitle"><span>%s</span></div>' |
ff945c7013d9
[form] make EntityFormRenderer a little bit easier to override
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
3360
diff
changeset
|
390 |
% self.req._(self.main_form_title)) |
1147 | 391 |
attrs_fs_label += '<div class="formBody">' |
1181
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
392 |
return attrs_fs_label + super(EntityFormRenderer, self).open_form(form, values) |
1147 | 393 |
|
394 |
def render_fields(self, w, form, values): |
|
395 |
super(EntityFormRenderer, self).render_fields(w, form, values) |
|
396 |
self.inline_entities_form(w, form) |
|
1436 | 397 |
if form.edited_entity.has_eid() and self.display_relations_form: |
1147 | 398 |
self.relations_form(w, form) |
399 |
||
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1401
diff
changeset
|
400 |
def _render_fields(self, fields, w, form): |
1147 | 401 |
if not form.edited_entity.has_eid() or form.edited_entity.has_perm('update'): |
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1401
diff
changeset
|
402 |
super(EntityFormRenderer, self)._render_fields(fields, w, form) |
1436 | 403 |
|
1147 | 404 |
def render_buttons(self, w, form): |
1304 | 405 |
if len(form.form_buttons) == 3: |
1147 | 406 |
w("""<table width="100%%"> |
407 |
<tbody> |
|
408 |
<tr><td align="center"> |
|
409 |
%s |
|
410 |
</td><td style="align: right; width: 50%%;"> |
|
411 |
%s |
|
412 |
%s |
|
413 |
</td></tr> |
|
414 |
</tbody> |
|
1304 | 415 |
</table>""" % tuple(button.render(form) for button in form.form_buttons)) |
1147 | 416 |
else: |
417 |
super(EntityFormRenderer, self).render_buttons(w, form) |
|
1436 | 418 |
|
1147 | 419 |
def relations_form(self, w, form): |
3244
1fc804b2c5ba
since unrelated_rql now consider security, we may consider returning relation having local role in some cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
420 |
srels_by_cat = form.srelations_by_category('generic', 'add', strict=True) |
1147 | 421 |
if not srels_by_cat: |
422 |
return u'' |
|
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
423 |
req = self.req |
1147 | 424 |
_ = req._ |
3263
5129918c671b
i18n update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3244
diff
changeset
|
425 |
__ = _ |
5129918c671b
i18n update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3244
diff
changeset
|
426 |
label = u'%s :' % __('This %s' % form.edited_entity.e_schema).capitalize() |
1147 | 427 |
eid = form.edited_entity.eid |
428 |
w(u'<fieldset class="subentity">') |
|
429 |
w(u'<legend class="iformTitle">%s</legend>' % label) |
|
430 |
w(u'<table id="relatedEntities">') |
|
431 |
for rschema, target, related in form.relations_table(): |
|
432 |
# already linked entities |
|
433 |
if related: |
|
434 |
w(u'<tr><th class="labelCol">%s</th>' % rschema.display_name(req, target)) |
|
435 |
w(u'<td>') |
|
436 |
w(u'<ul>') |
|
437 |
for viewparams in related: |
|
438 |
w(u'<li class="invisible">%s<div id="span%s" class="%s">%s</div></li>' |
|
439 |
% (viewparams[1], viewparams[0], viewparams[2], viewparams[3])) |
|
440 |
if not form.force_display and form.maxrelitems < len(related): |
|
1436 | 441 |
link = (u'<span class="invisible">' |
1147 | 442 |
'[<a href="javascript: window.location.href+=\'&__force_display=1\'">%s</a>]' |
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
443 |
'</span>' % self.req._('view all')) |
1147 | 444 |
w(u'<li class="invisible">%s</li>' % link) |
445 |
w(u'</ul>') |
|
446 |
w(u'</td>') |
|
447 |
w(u'</tr>') |
|
448 |
pendings = list(form.restore_pending_inserts()) |
|
449 |
if not pendings: |
|
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2656
diff
changeset
|
450 |
w(u'<tr><th> </th><td> </td></tr>') |
1147 | 451 |
else: |
452 |
for row in pendings: |
|
453 |
# soon to be linked to entities |
|
454 |
w(u'<tr id="tr%s">' % row[1]) |
|
455 |
w(u'<th>%s</th>' % row[3]) |
|
456 |
w(u'<td>') |
|
457 |
w(u'<a class="handle" title="%s" href="%s">[x]</a>' % |
|
458 |
(_('cancel this insert'), row[2])) |
|
459 |
w(u'<a id="a%s" class="editionPending" href="%s">%s</a>' |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2247
diff
changeset
|
460 |
% (row[1], row[4], xml_escape(row[5]))) |
1147 | 461 |
w(u'</td>') |
462 |
w(u'</tr>') |
|
463 |
w(u'<tr id="relationSelectorRow_%s" class="separator">' % eid) |
|
464 |
w(u'<th class="labelCol">') |
|
1305
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
465 |
w(u'<select id="relationSelector_%s" tabindex="%s" ' |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
466 |
'onchange="javascript:showMatchingSelect(this.options[this.selectedIndex].value,%s);">' |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2247
diff
changeset
|
467 |
% (eid, req.next_tabindex(), xml_escape(dumps(eid)))) |
1147 | 468 |
w(u'<option value="">%s</option>' % _('select a relation')) |
469 |
for i18nrtype, rschema, target in srels_by_cat: |
|
470 |
# more entities to link to |
|
471 |
w(u'<option value="%s_%s">%s</option>' % (rschema, target, i18nrtype)) |
|
472 |
w(u'</select>') |
|
473 |
w(u'</th>') |
|
474 |
w(u'<td id="unrelatedDivs_%s"></td>' % eid) |
|
475 |
w(u'</tr>') |
|
476 |
w(u'</table>') |
|
477 |
w(u'</fieldset>') |
|
1436 | 478 |
|
3360
b02df886eb3e
cleanup, reorganize
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3358
diff
changeset
|
479 |
# NOTE: should_* and display_* method extracted and moved to the form to |
b02df886eb3e
cleanup, reorganize
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3358
diff
changeset
|
480 |
# ease overriding |
b02df886eb3e
cleanup, reorganize
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3358
diff
changeset
|
481 |
|
1147 | 482 |
def inline_entities_form(self, w, form): |
483 |
"""create a form to edit entity's inlined relations""" |
|
3518
11ce4682187d
[form] important refactoring of inlined forms to get proper separation of form object creation / rendering
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3513
diff
changeset
|
484 |
if not hasattr(form, 'inlined_form_views'): |
1967
28eed65ce1e6
don't crash if inlined_relation is not defined on the form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1964
diff
changeset
|
485 |
return |
3518
11ce4682187d
[form] important refactoring of inlined forms to get proper separation of form object creation / rendering
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3513
diff
changeset
|
486 |
keysinorder = [] |
11ce4682187d
[form] important refactoring of inlined forms to get proper separation of form object creation / rendering
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3513
diff
changeset
|
487 |
formviews = form.inlined_form_views() |
11ce4682187d
[form] important refactoring of inlined forms to get proper separation of form object creation / rendering
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3513
diff
changeset
|
488 |
for formview in formviews: |
11ce4682187d
[form] important refactoring of inlined forms to get proper separation of form object creation / rendering
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3513
diff
changeset
|
489 |
if not (formview.rtype, formview.role) in keysinorder: |
11ce4682187d
[form] important refactoring of inlined forms to get proper separation of form object creation / rendering
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3513
diff
changeset
|
490 |
keysinorder.append( (formview.rtype, formview.role) ) |
11ce4682187d
[form] important refactoring of inlined forms to get proper separation of form object creation / rendering
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3513
diff
changeset
|
491 |
for key in keysinorder: |
11ce4682187d
[form] important refactoring of inlined forms to get proper separation of form object creation / rendering
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3513
diff
changeset
|
492 |
self.inline_relation_form(w, form, [fv for fv in formviews |
11ce4682187d
[form] important refactoring of inlined forms to get proper separation of form object creation / rendering
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3513
diff
changeset
|
493 |
if (fv.rtype, fv.role) == key]) |
2103
41840b1a2a1a
make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents:
2006
diff
changeset
|
494 |
|
3518
11ce4682187d
[form] important refactoring of inlined forms to get proper separation of form object creation / rendering
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3513
diff
changeset
|
495 |
def inline_relation_form(self, w, form, formviews): |
11ce4682187d
[form] important refactoring of inlined forms to get proper separation of form object creation / rendering
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3513
diff
changeset
|
496 |
i18nctx = 'inlined:%s.%s.%s' % (form.edited_entity.e_schema, |
11ce4682187d
[form] important refactoring of inlined forms to get proper separation of form object creation / rendering
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3513
diff
changeset
|
497 |
formviews[0].rtype, formviews[0].role) |
11ce4682187d
[form] important refactoring of inlined forms to get proper separation of form object creation / rendering
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3513
diff
changeset
|
498 |
w(u'<div id="inline%sslot">' % formviews[0].rtype) |
11ce4682187d
[form] important refactoring of inlined forms to get proper separation of form object creation / rendering
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3513
diff
changeset
|
499 |
for formview in formviews: |
11ce4682187d
[form] important refactoring of inlined forms to get proper separation of form object creation / rendering
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3513
diff
changeset
|
500 |
w(formview.render(i18nctx=i18nctx, row=formview.row, col=formview.col)) |
2103
41840b1a2a1a
make inline relation forms individually renderable
Florent <florent@secondweb.fr>
parents:
2006
diff
changeset
|
501 |
w(u'</div>') |
1147 | 502 |
|
1436 | 503 |
|
1147 | 504 |
class EntityInlinedFormRenderer(EntityFormRenderer): |
505 |
"""specific renderer for entity inlined edition form |
|
506 |
(inline-[creation|edition]) |
|
507 |
""" |
|
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
508 |
id = 'inline' |
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
509 |
|
1147 | 510 |
def render(self, form, values): |
511 |
form.add_media() |
|
512 |
data = [] |
|
513 |
w = data.append |
|
514 |
try: |
|
515 |
w(u'<div id="div-%(divid)s" onclick="%(divonclick)s">' % values) |
|
516 |
except KeyError: |
|
517 |
w(u'<div id="div-%(divid)s">' % values) |
|
518 |
else: |
|
519 |
w(u'<div id="notice-%s" class="notice">%s</div>' % ( |
|
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
520 |
values['divid'], self.req._('click on the box to cancel the deletion'))) |
1147 | 521 |
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
|
522 |
eschema = form.edited_entity.e_schema |
44efba78afac
fix/enhance i18n context usage for inlined forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
523 |
ctx = values.pop('i18nctx') |
44efba78afac
fix/enhance i18n context usage for inlined forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
524 |
values['removemsg'] = self.req.pgettext(ctx, 'remove this %s' % eschema) |
1147 | 525 |
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
|
526 |
'#<span class="icounter">%(counter)s</span> ' |
1147 | 527 |
'[<a href="javascript: %(removejs)s;noop();">%(removemsg)s</a>]</div>' |
528 |
% 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
|
529 |
# 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
|
530 |
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
|
531 |
values.pop(key) |
1147 | 532 |
self.render_fields(w, form, values) |
1293 | 533 |
w(u'</div></div>') |
1147 | 534 |
return '\n'.join(data) |
1436 | 535 |
|
1147 | 536 |
def render_fields(self, w, form, values): |
537 |
w(u'<fieldset id="fs-%(divid)s">' % values) |
|
538 |
fields = self._render_hidden_fields(w, form) |
|
539 |
w(u'</fieldset>') |
|
540 |
w(u'<fieldset class="subentity">') |
|
541 |
if fields: |
|
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1401
diff
changeset
|
542 |
self._render_fields(fields, w, form) |
1147 | 543 |
self.render_child_forms(w, form, values) |
544 |
self.inline_entities_form(w, form) |
|
545 |
w(u'</fieldset>') |
|
1436 | 546 |