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