author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 22 Sep 2009 14:08:21 +0200 | |
branch | stable |
changeset 3370 | 62ed9981a770 |
parent 3263 | 5129918c671b |
child 3806 | 6a4191b0eb9a |
permissions | -rw-r--r-- |
0 | 1 |
"""Set of HTML automatic forms to create, delete, copy or edit a single entity |
2 |
or a list of entities of the same type |
|
3 |
||
4 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
: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:
1802
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
||
11 |
from copy import copy |
|
12 |
||
13 |
from simplejson import dumps |
|
14 |
||
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
15 |
from logilab.mtconverter import xml_escape |
0 | 16 |
from logilab.common.decorators import cached |
17 |
||
1132 | 18 |
from cubicweb.selectors import (specified_etype_implements, accepts_etype_compat, |
19 |
non_final_entity, match_kwargs, one_line_rset) |
|
833
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
824
diff
changeset
|
20 |
from cubicweb.view import View, EntityView |
1132 | 21 |
from cubicweb.web import INTERNAL_FIELD_VALUE, eid_param |
0 | 22 |
from cubicweb.web.controller import NAV_FORM_PARAMETERS |
23 |
from cubicweb.web.widgets import checkbox, InputWidget, ComboBoxWidget |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
1088
diff
changeset
|
24 |
from cubicweb.web.form import FormMixIn |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1318
diff
changeset
|
25 |
from cubicweb.web.views.autoform import AutomaticEntityForm |
0 | 26 |
|
27 |
_ = unicode |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
28 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
29 |
|
765
8fda14081686
kill Form, EntityForm and AnyRsetForm
sylvain.thenault@logilab.fr
parents:
742
diff
changeset
|
30 |
class EditionForm(FormMixIn, EntityView): |
0 | 31 |
"""primary entity edition form |
32 |
||
33 |
When generating a new attribute_input, the editor will look for a method |
|
34 |
named 'default_ATTRNAME' on the entity instance, where ATTRNAME is the |
|
35 |
name of the attribute being edited. You may use this feature to compute |
|
36 |
dynamic default values such as the 'tomorrow' date or the user's login |
|
37 |
being connected |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
38 |
""" |
772
97b2b582e7f7
skip_relations is defined on FormMixIn
sylvain.thenault@logilab.fr
parents:
765
diff
changeset
|
39 |
id = 'edition' |
781
323656dd85a9
fix import, use non_final_entity instead of implements('Any')
sylvain.thenault@logilab.fr
parents:
772
diff
changeset
|
40 |
__select__ = one_line_rset() & non_final_entity() |
0 | 41 |
|
42 |
title = _('edition') |
|
43 |
controller = 'edit' |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1290
diff
changeset
|
44 |
skip_relations = set() |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
45 |
|
0 | 46 |
EDITION_BODY = u'''\ |
47 |
%(errormsg)s |
|
48 |
<form id="%(formid)s" class="entityForm" cubicweb:target="eformframe" |
|
49 |
method="post" onsubmit="%(onsubmit)s" enctype="%(enctype)s" action="%(action)s"> |
|
50 |
%(title)s |
|
51 |
<div id="progress">%(inprogress)s</div> |
|
52 |
<div class="iformTitle"><span>%(mainattrs_label)s</span></div> |
|
53 |
<div class="formBody"><fieldset> |
|
54 |
%(base)s |
|
55 |
%(attrform)s |
|
56 |
%(relattrform)s |
|
57 |
</fieldset> |
|
58 |
%(relform)s |
|
59 |
</div> |
|
60 |
<table width="100%%"> |
|
61 |
<tbody> |
|
62 |
<tr><td align="center"> |
|
63 |
%(validate)s |
|
64 |
</td><td style="align: right; width: 50%%;"> |
|
65 |
%(apply)s |
|
66 |
%(cancel)s |
|
67 |
</td></tr> |
|
68 |
</tbody> |
|
69 |
</table> |
|
70 |
</form> |
|
71 |
''' |
|
72 |
||
73 |
def cell_call(self, row, col, **kwargs): |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
1088
diff
changeset
|
74 |
self.req.add_js( ('cubicweb.ajax.js', ) ) |
0 | 75 |
entity = self.complete_entity(row, col) |
76 |
self.edit_form(entity, kwargs) |
|
77 |
||
78 |
def edit_form(self, entity, kwargs): |
|
79 |
varmaker = self.req.get_page_data('rql_varmaker') |
|
80 |
if varmaker is None: |
|
81 |
varmaker = self.req.varmaker |
|
82 |
self.req.set_page_data('rql_varmaker', varmaker) |
|
83 |
self.varmaker = varmaker |
|
84 |
self.w(self.EDITION_BODY % self.form_context(entity, kwargs)) |
|
85 |
||
86 |
def form_context(self, entity, kwargs): |
|
87 |
"""returns the dictionnary used to fill the EDITION_BODY template |
|
88 |
||
89 |
If you create your own edition form, you can probably just override |
|
90 |
`EDITION_BODY` and `form_context` |
|
91 |
""" |
|
92 |
if self.need_multipart(entity): |
|
93 |
enctype = 'multipart/form-data' |
|
94 |
else: |
|
95 |
enctype = 'application/x-www-form-urlencoded' |
|
96 |
self._hiddens = [] |
|
97 |
if entity.eid is None: |
|
98 |
entity.eid = self.varmaker.next() |
|
99 |
# XXX (hack) action_title might need __linkto req's original value |
|
100 |
# and widgets such as DynamicComboWidget might change it |
|
101 |
# so we need to compute title before calling atttributes_form |
|
102 |
formtitle = self.action_title(entity) |
|
103 |
# be sure to call .*_form first so tabindexes are correct and inlined |
|
104 |
# fields errors are consumed |
|
105 |
if not entity.has_eid() or entity.has_perm('update'): |
|
106 |
attrform = self.attributes_form(entity, kwargs) |
|
107 |
else: |
|
108 |
attrform = '' |
|
109 |
inlineform = self.inline_entities_form(entity, kwargs) |
|
110 |
relform = self.relations_form(entity, kwargs) |
|
111 |
vindex = self.req.next_tabindex() |
|
112 |
aindex = self.req.next_tabindex() |
|
113 |
cindex = self.req.next_tabindex() |
|
114 |
self.add_hidden_web_behaviour_params(entity) |
|
115 |
_ = self.req._ |
|
116 |
return { |
|
117 |
'formid' : self.domid, |
|
118 |
'onsubmit' : self.on_submit(entity), |
|
119 |
'enctype' : enctype, |
|
120 |
'errormsg' : self.error_message(), |
|
121 |
'action' : self.build_url('validateform'), |
|
122 |
'eids' : entity.has_eid() and [entity.eid] or [], |
|
123 |
'inprogress': _('validating...'), |
|
124 |
'title' : formtitle, |
|
125 |
'mainattrs_label' : _('main informations'), |
|
126 |
'reseturl' : self.redirect_url(entity), |
|
127 |
'attrform' : attrform, |
|
128 |
'relform' : relform, |
|
129 |
'relattrform': inlineform, |
|
130 |
'base' : self.base_form(entity, kwargs), |
|
131 |
'validate' : self.button_ok(tabindex=vindex), |
|
132 |
'apply' : self.button_apply(tabindex=aindex), |
|
133 |
'cancel' : self.button_cancel(tabindex=cindex), |
|
134 |
} |
|
135 |
||
136 |
@property |
|
137 |
def formid(self): |
|
138 |
return self.id |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
139 |
|
0 | 140 |
def action_title(self, entity): |
141 |
"""form's title""" |
|
142 |
ptitle = self.req._(self.title) |
|
143 |
return u'<div class="formTitle"><span>%s %s</span></div>' % ( |
|
144 |
entity.dc_type(), ptitle and '(%s)' % ptitle) |
|
145 |
||
146 |
||
147 |
def base_form(self, entity, kwargs): |
|
148 |
output = [] |
|
149 |
for name, value, iid in self._hiddens: |
|
150 |
if isinstance(value, basestring): |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
151 |
value = xml_escape(value) |
0 | 152 |
if iid: |
153 |
output.append(u'<input id="%s" type="hidden" name="%s" value="%s" />' |
|
154 |
% (iid, name, value)) |
|
155 |
else: |
|
156 |
output.append(u'<input type="hidden" name="%s" value="%s" />' |
|
157 |
% (name, value)) |
|
158 |
return u'\n'.join(output) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
159 |
|
0 | 160 |
def add_hidden_web_behaviour_params(self, entity): |
161 |
"""inserts hidden params controlling how errors and redirection |
|
162 |
should be handled |
|
163 |
""" |
|
164 |
req = self.req |
|
165 |
self._hiddens.append( (u'__maineid', entity.eid, u'') ) |
|
166 |
self._hiddens.append( (u'__errorurl', req.url(), u'errorurl') ) |
|
167 |
self._hiddens.append( (u'__form_id', self.formid, u'') ) |
|
168 |
for param in NAV_FORM_PARAMETERS: |
|
169 |
value = req.form.get(param) |
|
170 |
if value: |
|
171 |
self._hiddens.append( (param, value, u'') ) |
|
172 |
msg = self.submited_message() |
|
173 |
# If we need to directly attach the new object to another one |
|
174 |
for linkto in req.list_form_param('__linkto'): |
|
175 |
self._hiddens.append( ('__linkto', linkto, '') ) |
|
176 |
msg = '%s %s' % (msg, self.req._('and linked')) |
|
177 |
self._hiddens.append( ('__message', msg, '') ) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
178 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
179 |
|
0 | 180 |
def attributes_form(self, entity, kwargs, include_eid=True): |
181 |
"""create a form to edit entity's attributes""" |
|
182 |
html = [] |
|
183 |
w = html.append |
|
184 |
eid = entity.eid |
|
185 |
wdg = entity.get_widget |
|
186 |
lines = (wdg(rschema, x) for rschema, x in self.editable_attributes(entity)) |
|
187 |
if include_eid: |
|
188 |
self._hiddens.append( ('eid', entity.eid, '') ) |
|
189 |
self._hiddens.append( (eid_param('__type', eid), entity.e_schema, '') ) |
|
190 |
w(u'<table id="%s" class="%s" style="width:100%%;">' % |
|
191 |
(kwargs.get('tab_id', 'entityForm%s' % eid), |
|
192 |
kwargs.get('tab_class', 'attributeForm'))) |
|
193 |
for widget in lines: |
|
194 |
w(u'<tr>\n<th class="labelCol">%s</th>' % widget.render_label(entity)) |
|
195 |
error = widget.render_error(entity) |
|
196 |
if error: |
|
197 |
w(u'<td class="error" style="width:100%;">') |
|
198 |
else: |
|
199 |
w(u'<td style="width:100%;">') |
|
200 |
if error: |
|
201 |
w(error) |
|
202 |
w(widget.edit_render(entity)) |
|
203 |
w(widget.render_help(entity)) |
|
204 |
w(u'</td>\n</tr>') |
|
205 |
w(u'</table>') |
|
206 |
return u'\n'.join(html) |
|
207 |
||
208 |
def editable_attributes(self, entity): |
|
209 |
# XXX both (add, delete) |
|
210 |
return [(rschema, x) for rschema, _, x in entity.relations_by_category(('primary', 'secondary'), 'add') |
|
211 |
if rschema != 'eid'] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
212 |
|
0 | 213 |
def relations_form(self, entity, kwargs): |
578
86628c564144
[forms] fix #4240 (edition form should not show relations section if no relation is editable)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
214 |
srels_by_cat = entity.srelations_by_category(('generic', 'metadata'), 'add') |
579
77779cca6be6
[forms] simplify this test
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
578
diff
changeset
|
215 |
if not srels_by_cat: |
578
86628c564144
[forms] fix #4240 (edition form should not show relations section if no relation is editable)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
216 |
return u'' |
0 | 217 |
req = self.req |
218 |
_ = self.req._ |
|
3263
5129918c671b
i18n update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
219 |
__ = _ |
5129918c671b
i18n update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
220 |
label = u'%s :' % __('This %s' % entity.e_schema).capitalize() |
0 | 221 |
eid = entity.eid |
222 |
html = [] |
|
223 |
w = html.append |
|
224 |
w(u'<fieldset class="subentity">') |
|
225 |
w(u'<legend class="iformTitle">%s</legend>' % label) |
|
226 |
w(u'<table id="relatedEntities">') |
|
579
77779cca6be6
[forms] simplify this test
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
578
diff
changeset
|
227 |
for row in self.relations_table(entity): |
578
86628c564144
[forms] fix #4240 (edition form should not show relations section if no relation is editable)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
228 |
# already linked entities |
0 | 229 |
if row[2]: |
230 |
w(u'<tr><th class="labelCol">%s</th>' % row[0].display_name(req, row[1])) |
|
231 |
w(u'<td>') |
|
232 |
w(u'<ul>') |
|
233 |
for viewparams in row[2]: |
|
234 |
w(u'<li class="invisible">%s<div id="span%s" class="%s">%s</div></li>' |
|
235 |
% (viewparams[1], viewparams[0], viewparams[2], viewparams[3])) |
|
236 |
if not self.force_display and self.maxrelitems < len(row[2]): |
|
237 |
w(u'<li class="invisible">%s</li>' % self.force_display_link()) |
|
238 |
w(u'</ul>') |
|
239 |
w(u'</td>') |
|
240 |
w(u'</tr>') |
|
579
77779cca6be6
[forms] simplify this test
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
578
diff
changeset
|
241 |
pendings = list(self.restore_pending_inserts(entity)) |
0 | 242 |
if not pendings: |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2312
diff
changeset
|
243 |
w(u'<tr><th> </th><td> </td></tr>') |
0 | 244 |
else: |
245 |
for row in pendings: |
|
578
86628c564144
[forms] fix #4240 (edition form should not show relations section if no relation is editable)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
246 |
# soon to be linked to entities |
0 | 247 |
w(u'<tr id="tr%s">' % row[1]) |
248 |
w(u'<th>%s</th>' % row[3]) |
|
249 |
w(u'<td>') |
|
250 |
w(u'<a class="handle" title="%s" href="%s">[x]</a>' % |
|
251 |
(_('cancel this insert'), row[2])) |
|
252 |
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:
1977
diff
changeset
|
253 |
% (row[1], row[4], xml_escape(row[5]))) |
0 | 254 |
w(u'</td>') |
255 |
w(u'</tr>') |
|
256 |
w(u'<tr id="relationSelectorRow_%s" class="separator">' % eid) |
|
257 |
w(u'<th class="labelCol">') |
|
258 |
w(u'<span>%s</span>' % _('add relation')) |
|
259 |
w(u'<select id="relationSelector_%s" tabindex="%s" onchange="javascript:showMatchingSelect(this.options[this.selectedIndex].value,%s);">' |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
260 |
% (eid, req.next_tabindex(), xml_escape(dumps(eid)))) |
0 | 261 |
w(u'<option value="">%s</option>' % _('select a relation')) |
578
86628c564144
[forms] fix #4240 (edition form should not show relations section if no relation is editable)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
262 |
for i18nrtype, rschema, target in srels_by_cat: |
86628c564144
[forms] fix #4240 (edition form should not show relations section if no relation is editable)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
263 |
# more entities to link to |
0 | 264 |
w(u'<option value="%s_%s">%s</option>' % (rschema, target, i18nrtype)) |
265 |
w(u'</select>') |
|
266 |
w(u'</th>') |
|
267 |
w(u'<td id="unrelatedDivs_%s"></td>' % eid) |
|
268 |
w(u'</tr>') |
|
269 |
w(u'</table>') |
|
270 |
w(u'</fieldset>') |
|
271 |
return '\n'.join(html) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
272 |
|
0 | 273 |
def inline_entities_form(self, entity, kwargs): |
274 |
"""create a form to edit entity's inlined relations""" |
|
275 |
result = [] |
|
276 |
_ = self.req._ |
|
277 |
for rschema, targettypes, x in entity.relations_by_category('inlineview', 'add'): |
|
278 |
# show inline forms only if there's one possible target type |
|
279 |
# for rschema |
|
280 |
if len(targettypes) != 1: |
|
281 |
self.warning('entity related by the %s relation should have ' |
|
282 |
'inlined form but there is multiple target types, ' |
|
283 |
'dunno what to do', rschema) |
|
284 |
continue |
|
285 |
targettype = targettypes[0].type |
|
286 |
if self.should_inline_relation_form(entity, rschema, targettype, x): |
|
287 |
result.append(u'<div id="inline%sslot">' % rschema) |
|
288 |
existant = entity.has_eid() and entity.related(rschema) |
|
240
6ba006fb95d1
don't give a bool instead of a result set to select a view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
289 |
if existant: |
6ba006fb95d1
don't give a bool instead of a result set to select a view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
290 |
# display inline-edition view for all existing related entities |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
291 |
result.append(self.view('inline-edition', existant, |
240
6ba006fb95d1
don't give a bool instead of a result set to select a view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
292 |
ptype=entity.e_schema, peid=entity.eid, |
6ba006fb95d1
don't give a bool instead of a result set to select a view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
293 |
rtype=rschema, role=x, **kwargs)) |
0 | 294 |
if x == 'subject': |
295 |
card = rschema.rproperty(entity.e_schema, targettype, 'cardinality')[0] |
|
296 |
else: |
|
297 |
card = rschema.rproperty(targettype, entity.e_schema, 'cardinality')[1] |
|
298 |
# there is no related entity and we need at least one : we need to |
|
299 |
# display one explicit inline-creation view |
|
300 |
if self.should_display_inline_relation_form(rschema, existant, card): |
|
301 |
result.append(self.view('inline-creation', None, etype=targettype, |
|
302 |
peid=entity.eid, ptype=entity.e_schema, |
|
303 |
rtype=rschema, role=x, **kwargs)) |
|
304 |
# we can create more than one related entity, we thus display a link |
|
305 |
# to add new related entities |
|
306 |
if self.should_display_add_inline_relation_link(rschema, existant, card): |
|
307 |
divid = "addNew%s%s%s:%s" % (targettype, rschema, x, entity.eid) |
|
308 |
result.append(u'<div class="inlinedform" id="%s" cubicweb:limit="true">' |
|
309 |
% divid) |
|
310 |
js = "addInlineCreationForm('%s', '%s', '%s', '%s', '%s')" % ( |
|
311 |
entity.eid, entity.e_schema, targettype, rschema, x) |
|
312 |
if card in '1?': |
|
313 |
js = "toggleVisibility('%s'); %s" % (divid, js) |
|
314 |
result.append(u'<a class="addEntity" id="add%s:%slink" href="javascript: %s" >+ %s.</a>' |
|
315 |
% (rschema, entity.eid, js, |
|
316 |
self.req.__('add a %s' % targettype))) |
|
317 |
result.append(u'</div>') |
|
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2312
diff
changeset
|
318 |
result.append(u'<div class="trame_grise"> </div>') |
0 | 319 |
result.append(u'</div>') |
320 |
return '\n'.join(result) |
|
321 |
||
322 |
# should_* method extracted to allow overriding |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
323 |
|
0 | 324 |
def should_inline_relation_form(self, entity, rschema, targettype, role): |
1540
996355824825
more rtags api change propagation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1498
diff
changeset
|
325 |
return AutomaticEntityForm.rinlined.etype_get(entity.id, rschema, role, |
996355824825
more rtags api change propagation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1498
diff
changeset
|
326 |
targettype) |
0 | 327 |
|
328 |
def should_display_inline_relation_form(self, rschema, existant, card): |
|
329 |
return not existant and card in '1+' |
|
330 |
||
331 |
def should_display_add_inline_relation_link(self, rschema, existant, card): |
|
332 |
return not existant or card in '+*' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
333 |
|
0 | 334 |
def reset_url(self, entity): |
335 |
return entity.absolute_url() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
336 |
|
0 | 337 |
def on_submit(self, entity): |
338 |
return u'return freezeFormButtons(\'%s\')' % (self.domid) |
|
339 |
||
340 |
def submited_message(self): |
|
341 |
return self.req._('element edited') |
|
342 |
||
343 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
344 |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
1088
diff
changeset
|
345 |
class CreationForm(EditionForm): |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
346 |
__select__ = specified_etype_implements('Any') |
833
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
824
diff
changeset
|
347 |
# XXX bw compat, use View.registered since we don't want accept_compat |
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
824
diff
changeset
|
348 |
# wrapper set in EntityView |
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
824
diff
changeset
|
349 |
registered = accepts_etype_compat(View.registered) |
0 | 350 |
id = 'creation' |
351 |
title = _('creation') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
352 |
|
0 | 353 |
def call(self, **kwargs): |
354 |
"""creation view for an entity""" |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
1088
diff
changeset
|
355 |
self.req.add_js( ('cubicweb.ajax.js',) ) |
1088
b7b58c3bb575
commit pending work (started in TLS): starting to implement Creation/Edition form with new form API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1085
diff
changeset
|
356 |
self.initialize_varmaker() |
0 | 357 |
etype = kwargs.pop('etype', self.req.form.get('etype')) |
358 |
try: |
|
359 |
entity = self.vreg.etype_class(etype)(self.req, None, None) |
|
360 |
except: |
|
361 |
self.w(self.req._('no such entity type %s') % etype) |
|
362 |
else: |
|
1088
b7b58c3bb575
commit pending work (started in TLS): starting to implement Creation/Edition form with new form API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1085
diff
changeset
|
363 |
entity.eid = self.varmaker.next() |
0 | 364 |
self.edit_form(entity, kwargs) |
365 |
||
366 |
def action_title(self, entity): |
|
367 |
"""custom form title if creating a entity with __linkto""" |
|
368 |
if '__linkto' in self.req.form: |
|
369 |
if isinstance(self.req.form['__linkto'], list): |
|
370 |
# XXX which one should be considered (case: add a ticket to a version in jpl) |
|
371 |
rtype, linkto_eid, role = self.req.form['__linkto'][0].split(':') |
|
372 |
else: |
|
373 |
rtype, linkto_eid, role = self.req.form['__linkto'].split(':') |
|
374 |
linkto_rset = self.req.eid_rset(linkto_eid) |
|
375 |
linkto_type = linkto_rset.description[0][0] |
|
376 |
if role == 'subject': |
|
377 |
title = self.req.__('creating %s (%s %s %s %%(linkto)s)' % ( |
|
378 |
entity.e_schema, entity.e_schema, rtype, linkto_type)) |
|
379 |
else: |
|
380 |
title = self.req.__('creating %s (%s %%(linkto)s %s %s)' % ( |
|
381 |
entity.e_schema, linkto_type, rtype, entity.e_schema)) |
|
382 |
msg = title % {'linkto' : self.view('incontext', linkto_rset)} |
|
383 |
return u'<div class="formTitle notransform"><span>%s</span></div>' % msg |
|
384 |
else: |
|
385 |
return super(CreationForm, self).action_title(entity) |
|
386 |
||
387 |
@property |
|
388 |
def formid(self): |
|
389 |
return 'edition' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
390 |
|
0 | 391 |
def relations_form(self, entity, kwargs): |
392 |
return u'' |
|
393 |
||
394 |
def reset_url(self, entity=None): |
|
395 |
return self.build_url(self.req.form.get('etype', '').lower()) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
396 |
|
0 | 397 |
def submited_message(self): |
398 |
return self.req._('element created') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
399 |
|
0 | 400 |
def url(self): |
401 |
"""return the url associated with this view""" |
|
402 |
return self.create_url(self.req.form.get('etype')) |
|
403 |
||
404 |
||
405 |
class InlineFormMixIn(object): |
|
406 |
||
407 |
@cached |
|
408 |
def card(self, etype): |
|
409 |
return self.rschema.rproperty(self.parent_schema, etype, 'cardinality')[0] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
410 |
|
0 | 411 |
def action_title(self, entity): |
412 |
return self.rschema.display_name(self.req, self.role) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
413 |
|
0 | 414 |
def add_hidden_web_behaviour_params(self, entity): |
415 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
416 |
|
0 | 417 |
def edit_form(self, entity, ptype, peid, rtype, |
418 |
role='subject', **kwargs): |
|
419 |
self.rschema = self.schema.rschema(rtype) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
420 |
self.role = role |
0 | 421 |
self.parent_schema = self.schema.eschema(ptype) |
422 |
self.parent_eid = peid |
|
423 |
super(InlineFormMixIn, self).edit_form(entity, kwargs) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
424 |
|
0 | 425 |
def should_inline_relation_form(self, entity, rschema, targettype, role): |
426 |
if rschema == self.rschema: |
|
427 |
return False |
|
1540
996355824825
more rtags api change propagation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1498
diff
changeset
|
428 |
return AutomaticEntityForm.rinlined.etype_get(entity.id, rschema, role, |
996355824825
more rtags api change propagation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1498
diff
changeset
|
429 |
targettype) |
0 | 430 |
|
431 |
@cached |
|
432 |
def keep_entity(self, entity): |
|
433 |
req = self.req |
|
434 |
# are we regenerating form because of a validation error ? |
|
435 |
erroneous_post = req.data.get('formvalues') |
|
436 |
if erroneous_post: |
|
437 |
cdvalues = req.list_form_param('%s:%s' % (self.rschema, |
|
438 |
self.parent_eid), |
|
439 |
erroneous_post) |
|
440 |
if unicode(entity.eid) not in cdvalues: |
|
441 |
return False |
|
442 |
return True |
|
443 |
||
444 |
def form_context(self, entity, kwargs): |
|
445 |
ctx = super(InlineFormMixIn, self).form_context(entity, kwargs) |
|
446 |
_ = self.req._ |
|
447 |
local_ctx = {'createmsg' : self.req.__('add a %s' % entity.e_schema), |
|
448 |
'so': self.role[0], # 's' for subject, 'o' for object |
|
449 |
'eid' : entity.eid, |
|
450 |
'rtype' : self.rschema, |
|
451 |
'parenteid' : self.parent_eid, |
|
452 |
'parenttype' : self.parent_schema, |
|
453 |
'etype' : entity.e_schema, |
|
454 |
'novalue' : INTERNAL_FIELD_VALUE, |
|
455 |
'removemsg' : self.req.__('remove this %s' % entity.e_schema), |
|
456 |
'notice' : self.req._('click on the box to cancel the deletion'), |
|
457 |
} |
|
458 |
ctx.update(local_ctx) |
|
459 |
return ctx |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
460 |
|
0 | 461 |
|
462 |
class CopyEditionForm(EditionForm): |
|
463 |
id = 'copy' |
|
464 |
title = _('copy edition') |
|
465 |
||
466 |
def cell_call(self, row, col, **kwargs): |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
1088
diff
changeset
|
467 |
self.req.add_js(('cubicweb.ajax.js',)) |
0 | 468 |
entity = self.complete_entity(row, col, skip_bytes=True) |
469 |
# make a copy of entity to avoid altering the entity in the |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
470 |
# request's cache. |
0 | 471 |
self.newentity = copy(entity) |
472 |
self.copying = self.newentity.eid |
|
473 |
self.newentity.eid = None |
|
474 |
self.edit_form(self.newentity, kwargs) |
|
475 |
del self.newentity |
|
476 |
||
477 |
def action_title(self, entity): |
|
478 |
"""form's title""" |
|
479 |
msg = super(CopyEditionForm, self).action_title(entity) |
|
480 |
return msg + (u'<script type="text/javascript">updateMessage("%s");</script>\n' |
|
481 |
% self.req._('Please note that this is only a shallow copy')) |
|
482 |
# XXX above message should have style of a warning |
|
483 |
||
484 |
@property |
|
485 |
def formid(self): |
|
486 |
return 'edition' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
487 |
|
0 | 488 |
def relations_form(self, entity, kwargs): |
489 |
return u'' |
|
490 |
||
491 |
def reset_url(self, entity): |
|
492 |
return self.build_url('view', rql='Any X WHERE X eid %s' % self.copying) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
493 |
|
0 | 494 |
def attributes_form(self, entity, kwargs, include_eid=True): |
495 |
# we don't want __clone_eid on inlined edited entities |
|
496 |
if entity.eid == self.newentity.eid: |
|
497 |
self._hiddens.append((eid_param('__cloned_eid', entity.eid), self.copying, '')) |
|
498 |
return EditionForm.attributes_form(self, entity, kwargs, include_eid) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
499 |
|
0 | 500 |
def submited_message(self): |
501 |
return self.req._('element copied') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
502 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
503 |
|
765
8fda14081686
kill Form, EntityForm and AnyRsetForm
sylvain.thenault@logilab.fr
parents:
742
diff
changeset
|
504 |
class TableEditForm(FormMixIn, EntityView): |
0 | 505 |
id = 'muledit' |
506 |
title = _('multiple edit') |
|
507 |
||
508 |
EDITION_BODY = u'''<form method="post" id="entityForm" onsubmit="return validateForm('entityForm', null);" action="%(action)s"> |
|
509 |
%(error)s |
|
510 |
<div id="progress">%(progress)s</div> |
|
511 |
<fieldset> |
|
512 |
<input type="hidden" name="__errorurl" value="%(url)s" /> |
|
513 |
<input type="hidden" name="__form_id" value="%(formid)s" /> |
|
514 |
<input type="hidden" name="__redirectvid" value="%(redirectvid)s" /> |
|
515 |
<input type="hidden" name="__redirectrql" value="%(redirectrql)s" /> |
|
516 |
<table class="listing"> |
|
517 |
<tr class="header"> |
|
518 |
<th align="left"><input type="checkbox" onclick="setCheckboxesState('eid', this.checked)" value="" title="toggle check boxes" /></th> |
|
519 |
%(attrheaders)s |
|
520 |
</tr> |
|
521 |
%(lines)s |
|
522 |
</table> |
|
523 |
<table width="100%%"> |
|
524 |
<tr> |
|
525 |
<td align="left"> |
|
526 |
<input class="validateButton" type="submit" value="%(okvalue)s" title="%(oktitle)s" /> |
|
527 |
<input class="validateButton" type="reset" name="__action_cancel" value="%(cancelvalue)s" title="%(canceltitle)s" /> |
|
528 |
</td> |
|
529 |
</tr> |
|
530 |
</table> |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
531 |
</fieldset> |
0 | 532 |
</form> |
533 |
''' |
|
534 |
||
535 |
WIDGET_CELL = u'''\ |
|
536 |
<td%(csscls)s> |
|
537 |
%(error)s |
|
538 |
<div>%(widget)s</div> |
|
539 |
</td>''' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
540 |
|
0 | 541 |
def call(self, **kwargs): |
542 |
"""a view to edit multiple entities of the same type |
|
543 |
the first column should be the eid |
|
544 |
""" |
|
545 |
req = self.req |
|
546 |
form = req.form |
|
547 |
_ = req._ |
|
548 |
sampleentity = self.complete_entity(0) |
|
549 |
attrheaders = [u'<th>%s</th>' % rdef[0].display_name(req, rdef[-1]) |
|
550 |
for rdef in sampleentity.relations_by_category('primary', 'add') |
|
551 |
if rdef[0].type != 'eid'] |
|
552 |
ctx = {'action' : self.build_url('edit'), |
|
553 |
'error': self.error_message(), |
|
554 |
'progress': _('validating...'), |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
555 |
'url': xml_escape(req.url()), |
0 | 556 |
'formid': self.id, |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
557 |
'redirectvid': xml_escape(form.get('__redirectvid', 'list')), |
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
558 |
'redirectrql': xml_escape(form.get('__redirectrql', self.rset.printable_rql())), |
0 | 559 |
'attrheaders': u'\n'.join(attrheaders), |
560 |
'lines': u'\n'.join(self.edit_form(ent) for ent in self.rset.entities()), |
|
561 |
'okvalue': _('button_ok').capitalize(), |
|
562 |
'oktitle': _('validate modifications on selected items').capitalize(), |
|
563 |
'cancelvalue': _('button_reset').capitalize(), |
|
564 |
'canceltitle': _('revert changes').capitalize(), |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
565 |
} |
0 | 566 |
self.w(self.EDITION_BODY % ctx) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
567 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
568 |
|
0 | 569 |
def reset_url(self, entity=None): |
570 |
self.build_url('view', rql=self.rset.printable_rql()) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1540
diff
changeset
|
571 |
|
0 | 572 |
def edit_form(self, entity): |
573 |
html = [] |
|
574 |
w = html.append |
|
575 |
entity.complete() |
|
576 |
eid = entity.eid |
|
577 |
values = self.req.data.get('formvalues', ()) |
|
578 |
qeid = eid_param('eid', eid) |
|
579 |
checked = qeid in values |
|
580 |
w(u'<tr class="%s">' % (entity.row % 2 and u'even' or u'odd')) |
|
581 |
w(u'<td>%s<input type="hidden" name="__type:%s" value="%s" /></td>' |
|
582 |
% (checkbox('eid', eid, checked=checked), eid, entity.e_schema)) |
|
583 |
# attribute relations (skip eid which is handled by the checkbox |
|
584 |
wdg = entity.get_widget |
|
585 |
wdgfactories = [wdg(rschema, x) for rschema, _, x in entity.relations_by_category('primary', 'add') |
|
586 |
if rschema.type != 'eid'] # XXX both (add, delete) |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
587 |
seid = xml_escape(dumps(eid)) |
0 | 588 |
for wobj in wdgfactories: |
589 |
if isinstance(wobj, ComboBoxWidget): |
|
590 |
wobj.attrs['onchange'] = "setCheckboxesState2('eid', %s, 'checked')" % seid |
|
591 |
elif isinstance(wobj, InputWidget): |
|
592 |
wobj.attrs['onkeypress'] = "setCheckboxesState2('eid', %s, 'checked')" % seid |
|
593 |
error = wobj.render_error(entity) |
|
594 |
if error: |
|
595 |
csscls = u' class="error"' |
|
596 |
else: |
|
597 |
csscls = u'' |
|
598 |
w(self.WIDGET_CELL % {'csscls': csscls, 'error': error, |
|
599 |
'widget': wobj.edit_render(entity)}) |
|
600 |
w(u'</tr>') |
|
601 |
return '\n'.join(html) |
|
602 |
||
603 |
||
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
781
diff
changeset
|
604 |
# XXX bw compat |
0 | 605 |
|
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
781
diff
changeset
|
606 |
from logilab.common.deprecation import class_moved |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
781
diff
changeset
|
607 |
from cubicweb.web.views import editviews |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
781
diff
changeset
|
608 |
ComboboxView = class_moved(editviews.ComboboxView) |