author | sylvain.thenault@logilab.fr |
Thu, 30 Apr 2009 12:35:49 +0200 | |
branch | tls-sprint |
changeset 1576 | 3bfcf1e4eb26 |
parent 1559 | c4d4851bd18b |
child 1579 | 4eea314694e2 |
permissions | -rw-r--r-- |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1 |
"""Set of HTML automatic forms to create, delete, copy or edit a single entity |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
2 |
or a list of entities of the same type |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
3 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
4 |
:organization: Logilab |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
7 |
""" |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
8 |
__docformat__ = "restructuredtext en" |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
9 |
|
1147 | 10 |
from copy import copy |
11 |
||
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
12 |
from simplejson import dumps |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
13 |
|
1147 | 14 |
from logilab.mtconverter import html_escape |
15 |
||
16 |
from cubicweb.selectors import (match_kwargs, one_line_rset, non_final_entity, |
|
17 |
specified_etype_implements, yes) |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
18 |
from cubicweb.utils import make_uid |
1132 | 19 |
from cubicweb.view import EntityView |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
20 |
from cubicweb.common import tags |
1491
742aff97dbf7
move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
1459
diff
changeset
|
21 |
from cubicweb.web import stdmsgs |
742aff97dbf7
move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
1459
diff
changeset
|
22 |
from cubicweb.web.form import CompositeForm, EntityFieldsForm, FormViewMixIn |
1304 | 23 |
from cubicweb.web.formwidgets import Button, SubmitButton, ResetButton |
1147 | 24 |
from cubicweb.web.formrenderers import (FormRenderer, EntityFormRenderer, |
25 |
EntityCompositeFormRenderer, |
|
26 |
EntityInlinedFormRenderer) |
|
1491
742aff97dbf7
move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
1459
diff
changeset
|
27 |
|
1147 | 28 |
_ = unicode |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
29 |
|
1147 | 30 |
def relation_id(eid, rtype, role, reid): |
31 |
"""return an identifier for a relation between two entities""" |
|
32 |
if role == 'subject': |
|
33 |
return u'%s:%s:%s' % (eid, rtype, reid) |
|
34 |
return u'%s:%s:%s' % (reid, rtype, eid) |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
35 |
|
1528
864ae7c15ef5
other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1498
diff
changeset
|
36 |
def toggleable_relation_link(eid, nodeid, label='x'): |
1147 | 37 |
"""return javascript snippet to delete/undelete a relation between two |
38 |
entities |
|
39 |
""" |
|
1320 | 40 |
js = u"javascript: togglePendingDelete('%s', %s);" % ( |
41 |
nodeid, html_escape(dumps(eid))) |
|
42 |
return u'[<a class="handle" href="%s" id="handle%s">%s</a>]' % ( |
|
43 |
js, nodeid, label) |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
44 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
45 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
46 |
class DeleteConfForm(FormViewMixIn, EntityView): |
1147 | 47 |
"""form used to confirm deletion of some entities""" |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
48 |
id = 'deleteconf' |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
49 |
title = _('delete') |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
50 |
# don't use navigation, all entities asked to be deleted should be displayed |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
51 |
# else we will only delete the displayed page |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
52 |
need_navigation = False |
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
53 |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
54 |
def call(self): |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
55 |
"""ask for confirmation before real deletion""" |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
56 |
req, w = self.req, self.w |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
57 |
_ = req._ |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
58 |
w(u'<script type="text/javascript">updateMessage(\'%s\');</script>\n' |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
59 |
% _('this action is not reversible!')) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
60 |
# XXX above message should have style of a warning |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
61 |
w(u'<h4>%s</h4>\n' % _('Do you want to delete the following element(s) ?')) |
1304 | 62 |
form = CompositeForm(req, domid='deleteconf', copy_nav_params=True, |
63 |
action=self.build_url('edit'), onsubmit=None, |
|
64 |
form_buttons=[Button(stdmsgs.YES, cwaction='delete'), |
|
65 |
Button(stdmsgs.NO, cwaction='cancel')]) |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
66 |
done = set() |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
67 |
w(u'<ul>\n') |
1147 | 68 |
for entity in self.rset.entities(): |
69 |
if entity.eid in done: |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
70 |
continue |
1147 | 71 |
done.add(entity.eid) |
72 |
subform = EntityFieldsForm(req, entity=entity, set_error_url=False) |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
73 |
form.form_add_subform(subform) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
74 |
# don't use outofcontext view or any other that may contain inline edition form |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
75 |
w(u'<li>%s</li>' % tags.a(entity.view('textoutofcontext'), |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
76 |
href=entity.absolute_url())) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
77 |
w(u'</ul>\n') |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
78 |
w(form.form_render()) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
79 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
80 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
81 |
class ClickAndEditFormView(FormViewMixIn, EntityView): |
1147 | 82 |
"""form used to permit ajax edition of an attribute of an entity in a view |
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
83 |
|
1147 | 84 |
(double-click on the field to see an appropriate edition widget) |
85 |
""" |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
86 |
id = 'reledit' |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
87 |
__select__ = non_final_entity() & match_kwargs('rtype') |
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
88 |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
89 |
# FIXME editableField class could be toggleable from userprefs |
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
90 |
|
1147 | 91 |
onsubmit = ("return inlineValidateForm('%(divid)s-form', '%(rtype)s', " |
92 |
"'%(eid)s', '%(divid)s', %(reload)s);") |
|
93 |
ondblclick = "showInlineEditionForm(%(eid)s, '%(rtype)s', '%(divid)s')" |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
94 |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
95 |
def cell_call(self, row, col, rtype=None, role='subject', reload=False): |
1147 | 96 |
"""display field to edit entity's `rtype` relation on double-click""" |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
97 |
entity = self.entity(row, col) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
98 |
if getattr(entity, rtype) is None: |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
99 |
value = self.req._('not specified') |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
100 |
else: |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
101 |
value = entity.printable_value(rtype) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
102 |
if not entity.has_perm('update'): |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
103 |
self.w(value) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
104 |
return |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
105 |
eid = entity.eid |
1559
c4d4851bd18b
[editforms] fix missing __maineid, avoid double submit through submit button, say no to locals()
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1528
diff
changeset
|
106 |
divid = 'd%s' % make_uid('%s-%s' % (rtype, eid)) |
1576
3bfcf1e4eb26
display_fields should search for name+role
sylvain.thenault@logilab.fr
parents:
1559
diff
changeset
|
107 |
event_data = {'divid' : divid, 'eid' : eid, 'rtype' : rtype, |
3bfcf1e4eb26
display_fields should search for name+role
sylvain.thenault@logilab.fr
parents:
1559
diff
changeset
|
108 |
'reload' : dumps(reload)} |
1559
c4d4851bd18b
[editforms] fix missing __maineid, avoid double submit through submit button, say no to locals()
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1528
diff
changeset
|
109 |
buttons = [SubmitButton(stdmsgs.BUTTON_OK), |
1304 | 110 |
Button(stdmsgs.BUTTON_CANCEL, |
1576
3bfcf1e4eb26
display_fields should search for name+role
sylvain.thenault@logilab.fr
parents:
1559
diff
changeset
|
111 |
onclick="cancelInlineEdit(%s,\'%s\',\'%s\')" % ( |
3bfcf1e4eb26
display_fields should search for name+role
sylvain.thenault@logilab.fr
parents:
1559
diff
changeset
|
112 |
eid, rtype, divid))] |
1147 | 113 |
form = self.vreg.select_object('forms', 'edition', self.req, self.rset, |
1304 | 114 |
row=row, col=col, form_buttons=buttons, |
1147 | 115 |
domid='%s-form' % divid, action='#', |
116 |
cssstyle='display: none', |
|
1559
c4d4851bd18b
[editforms] fix missing __maineid, avoid double submit through submit button, say no to locals()
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1528
diff
changeset
|
117 |
onsubmit=self.onsubmit % event_data) |
c4d4851bd18b
[editforms] fix missing __maineid, avoid double submit through submit button, say no to locals()
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1528
diff
changeset
|
118 |
form.form_add_hidden(u'__maineid', entity.eid) |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
119 |
renderer = FormRenderer(display_label=False, display_help=False, |
1576
3bfcf1e4eb26
display_fields should search for name+role
sylvain.thenault@logilab.fr
parents:
1559
diff
changeset
|
120 |
display_fields=[(rtype, role)], |
3bfcf1e4eb26
display_fields should search for name+role
sylvain.thenault@logilab.fr
parents:
1559
diff
changeset
|
121 |
button_bar_class='buttonbar', |
1366
fd5bd1a63c3f
no progress div in reledit, else we get duplicated ids
sylvain.thenault@logilab.fr
parents:
1324
diff
changeset
|
122 |
display_progress_div=False) |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
123 |
self.w(tags.div(value, klass='editableField', id=divid, |
1559
c4d4851bd18b
[editforms] fix missing __maineid, avoid double submit through submit button, say no to locals()
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1528
diff
changeset
|
124 |
ondblclick=self.ondblclick % event_data)) |
1147 | 125 |
self.w(form.form_render(renderer=renderer)) |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
126 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
127 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
128 |
class EditionFormView(FormViewMixIn, EntityView): |
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
129 |
"""display primary entity edition form""" |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
130 |
id = 'edition' |
1147 | 131 |
# add yes() so it takes precedence over deprecated views in baseforms, |
132 |
# though not baseforms based customized view |
|
133 |
__select__ = one_line_rset() & non_final_entity() & yes() |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
134 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
135 |
title = _('edition') |
1324 | 136 |
renderer = EntityFormRenderer() |
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
137 |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
138 |
def cell_call(self, row, col, **kwargs): |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
139 |
entity = self.complete_entity(row, col) |
1147 | 140 |
self.render_form(entity) |
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
141 |
|
1147 | 142 |
def render_form(self, entity): |
143 |
"""fetch and render the form""" |
|
144 |
self.form_title(entity) |
|
1286
cb68c8af3858
use entity's rset and row/col info
sylvain.thenault@logilab.fr
parents:
1285
diff
changeset
|
145 |
form = self.vreg.select_object('forms', 'edition', self.req, entity.rset, |
cb68c8af3858
use entity's rset and row/col info
sylvain.thenault@logilab.fr
parents:
1285
diff
changeset
|
146 |
row=entity.row, col=entity.col, entity=entity, |
1305
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
147 |
submitmsg=self.submited_message()) |
1147 | 148 |
self.init_form(form, entity) |
1324 | 149 |
self.w(form.form_render(renderer=self.renderer, formvid=u'edition')) |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
150 |
|
1147 | 151 |
def init_form(self, form, entity): |
152 |
"""customize your form before rendering here""" |
|
153 |
form.form_add_hidden(u'__maineid', entity.eid) |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
154 |
|
1147 | 155 |
def form_title(self, entity): |
156 |
"""the form view title""" |
|
157 |
ptitle = self.req._(self.title) |
|
158 |
self.w(u'<div class="formTitle"><span>%s %s</span></div>' % ( |
|
159 |
entity.dc_type(), ptitle and '(%s)' % ptitle)) |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
160 |
|
1147 | 161 |
def submited_message(self): |
162 |
"""return the message that will be displayed on successful edition""" |
|
163 |
return self.req._('entity edited') |
|
164 |
||
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
165 |
|
1147 | 166 |
class CreationFormView(EditionFormView): |
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
167 |
"""display primary entity creation form""" |
1147 | 168 |
id = 'creation' |
169 |
__select__ = specified_etype_implements('Any') & yes() |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
170 |
|
1147 | 171 |
title = _('creation') |
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
172 |
|
1147 | 173 |
def call(self, **kwargs): |
174 |
"""creation view for an entity""" |
|
175 |
etype = kwargs.pop('etype', self.req.form.get('etype')) |
|
176 |
try: |
|
1435
6cd6172718bb
allow to instantiate an entity without rset
sylvain.thenault@logilab.fr
parents:
1423
diff
changeset
|
177 |
entity = self.vreg.etype_class(etype)(self.req) |
1147 | 178 |
except: |
179 |
self.w(self.req._('no such entity type %s') % etype) |
|
180 |
else: |
|
181 |
self.initialize_varmaker() |
|
182 |
entity.eid = self.varmaker.next() |
|
183 |
self.render_form(entity) |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
184 |
|
1147 | 185 |
def form_title(self, entity): |
186 |
"""the form view title""" |
|
187 |
if '__linkto' in self.req.form: |
|
188 |
if isinstance(self.req.form['__linkto'], list): |
|
189 |
# XXX which one should be considered (case: add a ticket to a |
|
190 |
# version in jpl) |
|
191 |
rtype, linkto_eid, role = self.req.form['__linkto'][0].split(':') |
|
192 |
else: |
|
193 |
rtype, linkto_eid, role = self.req.form['__linkto'].split(':') |
|
194 |
linkto_rset = self.req.eid_rset(linkto_eid) |
|
195 |
linkto_type = linkto_rset.description[0][0] |
|
196 |
if role == 'subject': |
|
197 |
title = self.req.__('creating %s (%s %s %s %%(linkto)s)' % ( |
|
198 |
entity.e_schema, entity.e_schema, rtype, linkto_type)) |
|
199 |
else: |
|
200 |
title = self.req.__('creating %s (%s %%(linkto)s %s %s)' % ( |
|
201 |
entity.e_schema, linkto_type, rtype, entity.e_schema)) |
|
202 |
msg = title % {'linkto' : self.view('incontext', linkto_rset)} |
|
203 |
self.w(u'<div class="formTitle notransform"><span>%s</span></div>' % msg) |
|
204 |
else: |
|
205 |
super(CreationFormView, self).form_title(entity) |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
206 |
|
1147 | 207 |
def url(self): |
208 |
"""return the url associated with this view""" |
|
209 |
return self.create_url(self.req.form.get('etype')) |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
210 |
|
1147 | 211 |
def submited_message(self): |
212 |
"""return the message that will be displayed on successful edition""" |
|
213 |
return self.req._('entity created') |
|
214 |
||
215 |
||
216 |
class CopyFormView(EditionFormView): |
|
217 |
"""display primary entity creation form initialized with values from another |
|
218 |
entity |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
219 |
""" |
1147 | 220 |
id = 'copy' |
221 |
def render_form(self, entity): |
|
222 |
"""fetch and render the form""" |
|
223 |
# make a copy of entity to avoid altering the entity in the |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
224 |
# request's cache. |
1147 | 225 |
self.newentity = copy(entity) |
226 |
self.copying = self.newentity.eid |
|
227 |
self.newentity.eid = None |
|
228 |
self.w(u'<script type="text/javascript">updateMessage("%s");</script>\n' |
|
229 |
% self.req._('Please note that this is only a shallow copy')) |
|
230 |
super(CopyFormView, self).render_form(entity) |
|
231 |
del self.newentity |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
232 |
|
1147 | 233 |
def init_form(self, form, entity): |
234 |
"""customize your form before rendering here""" |
|
235 |
super(CopyFormView, self).init_form(form, entity) |
|
236 |
if entity.eid == self.newentity.eid: |
|
237 |
form.form_add_hidden('__cloned_eid', self.copying, eidparam=True) |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
238 |
|
1147 | 239 |
def submited_message(self): |
240 |
"""return the message that will be displayed on successful edition""" |
|
241 |
return self.req._('entity copied') |
|
242 |
||
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
243 |
|
1147 | 244 |
class TableEditForm(CompositeForm): |
245 |
id = 'muledit' |
|
246 |
onsubmit = "return validateForm('entityForm', null);" |
|
1304 | 247 |
form_buttons = [SubmitButton(_('validate modifications on selected items')), |
248 |
ResetButton(_('revert changes'))] |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
249 |
|
1147 | 250 |
def __init__(self, *args, **kwargs): |
251 |
super(TableEditForm, self).__init__(*args, **kwargs) |
|
252 |
for row in xrange(len(self.rset)): |
|
253 |
form = self.vreg.select_object('forms', 'edition', self.req, self.rset, |
|
1305
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
254 |
row=row, attrcategories=('primary',), |
1147 | 255 |
set_error_url=False) |
1324 | 256 |
# XXX rely on the EntityCompositeFormRenderer to put the eid input |
1147 | 257 |
form.remove_field(form.field_by_name('eid')) |
258 |
self.form_add_subform(form) |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
259 |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
260 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
261 |
class TableEditFormView(FormViewMixIn, EntityView): |
1147 | 262 |
id = 'muledit' |
263 |
__select__ = EntityView.__select__ & yes() |
|
264 |
title = _('multiple edit') |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
265 |
|
1147 | 266 |
def call(self, **kwargs): |
267 |
"""a view to edit multiple entities of the same type the first column |
|
268 |
should be the eid |
|
269 |
""" |
|
270 |
#self.form_title(entity) |
|
1305
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
271 |
form = self.vreg.select_object('forms', self.id, self.req, self.rset) |
1147 | 272 |
self.w(form.form_render(renderer=EntityCompositeFormRenderer())) |
273 |
||
274 |
||
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
275 |
class InlineEntityEditionFormView(FormViewMixIn, EntityView): |
1147 | 276 |
id = 'inline-edition' |
277 |
__select__ = non_final_entity() & match_kwargs('peid', 'rtype') |
|
278 |
removejs = "removeInlinedEntity('%s', '%s', '%s')" |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
279 |
|
1147 | 280 |
def call(self, **kwargs): |
281 |
"""redefine default call() method to avoid automatic |
|
282 |
insertions of <div class="section"> between each row of |
|
283 |
the resultset |
|
284 |
""" |
|
285 |
rset = self.rset |
|
286 |
for i in xrange(len(rset)): |
|
287 |
self.wview(self.id, rset, row=i, **kwargs) |
|
288 |
||
289 |
def cell_call(self, row, col, peid, rtype, role='subject', **kwargs): |
|
290 |
""" |
|
291 |
:param peid: the parent entity's eid hosting the inline form |
|
292 |
:param rtype: the relation bridging `etype` and `peid` |
|
293 |
:param role: the role played by the `peid` in the relation |
|
294 |
""" |
|
295 |
entity = self.entity(row, col) |
|
296 |
divonclick = "restoreInlinedEntity('%s', '%s', '%s')" % (peid, rtype, |
|
297 |
entity.eid) |
|
298 |
self.render_form(entity, peid, rtype, role, divonclick=divonclick) |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
299 |
|
1147 | 300 |
def render_form(self, entity, peid, rtype, role, **kwargs): |
301 |
"""fetch and render the form""" |
|
1297 | 302 |
form = self.vreg.select_object('forms', 'edition', self.req, None, |
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
303 |
entity=entity, set_error_url=False) |
1396 | 304 |
self.add_hiddens(form, entity, peid, rtype, role) |
305 |
divid = '%s-%s-%s' % (peid, rtype, entity.eid) |
|
306 |
title = self.schema.rschema(rtype).display_name(self.req, role) |
|
1147 | 307 |
removejs = self.removejs % (peid, rtype,entity.eid) |
1396 | 308 |
self.w(form.form_render(renderer=EntityInlinedFormRenderer(), divid=divid, |
309 |
title=title, removejs=removejs,**kwargs)) |
|
310 |
||
311 |
def add_hiddens(self, form, entity, peid, rtype, role): |
|
312 |
# to ease overriding (see cubes.vcsfile.views.forms for instance) |
|
1147 | 313 |
if self.keep_entity(entity, peid, rtype): |
314 |
if entity.has_eid(): |
|
315 |
rval = entity.eid |
|
316 |
else: |
|
317 |
rval = INTERNAL_FIELD_VALUE |
|
318 |
form.form_add_hidden('edit%s-%s:%s' % (role[0], rtype, peid), rval) |
|
319 |
form.form_add_hidden(name='%s:%s' % (rtype, peid), value=entity.eid, |
|
320 |
id='rel-%s-%s-%s' % (peid, rtype, entity.eid)) |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
321 |
|
1147 | 322 |
def keep_entity(self, entity, peid, rtype): |
323 |
if not entity.has_eid(): |
|
324 |
return True |
|
325 |
# are we regenerating form because of a validation error ? |
|
326 |
erroneous_post = self.req.data.get('formvalues') |
|
327 |
if erroneous_post: |
|
328 |
cdvalues = self.req.list_form_param('%s:%s' % (rtype, peid), |
|
329 |
erroneous_post) |
|
330 |
if unicode(entity.eid) not in cdvalues: |
|
331 |
return False |
|
332 |
return True |
|
333 |
||
334 |
||
335 |
class InlineEntityCreationFormView(InlineEntityEditionFormView): |
|
336 |
id = 'inline-creation' |
|
337 |
__select__ = (match_kwargs('peid', 'rtype') |
|
338 |
& specified_etype_implements('Any')) |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
339 |
removejs = "removeInlineForm('%s', '%s', '%s')" |
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
340 |
|
1147 | 341 |
def call(self, etype, peid, rtype, role='subject', **kwargs): |
342 |
""" |
|
343 |
:param etype: the entity type being created in the inline form |
|
344 |
:param peid: the parent entity's eid hosting the inline form |
|
345 |
:param rtype: the relation bridging `etype` and `peid` |
|
346 |
:param role: the role played by the `peid` in the relation |
|
347 |
""" |
|
348 |
try: |
|
349 |
entity = self.vreg.etype_class(etype)(self.req, None, None) |
|
350 |
except: |
|
351 |
self.w(self.req._('no such entity type %s') % etype) |
|
352 |
return |
|
353 |
self.initialize_varmaker() |
|
354 |
entity.eid = self.varmaker.next() |
|
355 |
self.render_form(entity, peid, rtype, role) |