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