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