author | sylvain.thenault@logilab.fr |
Thu, 19 Feb 2009 18:22:55 +0100 | |
branch | tls-sprint |
changeset 845 | 6103d0f55a0b |
parent 833 | 8c6bfd9158fb |
child 849 | 8591d896db7e |
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 |
||
17 |
from cubicweb.interfaces import IWorkflowable |
|
781
323656dd85a9
fix import, use non_final_entity instead of implements('Any')
sylvain.thenault@logilab.fr
parents:
772
diff
changeset
|
18 |
from cubicweb.selectors import (specified_etype_implements, implements, |
323656dd85a9
fix import, use non_final_entity instead of implements('Any')
sylvain.thenault@logilab.fr
parents:
772
diff
changeset
|
19 |
match_kwargs, match_form_params, one_line_rset, |
833
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
824
diff
changeset
|
20 |
non_final_entity, accepts_etype_compat) |
765
8fda14081686
kill Form, EntityForm and AnyRsetForm
sylvain.thenault@logilab.fr
parents:
742
diff
changeset
|
21 |
from cubicweb.utils import make_uid |
833
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
824
diff
changeset
|
22 |
from cubicweb.view import View, EntityView |
0 | 23 |
from cubicweb.common.uilib import cut |
24 |
from cubicweb.web import INTERNAL_FIELD_VALUE, stdmsgs, eid_param |
|
25 |
from cubicweb.web.controller import NAV_FORM_PARAMETERS |
|
26 |
from cubicweb.web.widgets import checkbox, InputWidget, ComboBoxWidget |
|
765
8fda14081686
kill Form, EntityForm and AnyRsetForm
sylvain.thenault@logilab.fr
parents:
742
diff
changeset
|
27 |
from cubicweb.web.form import FormMixIn, relation_id |
0 | 28 |
|
29 |
_ = unicode |
|
30 |
||
765
8fda14081686
kill Form, EntityForm and AnyRsetForm
sylvain.thenault@logilab.fr
parents:
742
diff
changeset
|
31 |
class DeleteConfForm(FormMixIn, EntityView): |
0 | 32 |
id = 'deleteconf' |
33 |
title = _('delete') |
|
34 |
domid = 'deleteconf' |
|
35 |
onsubmit = None |
|
582
0260b3b71d71
don't use navigation in deleteconf form
sylvain.thenault@logilab.fr
parents:
579
diff
changeset
|
36 |
# don't use navigation, all entities asked to be deleted should be displayed |
0260b3b71d71
don't use navigation in deleteconf form
sylvain.thenault@logilab.fr
parents:
579
diff
changeset
|
37 |
# else we will only delete the displayed page |
0260b3b71d71
don't use navigation in deleteconf form
sylvain.thenault@logilab.fr
parents:
579
diff
changeset
|
38 |
need_navigation = False |
0 | 39 |
|
40 |
def call(self): |
|
41 |
"""ask for confirmation before real deletion""" |
|
42 |
_ = self.req._ |
|
43 |
self.req.add_js('cubicweb.edition.js') |
|
44 |
self.w(u'<script type="text/javascript">updateMessage(\'%s\');</script>\n' % _('this action is not reversible!')) |
|
45 |
# XXX above message should have style of a warning |
|
46 |
self.w(u'<h4>%s</h4>\n' % _('Do you want to delete the following element(s) ?')) |
|
47 |
if self.onsubmit: |
|
48 |
self.w(u'<form id="deleteconf" action="%s" onsubmit="%s" method="post">' |
|
49 |
% (self.build_url(), self.onsubmit)) |
|
50 |
else: |
|
51 |
self.w(u'<form id="deleteconf" action="%s" method="post">' |
|
52 |
% (self.build_url())) |
|
53 |
||
54 |
self.w(u'<fieldset>\n') |
|
55 |
self.display_rset() |
|
56 |
#self.w(u'<input type="hidden" name="rql" value="%s"/>' % self.req.form['rql']) |
|
57 |
self.w(u'<input type="hidden" name="__form_id" value="%s"/>' % self.id) |
|
58 |
self.w(self.button_delete(label=stdmsgs.YES)) |
|
59 |
self.w(self.button_cancel(label=stdmsgs.NO)) |
|
60 |
for param in NAV_FORM_PARAMETERS: |
|
61 |
value = self.req.form.get(param) |
|
62 |
if value: |
|
63 |
self.w(u'<input type="hidden" name="%s" value="%s"/>' % (param, value)) |
|
64 |
self.w(u'</fieldset></form>\n') |
|
65 |
||
66 |
def display_rset(self): |
|
67 |
self.w(u'<ul>\n') |
|
68 |
done = set() |
|
69 |
for i in xrange(self.rset.rowcount): |
|
70 |
if self.rset[i][0] in done: |
|
71 |
continue |
|
72 |
done.add(self.rset[i][0]) |
|
73 |
self.cell_call(i, 0) |
|
74 |
self.w(u'</ul>\n') |
|
75 |
||
76 |
def cell_call(self, row, col): |
|
77 |
entity = self.entity(row, col) |
|
78 |
self.w(u'<li>') |
|
79 |
self.w(u'<input type="hidden" name="eid" value="%s" />' % entity.eid) |
|
80 |
self.w(u'<input type="hidden" name="%s" value="%s"/>\n' |
|
81 |
% (eid_param('__type', entity.eid), self.rset.description[row][0])) |
|
82 |
self.w(u'<a href="%s">' % html_escape(entity.absolute_url())) |
|
83 |
# don't use outofcontext view or any other that may contain inline edition form |
|
84 |
self.w(html_escape(entity.view('textoutofcontext'))) |
|
85 |
self.w(u'</a>') |
|
86 |
self.w(u'</li>') |
|
87 |
||
88 |
||
845 | 89 |
class ChangeStateForm(EntityFieldsForm): |
90 |
state = TextField(widget=HiddenWidget) |
|
91 |
__method = TextField(widget=HiddenWidget, initial='set_state') |
|
92 |
trcomment = RichTextField(eidparam=True) |
|
93 |
||
94 |
def buttons(self): |
|
95 |
return [Button(label=stdmsgs.YES), |
|
96 |
Button(label=stdmsgs.NO)] |
|
97 |
||
98 |
class ChangeStateFormView(FormMixIn, EntityView): |
|
0 | 99 |
id = 'statuschange' |
100 |
title = _('status change') |
|
101 |
||
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
102 |
__select__ = implements(IWorkflowable) & match_form_params('treid') |
0 | 103 |
|
104 |
def cell_call(self, row, col, vid='secondary'): |
|
105 |
entity = self.entity(row, col) |
|
106 |
eid = entity.eid |
|
107 |
state = entity.in_state[0] |
|
108 |
transition = self.req.eid_rset(self.req.form['treid']).get_entity(0, 0) |
|
109 |
dest = transition.destination() |
|
110 |
self.req.add_js('cubicweb.edition.js') |
|
111 |
_ = self.req._ |
|
112 |
self.w(self.error_message()) |
|
113 |
self.w(u'<h4>%s %s</h4>\n' % (_(transition.name), entity.view('oneline'))) |
|
62
ef06f71533d9
use named substitutions in i18n strings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
114 |
msg = _('status will change from %(st1)s to %(st2)s') % { |
ef06f71533d9
use named substitutions in i18n strings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
115 |
'st1': _(state.name), |
ef06f71533d9
use named substitutions in i18n strings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
116 |
'st2': _(dest.name)} |
ef06f71533d9
use named substitutions in i18n strings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
117 |
self.w(u'<p>%s</p>\n' % msg) |
845 | 118 |
|
119 |
form = ChangeStateForm(redirect_path=self.redirectpath(entity)) # self.vreg.select_form('changestateform') |
|
120 |
self.w(form.render(req, entity, state=dest.eid)) |
|
121 |
||
0 | 122 |
|
845 | 123 |
# self.w(u'<form action="%s" onsubmit="return freezeFormButtons(\'entityForm\');" method="post" id="entityForm">\n' |
124 |
# % self.build_url('edit')) |
|
125 |
# self.w(u'<div id="progress">%s</div>' % _('validating...')) |
|
126 |
# self.w(u'<fieldset>\n') |
|
127 |
# #self.w(u'<input id="errorurl" type="hidden" name="__errorurl" value="%s"/>\n' |
|
128 |
# # % html_escape(self.req.url())) |
|
129 |
# self.w(u'<input type="hidden" name="__form_id" value="%s"/>\n' % self.id) |
|
130 |
# self.w(u'<input type="hidden" name="eid" value="%s" />' % eid) |
|
131 |
# self.w(u'<input type="hidden" name="%s" value="%s"/>\n' |
|
132 |
# % (eid_param('__type', eid), entity.e_schema)) |
|
133 |
# self.w(u'<input type="hidden" name="%s" value="%s"/>\n' |
|
134 |
# % (eid_param('state', eid), dest.eid)) |
|
135 |
# self.w(u'<input type="hidden" name="__redirectpath" value="%s"/>\n' |
|
136 |
# % html_escape(self.redirectpath(entity))) |
|
137 |
# self.fill_form(entity, state, dest) |
|
138 |
# self.w(u'<input type="hidden" name="__method" value="set_state"/>\n') |
|
139 |
# self.w(self.button_ok(label=stdmsgs.YES, tabindex=self.req.next_tabindex())) |
|
140 |
# self.w(self.button_cancel(label=stdmsgs.NO, tabindex=self.req.next_tabindex())) |
|
141 |
# self.w(u'</fieldset>\n') |
|
142 |
# self.w(u'</form>') |
|
143 |
||
144 |
# def fill_form(self, entity, state, dest): |
|
145 |
# # hack to use the widget for comment_format |
|
146 |
# trinfo = self.vreg.etype_class('TrInfo')(self.req, None) |
|
147 |
# # widget are cached, copy it since we want to modify its name attribute |
|
148 |
# wdg = trinfo.get_widget('comment_format') |
|
149 |
# wdg.name = 'trcommentformat' |
|
150 |
# # set a value in entity to avoid lookup for a non existant attribute... |
|
151 |
# trinfo['trcommentformat'] = u'' |
|
152 |
# # comment format/content have to be grouped using the original entity eid |
|
153 |
# wdg.rname = eid_param('trcommentformat', entity.eid) |
|
154 |
# self.w(wdg.render_label(trinfo)) |
|
155 |
# self.w(wdg._edit_render(trinfo)) |
|
156 |
# self.w(u'<br/>\n') |
|
157 |
# cformname = eid_param('trcomment', entity.eid) |
|
158 |
# self.w(u'<label for="%s">%s</label>\n' % (cformname, self.req._('comment:'))) |
|
159 |
# self.w(u'<textarea rows="10" cols="80" name="%s" tabindex="%s"></textarea><br/>\n' |
|
160 |
# % (cformname, self.req.next_tabindex())) |
|
0 | 161 |
|
186
5e550c6e554c
ChangeStateForm: extract method redirectpath() for easier subclassing
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
62
diff
changeset
|
162 |
def redirectpath(self, entity): |
5e550c6e554c
ChangeStateForm: extract method redirectpath() for easier subclassing
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
62
diff
changeset
|
163 |
return entity.rest_path() |
5e550c6e554c
ChangeStateForm: extract method redirectpath() for easier subclassing
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
62
diff
changeset
|
164 |
|
0 | 165 |
|
765
8fda14081686
kill Form, EntityForm and AnyRsetForm
sylvain.thenault@logilab.fr
parents:
742
diff
changeset
|
166 |
class ClickAndEditForm(FormMixIn, EntityView): |
0 | 167 |
id = 'reledit' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
168 |
__select__ = match_kwargs('rtype') |
0 | 169 |
|
170 |
#FIXME editableField class could be toggleable from userprefs |
|
171 |
||
172 |
EDITION_BODY = ''' |
|
173 |
<div class="editableField" id="%(divid)s" |
|
174 |
ondblclick="showInlineEditionForm(%(eid)s, '%(rtype)s', '%(divid)s')">%(value)s</div> |
|
175 |
<form style="display: none;" onsubmit="return inlineValidateForm('%(divid)s-form', '%(rtype)s', '%(eid)s', '%(divid)s', %(reload)s);" id="%(divid)s-form" action="#"> |
|
176 |
<fieldset> |
|
177 |
<input type="hidden" name="eid" value="%(eid)s" /> |
|
178 |
<input type="hidden" name="__maineid" value="%(eid)s" /> |
|
179 |
<input type="hidden" name="__type:%(eid)s" value="%(etype)s" /> |
|
180 |
%(attrform)s |
|
181 |
</fieldset> |
|
182 |
<div class="buttonbar"> |
|
183 |
%(ok)s |
|
184 |
%(cancel)s |
|
185 |
</div> |
|
186 |
</form> |
|
187 |
''' |
|
188 |
def cell_call(self, row, col, rtype=None, role='subject', reload=False): |
|
189 |
entity = self.entity(row, col) |
|
190 |
if getattr(entity, rtype) is None: |
|
191 |
value = self.req._('not specified') |
|
192 |
else: |
|
193 |
value = entity.printable_value(rtype) |
|
194 |
if not entity.has_perm('update'): |
|
195 |
self.w(value) |
|
196 |
return |
|
197 |
self.req.add_js( ('cubicweb.ajax.js', 'cubicweb.edition.js') ) |
|
198 |
eid = entity.eid |
|
199 |
edit_key = make_uid('%s-%s' % (rtype, eid)) |
|
200 |
divid = 'd%s' % edit_key |
|
201 |
widget = entity.get_widget(rtype, 'subject') |
|
202 |
eschema = entity.e_schema |
|
203 |
attrform = widget.edit_render(entity, useid='i%s' % edit_key) |
|
204 |
ok = (u'<input class="validateButton" type="submit" name="__action_apply" value="%s" tabindex="%s" />' |
|
205 |
% (self.req._(stdmsgs.BUTTON_OK), self.req.next_tabindex())) |
|
206 |
cancel = (u'<input class="validateButton" type="button" ' |
|
207 |
'value="%s" onclick="cancelInlineEdit(%s, \'%s\', \'%s\')" tabindex="%s" />' |
|
208 |
% (self.req._(stdmsgs.BUTTON_CANCEL), eid, rtype, divid, |
|
209 |
self.req.next_tabindex())) |
|
210 |
self.w(self.EDITION_BODY % { |
|
211 |
'eid': eid, |
|
212 |
'rtype': rtype, |
|
213 |
'etype': entity.e_schema, |
|
214 |
'attrform': attrform, |
|
215 |
'action' : self.build_url('edit'), # NOTE: actually never gets called |
|
216 |
'ok': ok, |
|
217 |
'cancel': cancel, |
|
218 |
'value': value, |
|
219 |
'reload': dumps(reload), |
|
220 |
'divid': divid, |
|
221 |
}) |
|
222 |
||
223 |
||
765
8fda14081686
kill Form, EntityForm and AnyRsetForm
sylvain.thenault@logilab.fr
parents:
742
diff
changeset
|
224 |
class EditionForm(FormMixIn, EntityView): |
0 | 225 |
"""primary entity edition form |
226 |
||
227 |
When generating a new attribute_input, the editor will look for a method |
|
228 |
named 'default_ATTRNAME' on the entity instance, where ATTRNAME is the |
|
229 |
name of the attribute being edited. You may use this feature to compute |
|
230 |
dynamic default values such as the 'tomorrow' date or the user's login |
|
231 |
being connected |
|
232 |
""" |
|
772
97b2b582e7f7
skip_relations is defined on FormMixIn
sylvain.thenault@logilab.fr
parents:
765
diff
changeset
|
233 |
id = 'edition' |
781
323656dd85a9
fix import, use non_final_entity instead of implements('Any')
sylvain.thenault@logilab.fr
parents:
772
diff
changeset
|
234 |
__select__ = one_line_rset() & non_final_entity() |
0 | 235 |
|
236 |
title = _('edition') |
|
237 |
controller = 'edit' |
|
772
97b2b582e7f7
skip_relations is defined on FormMixIn
sylvain.thenault@logilab.fr
parents:
765
diff
changeset
|
238 |
skip_relations = FormMixIn.skip_relations.copy() |
0 | 239 |
|
240 |
EDITION_BODY = u'''\ |
|
241 |
%(errormsg)s |
|
242 |
<form id="%(formid)s" class="entityForm" cubicweb:target="eformframe" |
|
243 |
method="post" onsubmit="%(onsubmit)s" enctype="%(enctype)s" action="%(action)s"> |
|
244 |
%(title)s |
|
245 |
<div id="progress">%(inprogress)s</div> |
|
246 |
<div class="iformTitle"><span>%(mainattrs_label)s</span></div> |
|
247 |
<div class="formBody"><fieldset> |
|
248 |
%(base)s |
|
249 |
%(attrform)s |
|
250 |
%(relattrform)s |
|
251 |
</fieldset> |
|
252 |
%(relform)s |
|
253 |
</div> |
|
254 |
<table width="100%%"> |
|
255 |
<tbody> |
|
256 |
<tr><td align="center"> |
|
257 |
%(validate)s |
|
258 |
</td><td style="align: right; width: 50%%;"> |
|
259 |
%(apply)s |
|
260 |
%(cancel)s |
|
261 |
</td></tr> |
|
262 |
</tbody> |
|
263 |
</table> |
|
264 |
</form> |
|
265 |
''' |
|
266 |
||
267 |
def cell_call(self, row, col, **kwargs): |
|
268 |
self.req.add_js( ('cubicweb.ajax.js', 'cubicweb.edition.js') ) |
|
269 |
self.req.add_css('cubicweb.form.css') |
|
270 |
entity = self.complete_entity(row, col) |
|
271 |
self.edit_form(entity, kwargs) |
|
272 |
||
273 |
def edit_form(self, entity, kwargs): |
|
274 |
varmaker = self.req.get_page_data('rql_varmaker') |
|
275 |
if varmaker is None: |
|
276 |
varmaker = self.req.varmaker |
|
277 |
self.req.set_page_data('rql_varmaker', varmaker) |
|
278 |
self.varmaker = varmaker |
|
279 |
self.w(self.EDITION_BODY % self.form_context(entity, kwargs)) |
|
280 |
||
281 |
def form_context(self, entity, kwargs): |
|
282 |
"""returns the dictionnary used to fill the EDITION_BODY template |
|
283 |
||
284 |
If you create your own edition form, you can probably just override |
|
285 |
`EDITION_BODY` and `form_context` |
|
286 |
""" |
|
287 |
if self.need_multipart(entity): |
|
288 |
enctype = 'multipart/form-data' |
|
289 |
else: |
|
290 |
enctype = 'application/x-www-form-urlencoded' |
|
291 |
self._hiddens = [] |
|
292 |
if entity.eid is None: |
|
293 |
entity.eid = self.varmaker.next() |
|
294 |
# XXX (hack) action_title might need __linkto req's original value |
|
295 |
# and widgets such as DynamicComboWidget might change it |
|
296 |
# so we need to compute title before calling atttributes_form |
|
297 |
formtitle = self.action_title(entity) |
|
298 |
# be sure to call .*_form first so tabindexes are correct and inlined |
|
299 |
# fields errors are consumed |
|
300 |
if not entity.has_eid() or entity.has_perm('update'): |
|
301 |
attrform = self.attributes_form(entity, kwargs) |
|
302 |
else: |
|
303 |
attrform = '' |
|
304 |
inlineform = self.inline_entities_form(entity, kwargs) |
|
305 |
relform = self.relations_form(entity, kwargs) |
|
306 |
vindex = self.req.next_tabindex() |
|
307 |
aindex = self.req.next_tabindex() |
|
308 |
cindex = self.req.next_tabindex() |
|
309 |
self.add_hidden_web_behaviour_params(entity) |
|
310 |
_ = self.req._ |
|
311 |
return { |
|
312 |
'formid' : self.domid, |
|
313 |
'onsubmit' : self.on_submit(entity), |
|
314 |
'enctype' : enctype, |
|
315 |
'errormsg' : self.error_message(), |
|
316 |
'action' : self.build_url('validateform'), |
|
317 |
'eids' : entity.has_eid() and [entity.eid] or [], |
|
318 |
'inprogress': _('validating...'), |
|
319 |
'title' : formtitle, |
|
320 |
'mainattrs_label' : _('main informations'), |
|
321 |
'reseturl' : self.redirect_url(entity), |
|
322 |
'attrform' : attrform, |
|
323 |
'relform' : relform, |
|
324 |
'relattrform': inlineform, |
|
325 |
'base' : self.base_form(entity, kwargs), |
|
326 |
'validate' : self.button_ok(tabindex=vindex), |
|
327 |
'apply' : self.button_apply(tabindex=aindex), |
|
328 |
'cancel' : self.button_cancel(tabindex=cindex), |
|
329 |
} |
|
330 |
||
331 |
@property |
|
332 |
def formid(self): |
|
333 |
return self.id |
|
334 |
||
335 |
def action_title(self, entity): |
|
336 |
"""form's title""" |
|
337 |
ptitle = self.req._(self.title) |
|
338 |
return u'<div class="formTitle"><span>%s %s</span></div>' % ( |
|
339 |
entity.dc_type(), ptitle and '(%s)' % ptitle) |
|
340 |
||
341 |
||
342 |
def base_form(self, entity, kwargs): |
|
343 |
output = [] |
|
344 |
for name, value, iid in self._hiddens: |
|
345 |
if isinstance(value, basestring): |
|
346 |
value = html_escape(value) |
|
347 |
if iid: |
|
348 |
output.append(u'<input id="%s" type="hidden" name="%s" value="%s" />' |
|
349 |
% (iid, name, value)) |
|
350 |
else: |
|
351 |
output.append(u'<input type="hidden" name="%s" value="%s" />' |
|
352 |
% (name, value)) |
|
353 |
return u'\n'.join(output) |
|
354 |
||
355 |
def add_hidden_web_behaviour_params(self, entity): |
|
356 |
"""inserts hidden params controlling how errors and redirection |
|
357 |
should be handled |
|
358 |
""" |
|
359 |
req = self.req |
|
360 |
self._hiddens.append( (u'__maineid', entity.eid, u'') ) |
|
361 |
self._hiddens.append( (u'__errorurl', req.url(), u'errorurl') ) |
|
362 |
self._hiddens.append( (u'__form_id', self.formid, u'') ) |
|
363 |
for param in NAV_FORM_PARAMETERS: |
|
364 |
value = req.form.get(param) |
|
365 |
if value: |
|
366 |
self._hiddens.append( (param, value, u'') ) |
|
367 |
msg = self.submited_message() |
|
368 |
# If we need to directly attach the new object to another one |
|
369 |
for linkto in req.list_form_param('__linkto'): |
|
370 |
self._hiddens.append( ('__linkto', linkto, '') ) |
|
371 |
msg = '%s %s' % (msg, self.req._('and linked')) |
|
372 |
self._hiddens.append( ('__message', msg, '') ) |
|
373 |
||
374 |
||
375 |
def attributes_form(self, entity, kwargs, include_eid=True): |
|
376 |
"""create a form to edit entity's attributes""" |
|
377 |
html = [] |
|
378 |
w = html.append |
|
379 |
eid = entity.eid |
|
380 |
wdg = entity.get_widget |
|
381 |
lines = (wdg(rschema, x) for rschema, x in self.editable_attributes(entity)) |
|
382 |
if include_eid: |
|
383 |
self._hiddens.append( ('eid', entity.eid, '') ) |
|
384 |
self._hiddens.append( (eid_param('__type', eid), entity.e_schema, '') ) |
|
385 |
w(u'<table id="%s" class="%s" style="width:100%%;">' % |
|
386 |
(kwargs.get('tab_id', 'entityForm%s' % eid), |
|
387 |
kwargs.get('tab_class', 'attributeForm'))) |
|
388 |
for widget in lines: |
|
389 |
w(u'<tr>\n<th class="labelCol">%s</th>' % widget.render_label(entity)) |
|
390 |
error = widget.render_error(entity) |
|
391 |
if error: |
|
392 |
w(u'<td class="error" style="width:100%;">') |
|
393 |
else: |
|
394 |
w(u'<td style="width:100%;">') |
|
395 |
if error: |
|
396 |
w(error) |
|
397 |
w(widget.edit_render(entity)) |
|
398 |
w(widget.render_help(entity)) |
|
399 |
w(u'</td>\n</tr>') |
|
400 |
w(u'</table>') |
|
401 |
return u'\n'.join(html) |
|
402 |
||
403 |
def editable_attributes(self, entity): |
|
404 |
# XXX both (add, delete) |
|
405 |
return [(rschema, x) for rschema, _, x in entity.relations_by_category(('primary', 'secondary'), 'add') |
|
406 |
if rschema != 'eid'] |
|
407 |
||
408 |
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
|
409 |
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
|
410 |
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
|
411 |
return u'' |
0 | 412 |
req = self.req |
413 |
_ = self.req._ |
|
414 |
label = u'%s :' % _('This %s' % entity.e_schema).capitalize() |
|
415 |
eid = entity.eid |
|
416 |
html = [] |
|
417 |
w = html.append |
|
418 |
w(u'<fieldset class="subentity">') |
|
419 |
w(u'<legend class="iformTitle">%s</legend>' % label) |
|
420 |
w(u'<table id="relatedEntities">') |
|
579
77779cca6be6
[forms] simplify this test
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
578
diff
changeset
|
421 |
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
|
422 |
# already linked entities |
0 | 423 |
if row[2]: |
424 |
w(u'<tr><th class="labelCol">%s</th>' % row[0].display_name(req, row[1])) |
|
425 |
w(u'<td>') |
|
426 |
w(u'<ul>') |
|
427 |
for viewparams in row[2]: |
|
428 |
w(u'<li class="invisible">%s<div id="span%s" class="%s">%s</div></li>' |
|
429 |
% (viewparams[1], viewparams[0], viewparams[2], viewparams[3])) |
|
430 |
if not self.force_display and self.maxrelitems < len(row[2]): |
|
431 |
w(u'<li class="invisible">%s</li>' % self.force_display_link()) |
|
432 |
w(u'</ul>') |
|
433 |
w(u'</td>') |
|
434 |
w(u'</tr>') |
|
579
77779cca6be6
[forms] simplify this test
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
578
diff
changeset
|
435 |
pendings = list(self.restore_pending_inserts(entity)) |
0 | 436 |
if not pendings: |
437 |
w(u'<tr><th> </th><td> </td></tr>') |
|
438 |
else: |
|
439 |
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
|
440 |
# soon to be linked to entities |
0 | 441 |
w(u'<tr id="tr%s">' % row[1]) |
442 |
w(u'<th>%s</th>' % row[3]) |
|
443 |
w(u'<td>') |
|
444 |
w(u'<a class="handle" title="%s" href="%s">[x]</a>' % |
|
445 |
(_('cancel this insert'), row[2])) |
|
446 |
w(u'<a id="a%s" class="editionPending" href="%s">%s</a>' |
|
447 |
% (row[1], row[4], html_escape(row[5]))) |
|
448 |
w(u'</td>') |
|
449 |
w(u'</tr>') |
|
450 |
w(u'<tr id="relationSelectorRow_%s" class="separator">' % eid) |
|
451 |
w(u'<th class="labelCol">') |
|
452 |
w(u'<span>%s</span>' % _('add relation')) |
|
453 |
w(u'<select id="relationSelector_%s" tabindex="%s" onchange="javascript:showMatchingSelect(this.options[this.selectedIndex].value,%s);">' |
|
454 |
% (eid, req.next_tabindex(), html_escape(dumps(eid)))) |
|
455 |
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
|
456 |
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
|
457 |
# more entities to link to |
0 | 458 |
w(u'<option value="%s_%s">%s</option>' % (rschema, target, i18nrtype)) |
459 |
w(u'</select>') |
|
460 |
w(u'</th>') |
|
461 |
w(u'<td id="unrelatedDivs_%s"></td>' % eid) |
|
462 |
w(u'</tr>') |
|
463 |
w(u'</table>') |
|
464 |
w(u'</fieldset>') |
|
465 |
return '\n'.join(html) |
|
466 |
||
467 |
def inline_entities_form(self, entity, kwargs): |
|
468 |
"""create a form to edit entity's inlined relations""" |
|
469 |
result = [] |
|
470 |
_ = self.req._ |
|
471 |
for rschema, targettypes, x in entity.relations_by_category('inlineview', 'add'): |
|
472 |
# show inline forms only if there's one possible target type |
|
473 |
# for rschema |
|
474 |
if len(targettypes) != 1: |
|
475 |
self.warning('entity related by the %s relation should have ' |
|
476 |
'inlined form but there is multiple target types, ' |
|
477 |
'dunno what to do', rschema) |
|
478 |
continue |
|
479 |
targettype = targettypes[0].type |
|
480 |
if self.should_inline_relation_form(entity, rschema, targettype, x): |
|
481 |
result.append(u'<div id="inline%sslot">' % rschema) |
|
482 |
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
|
483 |
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
|
484 |
# 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
|
485 |
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
|
486 |
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
|
487 |
rtype=rschema, role=x, **kwargs)) |
0 | 488 |
if x == 'subject': |
489 |
card = rschema.rproperty(entity.e_schema, targettype, 'cardinality')[0] |
|
490 |
else: |
|
491 |
card = rschema.rproperty(targettype, entity.e_schema, 'cardinality')[1] |
|
492 |
# there is no related entity and we need at least one : we need to |
|
493 |
# display one explicit inline-creation view |
|
494 |
if self.should_display_inline_relation_form(rschema, existant, card): |
|
495 |
result.append(self.view('inline-creation', None, etype=targettype, |
|
496 |
peid=entity.eid, ptype=entity.e_schema, |
|
497 |
rtype=rschema, role=x, **kwargs)) |
|
498 |
# we can create more than one related entity, we thus display a link |
|
499 |
# to add new related entities |
|
500 |
if self.should_display_add_inline_relation_link(rschema, existant, card): |
|
501 |
divid = "addNew%s%s%s:%s" % (targettype, rschema, x, entity.eid) |
|
502 |
result.append(u'<div class="inlinedform" id="%s" cubicweb:limit="true">' |
|
503 |
% divid) |
|
504 |
js = "addInlineCreationForm('%s', '%s', '%s', '%s', '%s')" % ( |
|
505 |
entity.eid, entity.e_schema, targettype, rschema, x) |
|
506 |
if card in '1?': |
|
507 |
js = "toggleVisibility('%s'); %s" % (divid, js) |
|
508 |
result.append(u'<a class="addEntity" id="add%s:%slink" href="javascript: %s" >+ %s.</a>' |
|
509 |
% (rschema, entity.eid, js, |
|
510 |
self.req.__('add a %s' % targettype))) |
|
511 |
result.append(u'</div>') |
|
512 |
result.append(u'<div class="trame_grise"> </div>') |
|
513 |
result.append(u'</div>') |
|
514 |
return '\n'.join(result) |
|
515 |
||
516 |
# should_* method extracted to allow overriding |
|
517 |
||
518 |
def should_inline_relation_form(self, entity, rschema, targettype, role): |
|
519 |
return entity.rtags.is_inlined(rschema, targettype, role) |
|
520 |
||
521 |
def should_display_inline_relation_form(self, rschema, existant, card): |
|
522 |
return not existant and card in '1+' |
|
523 |
||
524 |
def should_display_add_inline_relation_link(self, rschema, existant, card): |
|
525 |
return not existant or card in '+*' |
|
526 |
||
527 |
def reset_url(self, entity): |
|
528 |
return entity.absolute_url() |
|
529 |
||
530 |
def on_submit(self, entity): |
|
531 |
return u'return freezeFormButtons(\'%s\')' % (self.domid) |
|
532 |
||
533 |
||
534 |
def submited_message(self): |
|
535 |
return self.req._('element edited') |
|
536 |
||
537 |
||
538 |
||
539 |
class CreationForm(EditionForm): |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
540 |
__select__ = specified_etype_implements('Any') |
833
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
824
diff
changeset
|
541 |
# 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
|
542 |
# wrapper set in EntityView |
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
824
diff
changeset
|
543 |
registered = accepts_etype_compat(View.registered) |
0 | 544 |
id = 'creation' |
545 |
title = _('creation') |
|
546 |
||
547 |
def call(self, **kwargs): |
|
548 |
"""creation view for an entity""" |
|
549 |
self.req.add_js( ('cubicweb.ajax.js', 'cubicweb.edition.js') ) |
|
550 |
self.req.add_css('cubicweb.form.css') |
|
551 |
etype = kwargs.pop('etype', self.req.form.get('etype')) |
|
552 |
try: |
|
553 |
entity = self.vreg.etype_class(etype)(self.req, None, None) |
|
554 |
except: |
|
555 |
self.w(self.req._('no such entity type %s') % etype) |
|
556 |
else: |
|
557 |
self.edit_form(entity, kwargs) |
|
558 |
||
559 |
def action_title(self, entity): |
|
560 |
"""custom form title if creating a entity with __linkto""" |
|
561 |
if '__linkto' in self.req.form: |
|
562 |
if isinstance(self.req.form['__linkto'], list): |
|
563 |
# XXX which one should be considered (case: add a ticket to a version in jpl) |
|
564 |
rtype, linkto_eid, role = self.req.form['__linkto'][0].split(':') |
|
565 |
else: |
|
566 |
rtype, linkto_eid, role = self.req.form['__linkto'].split(':') |
|
567 |
linkto_rset = self.req.eid_rset(linkto_eid) |
|
568 |
linkto_type = linkto_rset.description[0][0] |
|
569 |
if role == 'subject': |
|
570 |
title = self.req.__('creating %s (%s %s %s %%(linkto)s)' % ( |
|
571 |
entity.e_schema, entity.e_schema, rtype, linkto_type)) |
|
572 |
else: |
|
573 |
title = self.req.__('creating %s (%s %%(linkto)s %s %s)' % ( |
|
574 |
entity.e_schema, linkto_type, rtype, entity.e_schema)) |
|
575 |
msg = title % {'linkto' : self.view('incontext', linkto_rset)} |
|
576 |
return u'<div class="formTitle notransform"><span>%s</span></div>' % msg |
|
577 |
else: |
|
578 |
return super(CreationForm, self).action_title(entity) |
|
579 |
||
580 |
@property |
|
581 |
def formid(self): |
|
582 |
return 'edition' |
|
583 |
||
584 |
def relations_form(self, entity, kwargs): |
|
585 |
return u'' |
|
586 |
||
587 |
def reset_url(self, entity=None): |
|
588 |
return self.build_url(self.req.form.get('etype', '').lower()) |
|
589 |
||
590 |
def submited_message(self): |
|
591 |
return self.req._('element created') |
|
592 |
||
593 |
def url(self): |
|
594 |
"""return the url associated with this view""" |
|
595 |
return self.create_url(self.req.form.get('etype')) |
|
596 |
||
597 |
||
598 |
class InlineFormMixIn(object): |
|
599 |
||
600 |
@cached |
|
601 |
def card(self, etype): |
|
602 |
return self.rschema.rproperty(self.parent_schema, etype, 'cardinality')[0] |
|
603 |
||
604 |
def action_title(self, entity): |
|
605 |
return self.rschema.display_name(self.req, self.role) |
|
606 |
||
607 |
def add_hidden_web_behaviour_params(self, entity): |
|
608 |
pass |
|
609 |
||
610 |
def edit_form(self, entity, ptype, peid, rtype, |
|
611 |
role='subject', **kwargs): |
|
612 |
self.rschema = self.schema.rschema(rtype) |
|
613 |
self.role = role |
|
614 |
self.parent_schema = self.schema.eschema(ptype) |
|
615 |
self.parent_eid = peid |
|
616 |
super(InlineFormMixIn, self).edit_form(entity, kwargs) |
|
617 |
||
618 |
def should_inline_relation_form(self, entity, rschema, targettype, role): |
|
619 |
if rschema == self.rschema: |
|
620 |
return False |
|
621 |
return entity.rtags.is_inlined(rschema, targettype, role) |
|
622 |
||
623 |
@cached |
|
624 |
def keep_entity(self, entity): |
|
625 |
req = self.req |
|
626 |
# are we regenerating form because of a validation error ? |
|
627 |
erroneous_post = req.data.get('formvalues') |
|
628 |
if erroneous_post: |
|
629 |
cdvalues = req.list_form_param('%s:%s' % (self.rschema, |
|
630 |
self.parent_eid), |
|
631 |
erroneous_post) |
|
632 |
if unicode(entity.eid) not in cdvalues: |
|
633 |
return False |
|
634 |
return True |
|
635 |
||
636 |
def form_context(self, entity, kwargs): |
|
637 |
ctx = super(InlineFormMixIn, self).form_context(entity, kwargs) |
|
638 |
_ = self.req._ |
|
639 |
local_ctx = {'createmsg' : self.req.__('add a %s' % entity.e_schema), |
|
640 |
'so': self.role[0], # 's' for subject, 'o' for object |
|
641 |
'eid' : entity.eid, |
|
642 |
'rtype' : self.rschema, |
|
643 |
'parenteid' : self.parent_eid, |
|
644 |
'parenttype' : self.parent_schema, |
|
645 |
'etype' : entity.e_schema, |
|
646 |
'novalue' : INTERNAL_FIELD_VALUE, |
|
647 |
'removemsg' : self.req.__('remove this %s' % entity.e_schema), |
|
648 |
'notice' : self.req._('click on the box to cancel the deletion'), |
|
649 |
} |
|
650 |
ctx.update(local_ctx) |
|
651 |
return ctx |
|
652 |
||
653 |
||
654 |
class InlineEntityCreationForm(InlineFormMixIn, CreationForm): |
|
655 |
id = 'inline-creation' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
656 |
__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
|
657 |
& specified_etype_implements('Any')) |
633 | 658 |
|
0 | 659 |
EDITION_BODY = u'''\ |
660 |
<div id="div-%(parenteid)s-%(rtype)s-%(eid)s" class="inlinedform"> |
|
661 |
<div class="iformBody"> |
|
662 |
<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> |
|
663 |
<fieldset class="subentity"> |
|
664 |
%(attrform)s |
|
665 |
%(relattrform)s |
|
666 |
</fieldset> |
|
667 |
</div> |
|
668 |
<fieldset class="hidden" id="fs-%(parenteid)s-%(rtype)s-%(eid)s"> |
|
669 |
%(base)s |
|
670 |
<input type="hidden" value="%(novalue)s" name="edit%(so)s-%(rtype)s:%(parenteid)s" /> |
|
671 |
<input id="rel-%(parenteid)s-%(rtype)s-%(eid)s" type="hidden" value="%(eid)s" name="%(rtype)s:%(parenteid)s" /> |
|
672 |
</fieldset> |
|
673 |
</div>''' # do not insert trailing space or \n here ! |
|
674 |
||
675 |
def call(self, etype, ptype, peid, rtype, role='subject', **kwargs): |
|
676 |
""" |
|
677 |
:param etype: the entity type being created in the inline form |
|
678 |
:param parent: the parent entity hosting the inline form |
|
679 |
:param rtype: the relation bridging `etype` and `parent` |
|
680 |
:param role: the role played by the `parent` in the relation |
|
681 |
""" |
|
682 |
self.req.add_css('cubicweb.form.css') |
|
683 |
try: |
|
684 |
entity = self.vreg.etype_class(etype)(self.req, None, None) |
|
685 |
except: |
|
686 |
self.w(self.req._('no such entity type %s') % etype) |
|
687 |
return |
|
688 |
self.edit_form(entity, ptype, peid, rtype, role, **kwargs) |
|
689 |
||
690 |
||
691 |
class InlineEntityEditionForm(InlineFormMixIn, EditionForm): |
|
692 |
id = 'inline-edition' |
|
781
323656dd85a9
fix import, use non_final_entity instead of implements('Any')
sylvain.thenault@logilab.fr
parents:
772
diff
changeset
|
693 |
__select__ = non_final_entity() & match_kwargs('ptype', 'peid', 'rtype') |
0 | 694 |
|
695 |
EDITION_BODY = u'''\ |
|
696 |
<div onclick="restoreInlinedEntity('%(parenteid)s', '%(rtype)s', '%(eid)s')" id="div-%(parenteid)s-%(rtype)s-%(eid)s" class="inlinedform"> |
|
697 |
<div id="notice-%(parenteid)s-%(rtype)s-%(eid)s" class="notice">%(notice)s</div> |
|
698 |
<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> |
|
699 |
<div class="iformBody"> |
|
700 |
<fieldset class="subentity"> |
|
701 |
%(attrform)s |
|
702 |
</fieldset> |
|
703 |
%(relattrform)s |
|
704 |
</div> |
|
705 |
<fieldset id="fs-%(parenteid)s-%(rtype)s-%(eid)s"> |
|
706 |
%(base)s |
|
707 |
<input type="hidden" value="%(eid)s" name="edit%(so)s-%(rtype)s:%(parenteid)s" /> |
|
708 |
%(rinput)s |
|
709 |
</fieldset> |
|
710 |
</div>''' # do not insert trailing space or \n here ! |
|
711 |
||
712 |
rel_input = u'''<input id="rel-%(parenteid)s-%(rtype)s-%(eid)s" type="hidden" value="%(eid)s" name="%(rtype)s:%(parenteid)s" />''' |
|
713 |
||
714 |
def call(self, **kwargs): |
|
715 |
"""redefine default View.call() method to avoid automatic |
|
716 |
insertions of <div class="section"> between each row of |
|
717 |
the resultset |
|
718 |
""" |
|
719 |
self.req.add_css('cubicweb.form.css') |
|
720 |
rset = self.rset |
|
721 |
for i in xrange(len(rset)): |
|
722 |
self.wview(self.id, rset, row=i, **kwargs) |
|
723 |
||
724 |
def cell_call(self, row, col, ptype, peid, rtype, role='subject', **kwargs): |
|
725 |
""" |
|
726 |
:param parent: the parent entity hosting the inline form |
|
727 |
:param rtype: the relation bridging `etype` and `parent` |
|
728 |
:param role: the role played by the `parent` in the relation |
|
729 |
""" |
|
730 |
entity = self.entity(row, col) |
|
731 |
self.edit_form(entity, ptype, peid, rtype, role, **kwargs) |
|
732 |
||
733 |
||
734 |
def form_context(self, entity, kwargs): |
|
735 |
ctx = super(InlineEntityEditionForm, self).form_context(entity, kwargs) |
|
736 |
if self.keep_entity(entity): |
|
737 |
ctx['rinput'] = self.rel_input % ctx |
|
738 |
ctx['todelete'] = u'' |
|
739 |
else: |
|
740 |
ctx['rinput'] = u'' |
|
741 |
ctx['todelete'] = u'checked="checked"' |
|
742 |
ctx['count'] = entity.row + 1 |
|
743 |
return ctx |
|
744 |
||
745 |
||
746 |
class CopyEditionForm(EditionForm): |
|
747 |
id = 'copy' |
|
748 |
title = _('copy edition') |
|
749 |
||
750 |
def cell_call(self, row, col, **kwargs): |
|
751 |
self.req.add_js(('cubicweb.ajax.js', 'cubicweb.edition.js')) |
|
752 |
self.req.add_css('cubicweb.form.css') |
|
753 |
entity = self.complete_entity(row, col, skip_bytes=True) |
|
754 |
# make a copy of entity to avoid altering the entity in the |
|
755 |
# request's cache. |
|
756 |
self.newentity = copy(entity) |
|
757 |
self.copying = self.newentity.eid |
|
758 |
self.newentity.eid = None |
|
759 |
self.edit_form(self.newentity, kwargs) |
|
760 |
del self.newentity |
|
761 |
||
762 |
def action_title(self, entity): |
|
763 |
"""form's title""" |
|
764 |
msg = super(CopyEditionForm, self).action_title(entity) |
|
765 |
return msg + (u'<script type="text/javascript">updateMessage("%s");</script>\n' |
|
766 |
% self.req._('Please note that this is only a shallow copy')) |
|
767 |
# XXX above message should have style of a warning |
|
768 |
||
769 |
@property |
|
770 |
def formid(self): |
|
771 |
return 'edition' |
|
772 |
||
773 |
def relations_form(self, entity, kwargs): |
|
774 |
return u'' |
|
775 |
||
776 |
def reset_url(self, entity): |
|
777 |
return self.build_url('view', rql='Any X WHERE X eid %s' % self.copying) |
|
778 |
||
779 |
def attributes_form(self, entity, kwargs, include_eid=True): |
|
780 |
# we don't want __clone_eid on inlined edited entities |
|
781 |
if entity.eid == self.newentity.eid: |
|
782 |
self._hiddens.append((eid_param('__cloned_eid', entity.eid), self.copying, '')) |
|
783 |
return EditionForm.attributes_form(self, entity, kwargs, include_eid) |
|
784 |
||
785 |
def submited_message(self): |
|
786 |
return self.req._('element copied') |
|
787 |
||
788 |
||
765
8fda14081686
kill Form, EntityForm and AnyRsetForm
sylvain.thenault@logilab.fr
parents:
742
diff
changeset
|
789 |
class TableEditForm(FormMixIn, EntityView): |
0 | 790 |
id = 'muledit' |
791 |
title = _('multiple edit') |
|
792 |
||
793 |
EDITION_BODY = u'''<form method="post" id="entityForm" onsubmit="return validateForm('entityForm', null);" action="%(action)s"> |
|
794 |
%(error)s |
|
795 |
<div id="progress">%(progress)s</div> |
|
796 |
<fieldset> |
|
797 |
<input type="hidden" name="__errorurl" value="%(url)s" /> |
|
798 |
<input type="hidden" name="__form_id" value="%(formid)s" /> |
|
799 |
<input type="hidden" name="__redirectvid" value="%(redirectvid)s" /> |
|
800 |
<input type="hidden" name="__redirectrql" value="%(redirectrql)s" /> |
|
801 |
<table class="listing"> |
|
802 |
<tr class="header"> |
|
803 |
<th align="left"><input type="checkbox" onclick="setCheckboxesState('eid', this.checked)" value="" title="toggle check boxes" /></th> |
|
804 |
%(attrheaders)s |
|
805 |
</tr> |
|
806 |
%(lines)s |
|
807 |
</table> |
|
808 |
<table width="100%%"> |
|
809 |
<tr> |
|
810 |
<td align="left"> |
|
811 |
<input class="validateButton" type="submit" value="%(okvalue)s" title="%(oktitle)s" /> |
|
812 |
<input class="validateButton" type="reset" name="__action_cancel" value="%(cancelvalue)s" title="%(canceltitle)s" /> |
|
813 |
</td> |
|
814 |
</tr> |
|
815 |
</table> |
|
816 |
</fieldset> |
|
817 |
</form> |
|
818 |
''' |
|
819 |
||
820 |
WIDGET_CELL = u'''\ |
|
821 |
<td%(csscls)s> |
|
822 |
%(error)s |
|
823 |
<div>%(widget)s</div> |
|
824 |
</td>''' |
|
825 |
||
826 |
def call(self, **kwargs): |
|
827 |
"""a view to edit multiple entities of the same type |
|
828 |
the first column should be the eid |
|
829 |
""" |
|
830 |
req = self.req |
|
831 |
form = req.form |
|
832 |
req.add_js('cubicweb.edition.js') |
|
833 |
req.add_css('cubicweb.form.css') |
|
834 |
_ = req._ |
|
835 |
sampleentity = self.complete_entity(0) |
|
836 |
attrheaders = [u'<th>%s</th>' % rdef[0].display_name(req, rdef[-1]) |
|
837 |
for rdef in sampleentity.relations_by_category('primary', 'add') |
|
838 |
if rdef[0].type != 'eid'] |
|
839 |
ctx = {'action' : self.build_url('edit'), |
|
840 |
'error': self.error_message(), |
|
841 |
'progress': _('validating...'), |
|
842 |
'url': html_escape(req.url()), |
|
843 |
'formid': self.id, |
|
844 |
'redirectvid': html_escape(form.get('__redirectvid', 'list')), |
|
845 |
'redirectrql': html_escape(form.get('__redirectrql', self.rset.printable_rql())), |
|
846 |
'attrheaders': u'\n'.join(attrheaders), |
|
847 |
'lines': u'\n'.join(self.edit_form(ent) for ent in self.rset.entities()), |
|
848 |
'okvalue': _('button_ok').capitalize(), |
|
849 |
'oktitle': _('validate modifications on selected items').capitalize(), |
|
850 |
'cancelvalue': _('button_reset').capitalize(), |
|
851 |
'canceltitle': _('revert changes').capitalize(), |
|
852 |
} |
|
853 |
self.w(self.EDITION_BODY % ctx) |
|
854 |
||
855 |
||
856 |
def reset_url(self, entity=None): |
|
857 |
self.build_url('view', rql=self.rset.printable_rql()) |
|
858 |
||
859 |
def edit_form(self, entity): |
|
860 |
html = [] |
|
861 |
w = html.append |
|
862 |
entity.complete() |
|
863 |
eid = entity.eid |
|
864 |
values = self.req.data.get('formvalues', ()) |
|
865 |
qeid = eid_param('eid', eid) |
|
866 |
checked = qeid in values |
|
867 |
w(u'<tr class="%s">' % (entity.row % 2 and u'even' or u'odd')) |
|
868 |
w(u'<td>%s<input type="hidden" name="__type:%s" value="%s" /></td>' |
|
869 |
% (checkbox('eid', eid, checked=checked), eid, entity.e_schema)) |
|
870 |
# attribute relations (skip eid which is handled by the checkbox |
|
871 |
wdg = entity.get_widget |
|
872 |
wdgfactories = [wdg(rschema, x) for rschema, _, x in entity.relations_by_category('primary', 'add') |
|
873 |
if rschema.type != 'eid'] # XXX both (add, delete) |
|
874 |
seid = html_escape(dumps(eid)) |
|
875 |
for wobj in wdgfactories: |
|
876 |
if isinstance(wobj, ComboBoxWidget): |
|
877 |
wobj.attrs['onchange'] = "setCheckboxesState2('eid', %s, 'checked')" % seid |
|
878 |
elif isinstance(wobj, InputWidget): |
|
879 |
wobj.attrs['onkeypress'] = "setCheckboxesState2('eid', %s, 'checked')" % seid |
|
880 |
error = wobj.render_error(entity) |
|
881 |
if error: |
|
882 |
csscls = u' class="error"' |
|
883 |
else: |
|
884 |
csscls = u'' |
|
885 |
w(self.WIDGET_CELL % {'csscls': csscls, 'error': error, |
|
886 |
'widget': wobj.edit_render(entity)}) |
|
887 |
w(u'</tr>') |
|
888 |
return '\n'.join(html) |
|
889 |
||
890 |
||
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
781
diff
changeset
|
891 |
# XXX bw compat |
0 | 892 |
|
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
781
diff
changeset
|
893 |
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
|
894 |
from cubicweb.web.views import editviews |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
781
diff
changeset
|
895 |
ComboboxView = class_moved(editviews.ComboboxView) |