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