author | sylvain.thenault@logilab.fr |
Wed, 13 May 2009 17:00:08 +0200 | |
branch | tls-sprint |
changeset 1788 | d6e6ad70e50a |
parent 1781 | 7a934055dd45 |
child 1798 | cc86fe8efaaa |
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 |
1685
3c59ae0e6548
fix copy edition view
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1629
diff
changeset
|
21 |
from cubicweb.web import INTERNAL_FIELD_VALUE, stdmsgs, eid_param |
1491
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 |
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
23 |
from cubicweb.web.formfields import RelationField |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
24 |
from cubicweb.web.formwidgets import Button, SubmitButton, ResetButton, Select |
1147 | 25 |
from cubicweb.web.formrenderers import (FormRenderer, EntityFormRenderer, |
26 |
EntityCompositeFormRenderer, |
|
27 |
EntityInlinedFormRenderer) |
|
1491
742aff97dbf7
move AutomaticEntityForm and PrimaryView into their own module
sylvain.thenault@logilab.fr
parents:
1459
diff
changeset
|
28 |
|
1147 | 29 |
_ = unicode |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
30 |
|
1147 | 31 |
def relation_id(eid, rtype, role, reid): |
32 |
"""return an identifier for a relation between two entities""" |
|
33 |
if role == 'subject': |
|
34 |
return u'%s:%s:%s' % (eid, rtype, reid) |
|
35 |
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
|
36 |
|
1528
864ae7c15ef5
other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1498
diff
changeset
|
37 |
def toggleable_relation_link(eid, nodeid, label='x'): |
1147 | 38 |
"""return javascript snippet to delete/undelete a relation between two |
39 |
entities |
|
40 |
""" |
|
1320 | 41 |
js = u"javascript: togglePendingDelete('%s', %s);" % ( |
42 |
nodeid, html_escape(dumps(eid))) |
|
43 |
return u'[<a class="handle" href="%s" id="handle%s">%s</a>]' % ( |
|
44 |
js, nodeid, label) |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
45 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
46 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
47 |
class DeleteConfForm(FormViewMixIn, EntityView): |
1147 | 48 |
"""form used to confirm deletion of some entities""" |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
49 |
id = 'deleteconf' |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
50 |
title = _('delete') |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
51 |
# 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
|
52 |
# else we will only delete the displayed page |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
53 |
need_navigation = False |
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
54 |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
55 |
def call(self): |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
56 |
"""ask for confirmation before real deletion""" |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
57 |
req, w = self.req, self.w |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
58 |
_ = req._ |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
59 |
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
|
60 |
% _('this action is not reversible!')) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
61 |
# XXX above message should have style of a warning |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
62 |
w(u'<h4>%s</h4>\n' % _('Do you want to delete the following element(s) ?')) |
1304 | 63 |
form = CompositeForm(req, domid='deleteconf', copy_nav_params=True, |
64 |
action=self.build_url('edit'), onsubmit=None, |
|
65 |
form_buttons=[Button(stdmsgs.YES, cwaction='delete'), |
|
66 |
Button(stdmsgs.NO, cwaction='cancel')]) |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
67 |
done = set() |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
68 |
w(u'<ul>\n') |
1147 | 69 |
for entity in self.rset.entities(): |
70 |
if entity.eid in done: |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
71 |
continue |
1147 | 72 |
done.add(entity.eid) |
73 |
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
|
74 |
form.form_add_subform(subform) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
75 |
# 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
|
76 |
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
|
77 |
href=entity.absolute_url())) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
78 |
w(u'</ul>\n') |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
79 |
w(form.form_render()) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
80 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
81 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
82 |
class ClickAndEditFormView(FormViewMixIn, EntityView): |
1147 | 83 |
"""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
|
84 |
|
1147 | 85 |
(double-click on the field to see an appropriate edition widget) |
86 |
""" |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
87 |
id = 'reledit' |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
88 |
__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
|
89 |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
90 |
# 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
|
91 |
|
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
92 |
onsubmit = ("return inlineValidateAttributeForm('%(divid)s-form', '%(rtype)s', " |
1147 | 93 |
"'%(eid)s', '%(divid)s', %(reload)s);") |
94 |
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
|
95 |
|
1579 | 96 |
def cell_call(self, row, col, rtype=None, role='subject', reload=False, |
1762
1f7bb0cd950c
accept a default value for empty attributes/relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1760
diff
changeset
|
97 |
vid='textoutofcontext', default=u''): |
1147 | 98 |
"""display field to edit entity's `rtype` relation on double-click""" |
1579 | 99 |
rschema = self.schema.rschema(rtype) |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
100 |
entity = self.entity(row, col) |
1579 | 101 |
if rschema.is_final(): |
102 |
if getattr(entity, rtype) is None: |
|
1762
1f7bb0cd950c
accept a default value for empty attributes/relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1760
diff
changeset
|
103 |
value = default or self.req._('not specified') |
1579 | 104 |
else: |
1781
7a934055dd45
no (not for attributes anyway)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1778
diff
changeset
|
105 |
value = entity.printable_value(rtype) |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
106 |
else: |
1579 | 107 |
rset = entity.related(rtype, role) |
1778 | 108 |
# XXX html_escape but that depends of the actual vid |
109 |
value = html_escape(self.view(vid, rset, 'null') or default) |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
110 |
if not entity.has_perm('update'): |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
111 |
self.w(value) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
112 |
return |
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
113 |
if rschema.is_final(): |
1760
6b97d286eb5a
[reledit] relation edition : cleanup a bit #343123
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1759
diff
changeset
|
114 |
form = self._build_attribute_form(entity, value, rtype, role, reload, row, col) |
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
115 |
else: |
1760
6b97d286eb5a
[reledit] relation edition : cleanup a bit #343123
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1759
diff
changeset
|
116 |
form = self._build_relation_form(entity, value, rtype, role, row, col, vid) |
6b97d286eb5a
[reledit] relation edition : cleanup a bit #343123
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1759
diff
changeset
|
117 |
form.form_add_hidden(u'__maineid', entity.eid) |
6b97d286eb5a
[reledit] relation edition : cleanup a bit #343123
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1759
diff
changeset
|
118 |
renderer = FormRenderer(display_label=False, display_help=False, |
6b97d286eb5a
[reledit] relation edition : cleanup a bit #343123
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1759
diff
changeset
|
119 |
display_fields=[(rtype, role)], |
6b97d286eb5a
[reledit] relation edition : cleanup a bit #343123
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1759
diff
changeset
|
120 |
table_class='', button_bar_class='buttonbar', |
6b97d286eb5a
[reledit] relation edition : cleanup a bit #343123
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1759
diff
changeset
|
121 |
display_progress_div=False) |
6b97d286eb5a
[reledit] relation edition : cleanup a bit #343123
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1759
diff
changeset
|
122 |
self.w(form.form_render(renderer=renderer)) |
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
123 |
|
1760
6b97d286eb5a
[reledit] relation edition : cleanup a bit #343123
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1759
diff
changeset
|
124 |
def _build_relation_form(self, entity, value, rtype, role, row, col, vid): |
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
125 |
entity = self.entity(row, col) |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
126 |
divid = 'd%s' % make_uid('%s-%s' % (rtype, entity.eid)) |
1760
6b97d286eb5a
[reledit] relation edition : cleanup a bit #343123
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1759
diff
changeset
|
127 |
event_data = {'divid' : divid, 'eid' : entity.eid, 'rtype' : rtype, 'vid' : vid} |
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
128 |
form = EntityFieldsForm(self.req, None, entity=entity, action='#', |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
129 |
domid='%s-form' % divid, |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
130 |
cssstyle='display: none', |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
131 |
onsubmit=("return inlineValidateRelationForm('%(divid)s-form', '%(rtype)s', " |
1760
6b97d286eb5a
[reledit] relation edition : cleanup a bit #343123
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1759
diff
changeset
|
132 |
"'%(eid)s', '%(divid)s', '%(vid)s');" % event_data), |
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
133 |
form_buttons=[SubmitButton(), |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
134 |
Button(stdmsgs.BUTTON_CANCEL, |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
135 |
onclick="cancelInlineEdit(%s,\'%s\',\'%s\')" %\ |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
136 |
(entity.eid, rtype, divid))]) |
1767 | 137 |
form.append_field(RelationField(name=rtype, role=role, sort=True, |
1763
65ec5ccc8927
that should be automatic
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1762
diff
changeset
|
138 |
widget=Select(), |
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
139 |
label=u' ')) |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
140 |
self.w(tags.div(value, klass='editableField', id=divid, |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
141 |
ondblclick=self.ondblclick % event_data)) |
1760
6b97d286eb5a
[reledit] relation edition : cleanup a bit #343123
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1759
diff
changeset
|
142 |
return form |
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1750
diff
changeset
|
143 |
|
1760
6b97d286eb5a
[reledit] relation edition : cleanup a bit #343123
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1759
diff
changeset
|
144 |
def _build_attribute_form(self, entity, value, rtype, role, reload, row, col): |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
145 |
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
|
146 |
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
|
147 |
event_data = {'divid' : divid, 'eid' : eid, 'rtype' : rtype, |
3bfcf1e4eb26
display_fields should search for name+role
sylvain.thenault@logilab.fr
parents:
1559
diff
changeset
|
148 |
'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
|
149 |
buttons = [SubmitButton(stdmsgs.BUTTON_OK), |
1304 | 150 |
Button(stdmsgs.BUTTON_CANCEL, |
1576
3bfcf1e4eb26
display_fields should search for name+role
sylvain.thenault@logilab.fr
parents:
1559
diff
changeset
|
151 |
onclick="cancelInlineEdit(%s,\'%s\',\'%s\')" % ( |
3bfcf1e4eb26
display_fields should search for name+role
sylvain.thenault@logilab.fr
parents:
1559
diff
changeset
|
152 |
eid, rtype, divid))] |
1147 | 153 |
form = self.vreg.select_object('forms', 'edition', self.req, self.rset, |
1304 | 154 |
row=row, col=col, form_buttons=buttons, |
1147 | 155 |
domid='%s-form' % divid, action='#', |
156 |
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
|
157 |
onsubmit=self.onsubmit % event_data) |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
158 |
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
|
159 |
ondblclick=self.ondblclick % event_data)) |
1760
6b97d286eb5a
[reledit] relation edition : cleanup a bit #343123
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1759
diff
changeset
|
160 |
return form |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
161 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
162 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
163 |
class EditionFormView(FormViewMixIn, EntityView): |
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
164 |
"""display primary entity edition form""" |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
165 |
id = 'edition' |
1147 | 166 |
# add yes() so it takes precedence over deprecated views in baseforms, |
167 |
# though not baseforms based customized view |
|
168 |
__select__ = one_line_rset() & non_final_entity() & yes() |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
169 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
170 |
title = _('edition') |
1324 | 171 |
renderer = EntityFormRenderer() |
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
172 |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
173 |
def cell_call(self, row, col, **kwargs): |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
174 |
entity = self.complete_entity(row, col) |
1147 | 175 |
self.render_form(entity) |
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
176 |
|
1147 | 177 |
def render_form(self, entity): |
178 |
"""fetch and render the form""" |
|
179 |
self.form_title(entity) |
|
1286
cb68c8af3858
use entity's rset and row/col info
sylvain.thenault@logilab.fr
parents:
1285
diff
changeset
|
180 |
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
|
181 |
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
|
182 |
submitmsg=self.submited_message()) |
1147 | 183 |
self.init_form(form, entity) |
1324 | 184 |
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
|
185 |
|
1147 | 186 |
def init_form(self, form, entity): |
187 |
"""customize your form before rendering here""" |
|
188 |
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
|
189 |
|
1147 | 190 |
def form_title(self, entity): |
191 |
"""the form view title""" |
|
192 |
ptitle = self.req._(self.title) |
|
193 |
self.w(u'<div class="formTitle"><span>%s %s</span></div>' % ( |
|
194 |
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
|
195 |
|
1147 | 196 |
def submited_message(self): |
197 |
"""return the message that will be displayed on successful edition""" |
|
198 |
return self.req._('entity edited') |
|
199 |
||
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
200 |
|
1147 | 201 |
class CreationFormView(EditionFormView): |
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
202 |
"""display primary entity creation form""" |
1147 | 203 |
id = 'creation' |
204 |
__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
|
205 |
|
1147 | 206 |
title = _('creation') |
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
207 |
|
1147 | 208 |
def call(self, **kwargs): |
209 |
"""creation view for an entity""" |
|
210 |
etype = kwargs.pop('etype', self.req.form.get('etype')) |
|
211 |
try: |
|
1435
6cd6172718bb
allow to instantiate an entity without rset
sylvain.thenault@logilab.fr
parents:
1423
diff
changeset
|
212 |
entity = self.vreg.etype_class(etype)(self.req) |
1147 | 213 |
except: |
214 |
self.w(self.req._('no such entity type %s') % etype) |
|
215 |
else: |
|
216 |
self.initialize_varmaker() |
|
217 |
entity.eid = self.varmaker.next() |
|
218 |
self.render_form(entity) |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
219 |
|
1147 | 220 |
def form_title(self, entity): |
221 |
"""the form view title""" |
|
222 |
if '__linkto' in self.req.form: |
|
223 |
if isinstance(self.req.form['__linkto'], list): |
|
224 |
# XXX which one should be considered (case: add a ticket to a |
|
225 |
# version in jpl) |
|
226 |
rtype, linkto_eid, role = self.req.form['__linkto'][0].split(':') |
|
227 |
else: |
|
228 |
rtype, linkto_eid, role = self.req.form['__linkto'].split(':') |
|
229 |
linkto_rset = self.req.eid_rset(linkto_eid) |
|
230 |
linkto_type = linkto_rset.description[0][0] |
|
231 |
if role == 'subject': |
|
232 |
title = self.req.__('creating %s (%s %s %s %%(linkto)s)' % ( |
|
233 |
entity.e_schema, entity.e_schema, rtype, linkto_type)) |
|
234 |
else: |
|
235 |
title = self.req.__('creating %s (%s %%(linkto)s %s %s)' % ( |
|
236 |
entity.e_schema, linkto_type, rtype, entity.e_schema)) |
|
237 |
msg = title % {'linkto' : self.view('incontext', linkto_rset)} |
|
238 |
self.w(u'<div class="formTitle notransform"><span>%s</span></div>' % msg) |
|
239 |
else: |
|
240 |
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
|
241 |
|
1147 | 242 |
def url(self): |
243 |
"""return the url associated with this view""" |
|
244 |
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
|
245 |
|
1147 | 246 |
def submited_message(self): |
247 |
"""return the message that will be displayed on successful edition""" |
|
248 |
return self.req._('entity created') |
|
249 |
||
250 |
||
251 |
class CopyFormView(EditionFormView): |
|
252 |
"""display primary entity creation form initialized with values from another |
|
253 |
entity |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
254 |
""" |
1147 | 255 |
id = 'copy' |
256 |
def render_form(self, entity): |
|
257 |
"""fetch and render the form""" |
|
258 |
# 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
|
259 |
# request's cache. |
1685
3c59ae0e6548
fix copy edition view
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1629
diff
changeset
|
260 |
entity.complete() |
1147 | 261 |
self.newentity = copy(entity) |
1703
a2b5dfdb4b62
should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents:
1692
diff
changeset
|
262 |
self.copying = entity |
1685
3c59ae0e6548
fix copy edition view
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1629
diff
changeset
|
263 |
self.initialize_varmaker() |
3c59ae0e6548
fix copy edition view
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1629
diff
changeset
|
264 |
self.newentity.eid = self.varmaker.next() |
1147 | 265 |
self.w(u'<script type="text/javascript">updateMessage("%s");</script>\n' |
266 |
% self.req._('Please note that this is only a shallow copy')) |
|
1685
3c59ae0e6548
fix copy edition view
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1629
diff
changeset
|
267 |
super(CopyFormView, self).render_form(self.newentity) |
1147 | 268 |
del self.newentity |
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
269 |
|
1147 | 270 |
def init_form(self, form, entity): |
271 |
"""customize your form before rendering here""" |
|
272 |
super(CopyFormView, self).init_form(form, entity) |
|
273 |
if entity.eid == self.newentity.eid: |
|
1703
a2b5dfdb4b62
should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents:
1692
diff
changeset
|
274 |
form.form_add_hidden(eid_param('__cloned_eid', entity.eid), |
a2b5dfdb4b62
should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents:
1692
diff
changeset
|
275 |
self.copying.eid) |
a2b5dfdb4b62
should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents:
1692
diff
changeset
|
276 |
for rschema, _, role in form.relations_by_category(form.attrcategories, |
a2b5dfdb4b62
should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents:
1692
diff
changeset
|
277 |
'add'): |
a2b5dfdb4b62
should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents:
1692
diff
changeset
|
278 |
if not rschema.is_final(): |
a2b5dfdb4b62
should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents:
1692
diff
changeset
|
279 |
# ensure relation cache is filed |
a2b5dfdb4b62
should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents:
1692
diff
changeset
|
280 |
rset = self.copying.related(rschema, role) |
a2b5dfdb4b62
should prefill cached values for relation in the primary or secondary category
sylvain.thenault@logilab.fr
parents:
1692
diff
changeset
|
281 |
self.newentity.set_related_cache(rschema, role, rset) |
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
282 |
|
1147 | 283 |
def submited_message(self): |
284 |
"""return the message that will be displayed on successful edition""" |
|
285 |
return self.req._('entity copied') |
|
286 |
||
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
287 |
|
1147 | 288 |
class TableEditForm(CompositeForm): |
289 |
id = 'muledit' |
|
1692 | 290 |
domid = 'entityForm' |
291 |
onsubmit = "return validateForm('%s', null);" % domid |
|
1304 | 292 |
form_buttons = [SubmitButton(_('validate modifications on selected items')), |
293 |
ResetButton(_('revert changes'))] |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
294 |
|
1692 | 295 |
def __init__(self, req, rset, **kwargs): |
296 |
kwargs.setdefault('__redirectrql', rset.printable_rql()) |
|
297 |
super(TableEditForm, self).__init__(req, rset, **kwargs) |
|
1147 | 298 |
for row in xrange(len(self.rset)): |
299 |
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
|
300 |
row=row, attrcategories=('primary',), |
1147 | 301 |
set_error_url=False) |
1324 | 302 |
# XXX rely on the EntityCompositeFormRenderer to put the eid input |
1147 | 303 |
form.remove_field(form.field_by_name('eid')) |
304 |
self.form_add_subform(form) |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
305 |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
306 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
307 |
class TableEditFormView(FormViewMixIn, EntityView): |
1147 | 308 |
id = 'muledit' |
309 |
__select__ = EntityView.__select__ & yes() |
|
310 |
title = _('multiple edit') |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
311 |
|
1147 | 312 |
def call(self, **kwargs): |
313 |
"""a view to edit multiple entities of the same type the first column |
|
314 |
should be the eid |
|
315 |
""" |
|
316 |
#self.form_title(entity) |
|
1305
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
317 |
form = self.vreg.select_object('forms', self.id, self.req, self.rset) |
1147 | 318 |
self.w(form.form_render(renderer=EntityCompositeFormRenderer())) |
319 |
||
320 |
||
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
321 |
class InlineEntityEditionFormView(FormViewMixIn, EntityView): |
1147 | 322 |
id = 'inline-edition' |
323 |
__select__ = non_final_entity() & match_kwargs('peid', 'rtype') |
|
324 |
removejs = "removeInlinedEntity('%s', '%s', '%s')" |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
325 |
|
1147 | 326 |
def call(self, **kwargs): |
327 |
"""redefine default call() method to avoid automatic |
|
328 |
insertions of <div class="section"> between each row of |
|
329 |
the resultset |
|
330 |
""" |
|
331 |
rset = self.rset |
|
332 |
for i in xrange(len(rset)): |
|
333 |
self.wview(self.id, rset, row=i, **kwargs) |
|
334 |
||
335 |
def cell_call(self, row, col, peid, rtype, role='subject', **kwargs): |
|
336 |
""" |
|
337 |
:param peid: the parent entity's eid hosting the inline form |
|
338 |
:param rtype: the relation bridging `etype` and `peid` |
|
339 |
:param role: the role played by the `peid` in the relation |
|
340 |
""" |
|
341 |
entity = self.entity(row, col) |
|
342 |
divonclick = "restoreInlinedEntity('%s', '%s', '%s')" % (peid, rtype, |
|
343 |
entity.eid) |
|
344 |
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
|
345 |
|
1147 | 346 |
def render_form(self, entity, peid, rtype, role, **kwargs): |
347 |
"""fetch and render the form""" |
|
1297 | 348 |
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
|
349 |
entity=entity, set_error_url=False) |
1396 | 350 |
self.add_hiddens(form, entity, peid, rtype, role) |
351 |
divid = '%s-%s-%s' % (peid, rtype, entity.eid) |
|
352 |
title = self.schema.rschema(rtype).display_name(self.req, role) |
|
1147 | 353 |
removejs = self.removejs % (peid, rtype,entity.eid) |
1396 | 354 |
self.w(form.form_render(renderer=EntityInlinedFormRenderer(), divid=divid, |
355 |
title=title, removejs=removejs,**kwargs)) |
|
356 |
||
357 |
def add_hiddens(self, form, entity, peid, rtype, role): |
|
358 |
# to ease overriding (see cubes.vcsfile.views.forms for instance) |
|
1710
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1703
diff
changeset
|
359 |
if self.keep_entity(form, entity, peid, rtype): |
1147 | 360 |
if entity.has_eid(): |
361 |
rval = entity.eid |
|
362 |
else: |
|
363 |
rval = INTERNAL_FIELD_VALUE |
|
364 |
form.form_add_hidden('edit%s-%s:%s' % (role[0], rtype, peid), rval) |
|
365 |
form.form_add_hidden(name='%s:%s' % (rtype, peid), value=entity.eid, |
|
366 |
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
|
367 |
|
1710
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1703
diff
changeset
|
368 |
def keep_entity(self, form, entity, peid, rtype): |
1147 | 369 |
if not entity.has_eid(): |
370 |
return True |
|
371 |
# are we regenerating form because of a validation error ? |
|
1750 | 372 |
if form.form_previous_values: |
1710
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1703
diff
changeset
|
373 |
cdvalues = self.req.list_form_param(eid_param(rtype, peid), |
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1703
diff
changeset
|
374 |
form.form_previous_values) |
1147 | 375 |
if unicode(entity.eid) not in cdvalues: |
376 |
return False |
|
377 |
return True |
|
378 |
||
379 |
||
380 |
class InlineEntityCreationFormView(InlineEntityEditionFormView): |
|
381 |
id = 'inline-creation' |
|
382 |
__select__ = (match_kwargs('peid', 'rtype') |
|
383 |
& specified_etype_implements('Any')) |
|
1423
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
384 |
removejs = "removeInlineForm('%s', '%s', '%s')" |
39320a58faac
missing overriding of removejs in inline-creation form view
sylvain.thenault@logilab.fr
parents:
1396
diff
changeset
|
385 |
|
1147 | 386 |
def call(self, etype, peid, rtype, role='subject', **kwargs): |
387 |
""" |
|
388 |
:param etype: the entity type being created in the inline form |
|
389 |
:param peid: the parent entity's eid hosting the inline form |
|
390 |
:param rtype: the relation bridging `etype` and `peid` |
|
391 |
:param role: the role played by the `peid` in the relation |
|
392 |
""" |
|
393 |
try: |
|
394 |
entity = self.vreg.etype_class(etype)(self.req, None, None) |
|
395 |
except: |
|
396 |
self.w(self.req._('no such entity type %s') % etype) |
|
397 |
return |
|
398 |
self.initialize_varmaker() |
|
399 |
entity.eid = self.varmaker.next() |
|
400 |
self.render_form(entity, peid, rtype, role) |