author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Fri, 29 May 2009 19:34:00 +0200 | |
changeset 2009 | b1e2b9e4c809 |
parent 2006 | 78d5b57d4964 |
child 2093 | e3aaf61f8c66 |
child 2103 | 41840b1a2a1a |
permissions | -rw-r--r-- |
1147 | 1 |
"""form renderers, responsible to layout a form to html |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1967
diff
changeset
|
4 |
:copyright: 2009 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 |
|
11 |
from logilab.mtconverter import html_escape |
|
12 |
||
13 |
from simplejson import dumps |
|
14 |
||
15 |
from cubicweb.common import tags |
|
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
16 |
from cubicweb.appobject import AppRsetObject |
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 |
|
1319 | 23 |
|
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
24 |
class FormRenderer(AppRsetObject): |
1147 | 25 |
"""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
|
26 |
|
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
27 |
+--------------+--------------+ |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
28 |
| field1 label | field1 input | |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
29 |
+--------------+--------------+ |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
30 |
| field1 label | field2 input | |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
31 |
+--------------+--------------+ |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
32 |
+---------+ |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
33 |
| buttons | |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
34 |
+---------+ |
1147 | 35 |
""" |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
36 |
__registry__ = 'formrenderers' |
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
37 |
id = 'default' |
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
38 |
|
1434
42e57dbbc585
new display_relations_form option on EntityFormRender
sylvain.thenault@logilab.fr
parents:
1412
diff
changeset
|
39 |
_options = ('display_fields', 'display_label', 'display_help', |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
40 |
'display_progress_div', 'table_class', 'button_bar_class', |
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
41 |
# 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
|
42 |
'entity') |
1362
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
43 |
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
|
44 |
display_label = True |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
45 |
display_help = True |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
46 |
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
|
47 |
table_class = u'attributeForm' |
1147 | 48 |
button_bar_class = u'formButtonBar' |
1436 | 49 |
|
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
50 |
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
|
51 |
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
|
52 |
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
|
53 |
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
|
54 |
|
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
55 |
def _set_options(self, kwargs): |
1434
42e57dbbc585
new display_relations_form option on EntityFormRender
sylvain.thenault@logilab.fr
parents:
1412
diff
changeset
|
56 |
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
|
57 |
try: |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
58 |
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
|
59 |
except KeyError: |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
60 |
continue |
decb7507fa91
nicer renderer options handling: can be passed to __init__ or to render
sylvain.thenault@logilab.fr
parents:
1340
diff
changeset
|
61 |
return kwargs |
1436 | 62 |
|
1147 | 63 |
# renderer interface ###################################################### |
1436 | 64 |
|
1147 | 65 |
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
|
66 |
self._set_options(values) |
1147 | 67 |
form.add_media() |
68 |
data = [] |
|
69 |
w = data.append |
|
70 |
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
|
71 |
if self.display_progress_div: |
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
72 |
w(u'<div id="progress">%s</div>' % self.req._('validating...')) |
1147 | 73 |
w(u'<fieldset>') |
74 |
w(tags.input(type=u'hidden', name=u'__form_id', |
|
75 |
value=values.get('formvid', form.id))) |
|
76 |
if form.redirect_path: |
|
77 |
w(tags.input(type='hidden', name='__redirectpath', value=form.redirect_path)) |
|
78 |
self.render_fields(w, form, values) |
|
79 |
self.render_buttons(w, form) |
|
80 |
w(u'</fieldset>') |
|
81 |
w(u'</form>') |
|
82 |
errormsg = self.error_message(form) |
|
83 |
if errormsg: |
|
1436 | 84 |
data.insert(0, errormsg) |
1147 | 85 |
return '\n'.join(data) |
86 |
||
87 |
def render_label(self, form, field): |
|
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
88 |
label = self.req._(field.label) |
1147 | 89 |
attrs = {'for': form.context[field]['id']} |
90 |
if field.required: |
|
91 |
attrs['class'] = 'required' |
|
92 |
return tags.label(label, **attrs) |
|
93 |
||
94 |
def render_help(self, form, field): |
|
1946
d60ddfc4bb09
don't use tab
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1863
diff
changeset
|
95 |
help = [] |
1147 | 96 |
descr = field.help |
97 |
if descr: |
|
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
98 |
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
|
99 |
example = field.example_format(self.req) |
1147 | 100 |
if example: |
1863
9a02979a2c00
do not use <br> in widgets
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
1757
diff
changeset
|
101 |
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
|
102 |
% (self.req._('sample format'), example)) |
1147 | 103 |
return u' '.join(help) |
104 |
||
105 |
# specific methods (mostly to ease overriding) ############################# |
|
106 |
||
107 |
def error_message(self, form): |
|
108 |
"""return formatted error message |
|
109 |
||
110 |
This method should be called once inlined field errors has been consumed |
|
111 |
""" |
|
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
112 |
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
|
113 |
errex = form.form_valerror |
1147 | 114 |
# get extra errors |
115 |
if errex is not None: |
|
116 |
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
|
117 |
displayed = form.form_displayed_errors |
1147 | 118 |
errors = sorted((field, err) for field, err in errex.errors.items() |
119 |
if not field in displayed) |
|
120 |
if errors: |
|
121 |
if len(errors) > 1: |
|
1436 | 122 |
templstr = '<li>%s</li>\n' |
1147 | 123 |
else: |
124 |
templstr = ' %s\n' |
|
125 |
for field, err in errors: |
|
126 |
if field is None: |
|
127 |
errormsg += templstr % err |
|
128 |
else: |
|
129 |
errormsg += templstr % '%s: %s' % (req._(field), err) |
|
130 |
if len(errors) > 1: |
|
131 |
errormsg = '<ul>%s</ul>' % errormsg |
|
132 |
return u'<div class="errorMessage">%s</div>' % errormsg |
|
133 |
return u'' |
|
1436 | 134 |
|
1147 | 135 |
def open_form(self, form, values): |
136 |
if form.form_needs_multipart: |
|
137 |
enctype = 'multipart/form-data' |
|
138 |
else: |
|
139 |
enctype = 'application/x-www-form-urlencoded' |
|
140 |
if form.action is None: |
|
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
141 |
action = self.req.build_url('edit') |
1147 | 142 |
else: |
143 |
action = form.action |
|
1274
b730932a79b7
we don't want dom id systematically
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
144 |
tag = ('<form action="%s" method="post" enctype="%s"' % ( |
b730932a79b7
we don't want dom id systematically
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
145 |
html_escape(action or '#'), enctype)) |
b730932a79b7
we don't want dom id systematically
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
146 |
if form.domid: |
b730932a79b7
we don't want dom id systematically
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
147 |
tag += ' id="%s"' % form.domid |
1147 | 148 |
if form.onsubmit: |
149 |
tag += ' onsubmit="%s"' % html_escape(form.onsubmit % dictattr(form)) |
|
150 |
if form.cssstyle: |
|
151 |
tag += ' style="%s"' % html_escape(form.cssstyle) |
|
152 |
if form.cssclass: |
|
153 |
tag += ' class="%s"' % html_escape(form.cssclass) |
|
154 |
if form.cwtarget: |
|
155 |
tag += ' cubicweb:target="%s"' % html_escape(form.cwtarget) |
|
156 |
return tag + '>' |
|
1436 | 157 |
|
1147 | 158 |
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
|
159 |
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
|
160 |
field = field.visible_field |
1147 | 161 |
return (self.display_fields is None |
1628 | 162 |
or field.name in form.internal_fields |
1576
3bfcf1e4eb26
display_fields should search for name+role
sylvain.thenault@logilab.fr
parents:
1436
diff
changeset
|
163 |
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
|
164 |
or (field.name, field.role) in form.internal_fields) |
1436 | 165 |
|
1147 | 166 |
def render_fields(self, w, form, values): |
167 |
form.form_build_context(values) |
|
168 |
fields = self._render_hidden_fields(w, form) |
|
169 |
if fields: |
|
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1401
diff
changeset
|
170 |
self._render_fields(fields, w, form) |
1147 | 171 |
self.render_child_forms(w, form, values) |
1436 | 172 |
|
1147 | 173 |
def render_child_forms(self, w, form, values): |
1436 | 174 |
# render |
1147 | 175 |
for childform in getattr(form, 'forms', []): |
176 |
self.render_fields(w, childform, values) |
|
177 |
||
178 |
def _render_hidden_fields(self, w, form): |
|
179 |
fields = form.fields[:] |
|
180 |
for field in form.fields: |
|
181 |
if not self.display_field(form, field): |
|
182 |
fields.remove(field) |
|
183 |
elif not field.is_visible(): |
|
184 |
w(field.render(form, self)) |
|
185 |
fields.remove(field) |
|
186 |
return fields |
|
1436 | 187 |
|
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1401
diff
changeset
|
188 |
def _render_fields(self, fields, w, form): |
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
|
189 |
w(u'<table class="%s">' % self.table_class) |
1147 | 190 |
for field in fields: |
191 |
w(u'<tr>') |
|
192 |
if self.display_label: |
|
193 |
w(u'<th class="labelCol">%s</th>' % self.render_label(form, field)) |
|
194 |
error = form.form_field_error(field) |
|
195 |
if error: |
|
1412
f3361e29d91f
give more space to labels in forms
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1403
diff
changeset
|
196 |
w(u'<td class="error">') |
1147 | 197 |
w(error) |
198 |
else: |
|
1412
f3361e29d91f
give more space to labels in forms
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1403
diff
changeset
|
199 |
w(u'<td>') |
1147 | 200 |
w(field.render(form, self)) |
201 |
if self.display_help: |
|
202 |
w(self.render_help(form, field)) |
|
203 |
w(u'</td></tr>') |
|
204 |
w(u'</table>') |
|
205 |
||
206 |
def render_buttons(self, w, form): |
|
207 |
w(u'<table class="%s">\n<tr>\n' % self.button_bar_class) |
|
1304 | 208 |
for button in form.form_buttons: |
209 |
w(u'<td>%s</td>\n' % button.render(form)) |
|
1147 | 210 |
w(u'</tr></table>') |
211 |
||
212 |
||
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
|
213 |
class BaseFormRenderer(FormRenderer): |
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
|
214 |
"""use form_renderer_id = 'base' if you want base FormRenderer without |
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
|
215 |
adaptation by selection |
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
|
216 |
""" |
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
|
217 |
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
|
218 |
|
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
|
219 |
|
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
220 |
class HTableFormRenderer(FormRenderer): |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
221 |
"""display fields horizontally in a table |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
222 |
|
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
223 |
+--------------+--------------+---------+ |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
224 |
| field1 label | field2 label | | |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
225 |
+--------------+--------------+---------+ |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
226 |
| field1 input | field2 input | buttons |
1436 | 227 |
+--------------+--------------+---------+ |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
228 |
""" |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
229 |
id = 'htable' |
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
230 |
|
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
231 |
display_help = False |
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1401
diff
changeset
|
232 |
def _render_fields(self, fields, w, form): |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
233 |
w(u'<table border="0">') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
234 |
w(u'<tr>') |
1436 | 235 |
for field in fields: |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
236 |
if self.display_label: |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
237 |
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
|
238 |
if self.display_help: |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
239 |
w(self.render_help(form, field)) |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
240 |
# empty slot for buttons |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
241 |
w(u'<th class="labelCol"> </th>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
242 |
w(u'</tr>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
243 |
w(u'<tr>') |
1436 | 244 |
for field in fields: |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
245 |
error = form.form_field_error(field) |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
246 |
if error: |
1757 | 247 |
w(u'<td class="error">') |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
248 |
w(error) |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
249 |
else: |
1757 | 250 |
w(u'<td>') |
1401
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
251 |
w(field.render(form, self)) |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
252 |
w(u'</td>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
253 |
w(u'<td>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
254 |
for button in form.form_buttons: |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
255 |
w(button.render(form)) |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
256 |
w(u'</td>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
257 |
w(u'</tr>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
258 |
w(u'</table>') |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
259 |
|
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
260 |
def render_buttons(self, w, form): |
56764b1b4887
new renderer displaying field in an horizontable table
sylvain.thenault@logilab.fr
parents:
1362
diff
changeset
|
261 |
pass |
1436 | 262 |
|
263 |
||
1147 | 264 |
class EntityCompositeFormRenderer(FormRenderer): |
265 |
"""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
|
266 |
id = 'composite' |
1436 | 267 |
|
1147 | 268 |
def render_fields(self, w, form, values): |
269 |
if not form.is_subform: |
|
270 |
w(u'<table class="listing">') |
|
271 |
super(EntityCompositeFormRenderer, self).render_fields(w, form, values) |
|
272 |
if not form.is_subform: |
|
273 |
w(u'</table>') |
|
1436 | 274 |
|
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1401
diff
changeset
|
275 |
def _render_fields(self, fields, w, form): |
1147 | 276 |
if form.is_subform: |
277 |
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
|
278 |
values = form.form_previous_values |
1147 | 279 |
qeid = eid_param('eid', entity.eid) |
280 |
cbsetstate = "setCheckboxesState2('eid', %s, 'checked')" % html_escape(dumps(entity.eid)) |
|
281 |
w(u'<tr class="%s">' % (entity.row % 2 and u'even' or u'odd')) |
|
282 |
# XXX turn this into a widget used on the eid field |
|
283 |
w(u'<td>%s</td>' % checkbox('eid', entity.eid, checked=qeid in values)) |
|
284 |
for field in fields: |
|
285 |
error = form.form_field_error(field) |
|
286 |
if error: |
|
287 |
w(u'<td class="error">') |
|
288 |
w(error) |
|
289 |
else: |
|
290 |
w(u'<td>') |
|
291 |
if isinstance(field.widget, (fwdgs.Select, fwdgs.CheckBox, fwdgs.Radio)): |
|
292 |
field.widget.attrs['onchange'] = cbsetstate |
|
293 |
elif isinstance(field.widget, fwdgs.Input): |
|
294 |
field.widget.attrs['onkeypress'] = cbsetstate |
|
295 |
w(u'<div>%s</div>' % field.render(form, self)) |
|
1691 | 296 |
w(u'</td>') |
1147 | 297 |
else: |
298 |
# main form, display table headers |
|
299 |
w(u'<tr class="header">') |
|
300 |
w(u'<th align="left">%s</th>' |
|
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
301 |
% tags.input(type='checkbox', title=self.req._('toggle check boxes'), |
1147 | 302 |
onclick="setCheckboxesState('eid', this.checked)")) |
303 |
for field in self.forms[0].fields: |
|
304 |
if self.display_field(form, field) and field.is_visible(): |
|
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
305 |
w(u'<th>%s</th>' % self.req._(field.label)) |
1147 | 306 |
w(u'</tr>') |
1436 | 307 |
|
308 |
||
1147 | 309 |
class EntityFormRenderer(FormRenderer): |
310 |
"""specific renderer for entity edition form (edition)""" |
|
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
311 |
__select__ = entity_implements('Any') & yes() |
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
312 |
|
1434
42e57dbbc585
new display_relations_form option on EntityFormRender
sylvain.thenault@logilab.fr
parents:
1412
diff
changeset
|
313 |
_options = FormRenderer._options + ('display_relations_form',) |
42e57dbbc585
new display_relations_form option on EntityFormRender
sylvain.thenault@logilab.fr
parents:
1412
diff
changeset
|
314 |
display_relations_form = True |
1436 | 315 |
|
1181
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
316 |
def render(self, form, values): |
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
317 |
rendered = super(EntityFormRenderer, self).render(form, values) |
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
318 |
return rendered + u'</div>' # close extra div introducted by open_form |
1436 | 319 |
|
1147 | 320 |
def open_form(self, form, values): |
321 |
attrs_fs_label = ('<div class="iformTitle"><span>%s</span></div>' |
|
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
322 |
% self.req._('main informations')) |
1147 | 323 |
attrs_fs_label += '<div class="formBody">' |
1181
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
324 |
return attrs_fs_label + super(EntityFormRenderer, self).open_form(form, values) |
1147 | 325 |
|
326 |
def render_fields(self, w, form, values): |
|
327 |
super(EntityFormRenderer, self).render_fields(w, form, values) |
|
328 |
self.inline_entities_form(w, form) |
|
1436 | 329 |
if form.edited_entity.has_eid() and self.display_relations_form: |
1147 | 330 |
self.relations_form(w, form) |
331 |
||
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1401
diff
changeset
|
332 |
def _render_fields(self, fields, w, form): |
1147 | 333 |
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
|
334 |
super(EntityFormRenderer, self)._render_fields(fields, w, form) |
1436 | 335 |
|
1147 | 336 |
def render_buttons(self, w, form): |
1304 | 337 |
if len(form.form_buttons) == 3: |
1147 | 338 |
w("""<table width="100%%"> |
339 |
<tbody> |
|
340 |
<tr><td align="center"> |
|
341 |
%s |
|
342 |
</td><td style="align: right; width: 50%%;"> |
|
343 |
%s |
|
344 |
%s |
|
345 |
</td></tr> |
|
346 |
</tbody> |
|
1304 | 347 |
</table>""" % tuple(button.render(form) for button in form.form_buttons)) |
1147 | 348 |
else: |
349 |
super(EntityFormRenderer, self).render_buttons(w, form) |
|
1436 | 350 |
|
1147 | 351 |
def relations_form(self, w, form): |
1683
c74566e61f1a
don't display relation in the metadata category here
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1628
diff
changeset
|
352 |
srels_by_cat = form.srelations_by_category('generic', 'add') |
1147 | 353 |
if not srels_by_cat: |
354 |
return u'' |
|
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
355 |
req = self.req |
1147 | 356 |
_ = req._ |
357 |
label = u'%s :' % _('This %s' % form.edited_entity.e_schema).capitalize() |
|
358 |
eid = form.edited_entity.eid |
|
359 |
w(u'<fieldset class="subentity">') |
|
360 |
w(u'<legend class="iformTitle">%s</legend>' % label) |
|
361 |
w(u'<table id="relatedEntities">') |
|
362 |
for rschema, target, related in form.relations_table(): |
|
363 |
# already linked entities |
|
364 |
if related: |
|
365 |
w(u'<tr><th class="labelCol">%s</th>' % rschema.display_name(req, target)) |
|
366 |
w(u'<td>') |
|
367 |
w(u'<ul>') |
|
368 |
for viewparams in related: |
|
369 |
w(u'<li class="invisible">%s<div id="span%s" class="%s">%s</div></li>' |
|
370 |
% (viewparams[1], viewparams[0], viewparams[2], viewparams[3])) |
|
371 |
if not form.force_display and form.maxrelitems < len(related): |
|
1436 | 372 |
link = (u'<span class="invisible">' |
1147 | 373 |
'[<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
|
374 |
'</span>' % self.req._('view all')) |
1147 | 375 |
w(u'<li class="invisible">%s</li>' % link) |
376 |
w(u'</ul>') |
|
377 |
w(u'</td>') |
|
378 |
w(u'</tr>') |
|
379 |
pendings = list(form.restore_pending_inserts()) |
|
380 |
if not pendings: |
|
381 |
w(u'<tr><th> </th><td> </td></tr>') |
|
382 |
else: |
|
383 |
for row in pendings: |
|
384 |
# soon to be linked to entities |
|
385 |
w(u'<tr id="tr%s">' % row[1]) |
|
386 |
w(u'<th>%s</th>' % row[3]) |
|
387 |
w(u'<td>') |
|
388 |
w(u'<a class="handle" title="%s" href="%s">[x]</a>' % |
|
389 |
(_('cancel this insert'), row[2])) |
|
390 |
w(u'<a id="a%s" class="editionPending" href="%s">%s</a>' |
|
391 |
% (row[1], row[4], html_escape(row[5]))) |
|
392 |
w(u'</td>') |
|
393 |
w(u'</tr>') |
|
394 |
w(u'<tr id="relationSelectorRow_%s" class="separator">' % eid) |
|
395 |
w(u'<th class="labelCol">') |
|
396 |
w(u'<span>%s</span>' % _('add relation')) |
|
1305
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
397 |
w(u'<select id="relationSelector_%s" tabindex="%s" ' |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
398 |
'onchange="javascript:showMatchingSelect(this.options[this.selectedIndex].value,%s);">' |
1147 | 399 |
% (eid, req.next_tabindex(), html_escape(dumps(eid)))) |
400 |
w(u'<option value="">%s</option>' % _('select a relation')) |
|
401 |
for i18nrtype, rschema, target in srels_by_cat: |
|
402 |
# more entities to link to |
|
403 |
w(u'<option value="%s_%s">%s</option>' % (rschema, target, i18nrtype)) |
|
404 |
w(u'</select>') |
|
405 |
w(u'</th>') |
|
406 |
w(u'<td id="unrelatedDivs_%s"></td>' % eid) |
|
407 |
w(u'</tr>') |
|
408 |
w(u'</table>') |
|
409 |
w(u'</fieldset>') |
|
1436 | 410 |
|
1147 | 411 |
def inline_entities_form(self, w, form): |
412 |
"""create a form to edit entity's inlined relations""" |
|
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
|
413 |
if not hasattr(form, 'inlined_relations'): |
28eed65ce1e6
don't crash if inlined_relation is not defined on the form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1964
diff
changeset
|
414 |
return |
1147 | 415 |
entity = form.edited_entity |
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
416 |
__ = self.req.__ |
1291
22b4d300d18d
inlineview is no more a 'category', needs specific handling
sylvain.thenault@logilab.fr
parents:
1274
diff
changeset
|
417 |
for rschema, targettypes, role in form.inlined_relations(): |
1147 | 418 |
# show inline forms only if there's one possible target type |
419 |
# for rschema |
|
420 |
if len(targettypes) != 1: |
|
421 |
self.warning('entity related by the %s relation should have ' |
|
422 |
'inlined form but there is multiple target types, ' |
|
423 |
'dunno what to do', rschema) |
|
424 |
continue |
|
425 |
targettype = targettypes[0].type |
|
426 |
if form.should_inline_relation_form(rschema, targettype, role): |
|
427 |
w(u'<div id="inline%sslot">' % rschema) |
|
428 |
existant = entity.has_eid() and entity.related(rschema) |
|
429 |
if existant: |
|
430 |
# display inline-edition view for all existing related entities |
|
1436 | 431 |
w(form.view('inline-edition', existant, rtype=rschema, role=role, |
1292
18a86192c9c4
inline_creation_form doesn't take ptype argument anymore
sylvain.thenault@logilab.fr
parents:
1291
diff
changeset
|
432 |
ptype=entity.e_schema, peid=entity.eid)) |
1147 | 433 |
if role == 'subject': |
434 |
card = rschema.rproperty(entity.e_schema, targettype, 'cardinality')[0] |
|
435 |
else: |
|
436 |
card = rschema.rproperty(targettype, entity.e_schema, 'cardinality')[1] |
|
437 |
# there is no related entity and we need at least one: we need to |
|
438 |
# display one explicit inline-creation view |
|
439 |
if form.should_display_inline_creation_form(rschema, existant, card): |
|
1319 | 440 |
w(form.view('inline-creation', None, etype=targettype, |
1147 | 441 |
peid=entity.eid, ptype=entity.e_schema, |
1319 | 442 |
rtype=rschema, role=role)) |
1147 | 443 |
# we can create more than one related entity, we thus display a link |
444 |
# to add new related entities |
|
445 |
if form.should_display_add_new_relation_link(rschema, existant, card): |
|
446 |
divid = "addNew%s%s%s:%s" % (targettype, rschema, role, entity.eid) |
|
447 |
w(u'<div class="inlinedform" id="%s" cubicweb:limit="true">' |
|
448 |
% divid) |
|
1292
18a86192c9c4
inline_creation_form doesn't take ptype argument anymore
sylvain.thenault@logilab.fr
parents:
1291
diff
changeset
|
449 |
js = "addInlineCreationForm('%s', '%s', '%s', '%s')" % ( |
18a86192c9c4
inline_creation_form doesn't take ptype argument anymore
sylvain.thenault@logilab.fr
parents:
1291
diff
changeset
|
450 |
entity.eid, targettype, rschema, role) |
1147 | 451 |
if card in '1?': |
452 |
js = "toggleVisibility('%s'); %s" % (divid, js) |
|
453 |
w(u'<a class="addEntity" id="add%s:%slink" href="javascript: %s" >+ %s.</a>' |
|
454 |
% (rschema, entity.eid, js, __('add a %s' % targettype))) |
|
455 |
w(u'</div>') |
|
456 |
w(u'<div class="trame_grise"> </div>') |
|
457 |
w(u'</div>') |
|
458 |
||
1436 | 459 |
|
1147 | 460 |
class EntityInlinedFormRenderer(EntityFormRenderer): |
461 |
"""specific renderer for entity inlined edition form |
|
462 |
(inline-[creation|edition]) |
|
463 |
""" |
|
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
464 |
id = 'inline' |
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
465 |
|
1147 | 466 |
def render(self, form, values): |
467 |
form.add_media() |
|
468 |
data = [] |
|
469 |
w = data.append |
|
470 |
try: |
|
471 |
w(u'<div id="div-%(divid)s" onclick="%(divonclick)s">' % values) |
|
472 |
except KeyError: |
|
473 |
w(u'<div id="div-%(divid)s">' % values) |
|
474 |
else: |
|
475 |
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
|
476 |
values['divid'], self.req._('click on the box to cancel the deletion'))) |
1147 | 477 |
w(u'<div class="iformBody">') |
2006
78d5b57d4964
we can now use self.req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
478 |
values['removemsg'] = self.req.__('remove this %s' % form.edited_entity.e_schema) |
1147 | 479 |
w(u'<div class="iformTitle"><span>%(title)s</span> ' |
480 |
'#<span class="icounter">1</span> ' |
|
481 |
'[<a href="javascript: %(removejs)s;noop();">%(removemsg)s</a>]</div>' |
|
482 |
% 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
|
483 |
# 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
|
484 |
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
|
485 |
values.pop(key) |
1147 | 486 |
self.render_fields(w, form, values) |
1293 | 487 |
w(u'</div></div>') |
1147 | 488 |
return '\n'.join(data) |
1436 | 489 |
|
1147 | 490 |
def render_fields(self, w, form, values): |
491 |
form.form_build_context(values) |
|
492 |
w(u'<fieldset id="fs-%(divid)s">' % values) |
|
493 |
fields = self._render_hidden_fields(w, form) |
|
494 |
w(u'</fieldset>') |
|
495 |
w(u'<fieldset class="subentity">') |
|
496 |
if fields: |
|
1403
425892e14e67
_render_fields don't need values argument
sylvain.thenault@logilab.fr
parents:
1401
diff
changeset
|
497 |
self._render_fields(fields, w, form) |
1147 | 498 |
self.render_child_forms(w, form, values) |
499 |
self.inline_entities_form(w, form) |
|
500 |
w(u'</fieldset>') |
|
1436 | 501 |