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