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