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