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