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