author | sylvain.thenault@logilab.fr |
Thu, 26 Mar 2009 20:11:20 +0100 | |
branch | tls-sprint |
changeset 1151 | b20677336ee6 |
parent 1147 | 402e8a8b1d6a |
child 1154 | 9b23a6836c32 |
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 import typed_eid |
|
17 |
from cubicweb.selectors import (match_kwargs, one_line_rset, non_final_entity, |
|
18 |
specified_etype_implements, yes) |
|
19 |
from cubicweb.rtags import RelationTags |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
20 |
from cubicweb.utils import make_uid |
1132 | 21 |
from cubicweb.view import EntityView |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
22 |
from cubicweb.common import tags |
1147 | 23 |
from cubicweb.web import INTERNAL_FIELD_VALUE, stdmsgs, formwidgets |
24 |
from cubicweb.web.form import CompositeForm, EntityFieldsForm, FormMixIn |
|
1132 | 25 |
from cubicweb.web.formfields import guess_field |
1147 | 26 |
from cubicweb.web.formrenderers import (FormRenderer, EntityFormRenderer, |
27 |
EntityCompositeFormRenderer, |
|
28 |
EntityInlinedFormRenderer) |
|
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) |
|
36 |
||
37 |
def toggable_relation_link(eid, nodeid, label='x'): |
|
38 |
"""return javascript snippet to delete/undelete a relation between two |
|
39 |
entities |
|
40 |
""" |
|
41 |
js = u"javascript: togglePendingDelete('%s', %s);" % (nodeid, html_escape(dumps(eid))) |
|
42 |
return u'[<a class="handle" href="%s" id="handle%s">%s</a>]' % (js, nodeid, label) |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
43 |
|
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 |
class DeleteConfForm(EntityView): |
1147 | 46 |
"""form used to confirm deletion of some entities""" |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
47 |
id = 'deleteconf' |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
48 |
title = _('delete') |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
49 |
domid = 'deleteconf' |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
50 |
onsubmit = None |
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 |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
54 |
|
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) ?')) |
1147 | 63 |
form = CompositeForm(req, domid='deleteconf', action=self.build_url('edit'), |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
64 |
onsubmit=self.onsubmit, copy_nav_params=True) |
1147 | 65 |
# XXX tabindex |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
66 |
form.buttons.append(form.button_delete(label=stdmsgs.YES)) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
67 |
form.buttons.append(form.button_cancel(label=stdmsgs.NO)) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
68 |
done = set() |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
69 |
w(u'<ul>\n') |
1147 | 70 |
for entity in self.rset.entities(): |
71 |
if entity.eid in done: |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
72 |
continue |
1147 | 73 |
done.add(entity.eid) |
74 |
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
|
75 |
form.form_add_subform(subform) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
76 |
# 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
|
77 |
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
|
78 |
href=entity.absolute_url())) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
79 |
w(u'</ul>\n') |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
80 |
w(form.form_render()) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
81 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
82 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
83 |
class ClickAndEditForm(FormMixIn, EntityView): |
1147 | 84 |
"""form used to permit ajax edition of an attribute of an entity in a view |
85 |
|
|
86 |
(double-click on the field to see an appropriate edition widget) |
|
87 |
""" |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
88 |
id = 'reledit' |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
89 |
__select__ = non_final_entity() & match_kwargs('rtype') |
1147 | 90 |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
91 |
# FIXME editableField class could be toggleable from userprefs |
1147 | 92 |
|
93 |
onsubmit = ("return inlineValidateForm('%(divid)s-form', '%(rtype)s', " |
|
94 |
"'%(eid)s', '%(divid)s', %(reload)s);") |
|
95 |
ondblclick = "showInlineEditionForm(%(eid)s, '%(rtype)s', '%(divid)s')" |
|
96 |
||
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
97 |
def cell_call(self, row, col, rtype=None, role='subject', reload=False): |
1147 | 98 |
"""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
|
99 |
entity = self.entity(row, col) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
100 |
if getattr(entity, rtype) is None: |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
101 |
value = self.req._('not specified') |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
102 |
else: |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
103 |
value = entity.printable_value(rtype) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
104 |
if not entity.has_perm('update'): |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
105 |
self.w(value) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
106 |
return |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
107 |
self.req.add_js( ('cubicweb.ajax.js',) ) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
108 |
eid = entity.eid |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
109 |
edit_key = make_uid('%s-%s' % (rtype, eid)) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
110 |
divid = 'd%s' % edit_key |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
111 |
reload = dumps(reload) |
1147 | 112 |
# XXX tab index |
113 |
buttons = [tags.input(klass="validateButton", type="submit", |
|
114 |
name="__action_apply", |
|
115 |
value=self.req._(stdmsgs.BUTTON_OK), |
|
116 |
tabindex=self.req.next_tabindex()), |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
117 |
tags.input(klass="validateButton", type="button", |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
118 |
value=self.req._(stdmsgs.BUTTON_CANCEL), |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
119 |
onclick="cancelInlineEdit(%s,\'%s\',\'%s\')" % (eid, rtype, divid), |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
120 |
tabindex=self.req.next_tabindex())] |
1147 | 121 |
form = self.vreg.select_object('forms', 'edition', self.req, self.rset, |
122 |
row=row, col=col, buttons=buttons, |
|
123 |
domid='%s-form' % divid, action='#', |
|
124 |
cssstyle='display: none', |
|
125 |
onsubmit=self.onsubmit % locals()) |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
126 |
renderer = FormRenderer(display_label=False, display_help=False, |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
127 |
display_fields=(rtype,), button_bar_class='buttonbar') |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
128 |
self.w(tags.div(value, klass='editableField', id=divid, |
1147 | 129 |
ondblclick=self.ondblclick % locals())) |
130 |
self.w(form.form_render(renderer=renderer)) |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
131 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
132 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
133 |
class AutomaticEntityForm(EntityFieldsForm): |
1147 | 134 |
"""base automatic form to edit any entity |
135 |
||
136 |
Designed to be flly generated from schema but highly configurable through: |
|
137 |
* rtags (rcategories, rwidgets, inlined, rpermissions) |
|
138 |
* various standard form parameters |
|
139 |
||
140 |
You can also easily customise it by adding/removing fields in |
|
141 |
AutomaticEntityForm instances. |
|
142 |
""" |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
143 |
id = 'edition' |
1147 | 144 |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
145 |
needs_js = EntityFieldsForm.needs_js + ('cubicweb.ajax.js',) |
1147 | 146 |
cwtarget = 'eformframe' |
147 |
cssclass = 'entityForm' |
|
148 |
copy_nav_params = True |
|
149 |
attrcategories = ('primary', 'secondary') |
|
150 |
||
151 |
# relations'category (eg primary/secondary/generic/metadata/generated) |
|
152 |
rcategories = RelationTags() |
|
153 |
# use primary and not generated for eid since it has to be an hidden |
|
154 |
rcategories.set_rtag('primary', 'subject', 'eid') |
|
155 |
rcategories.set_rtag('metadata', 'subject', 'creation_date') |
|
156 |
rcategories.set_rtag('metadata', 'subject', 'modification_date') |
|
1151
b20677336ee6
turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
157 |
rcategories.set_rtag('generated', 'subject', 'has_text') |
b20677336ee6
turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
158 |
rcategories.set_rtag('metadata', 'owned_by', 'subject') |
b20677336ee6
turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
159 |
rcategories.set_rtag('metadata', 'created_by', 'subject') |
b20677336ee6
turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
160 |
rcategories.set_rtag('generated', 'is', 'subject') |
b20677336ee6
turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
161 |
rcategories.set_rtag('generated', 'is', 'object') |
b20677336ee6
turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
162 |
rcategories.set_rtag('generated', 'is_instance_of', 'subject') |
b20677336ee6
turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
163 |
rcategories.set_rtag('generated', 'is_instance_of', 'object') |
b20677336ee6
turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
164 |
rcategories.set_rtag('generated', 'identity', 'subject') |
b20677336ee6
turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
165 |
rcategories.set_rtag('generated', 'identity', 'object') |
b20677336ee6
turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
166 |
rcategories.set_rtag('generated', 'require_permission', 'subject') |
b20677336ee6
turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
167 |
rcategories.set_rtag('primary', 'in_state', 'subject') |
b20677336ee6
turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
168 |
rcategories.set_rtag('generated', 'wf_info_for', 'subject') |
b20677336ee6
turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
169 |
rcategories.set_rtag('generated', 'wf_info_for', 'subject') |
b20677336ee6
turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
170 |
rcategories.set_rtag('secondary', 'description', 'subject') |
1147 | 171 |
|
172 |
# relations'widget (eg one of available class name in cubicweb.web.formwidgets) |
|
173 |
rwidgets = RelationTags() |
|
174 |
# inlined view flag for non final relations |
|
1151
b20677336ee6
turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
175 |
rinlined = RelationTags() |
1147 | 176 |
# set of tags of the form <action>_on_new on relations. <action> is a |
177 |
# schema action (add/update/delete/read), and when such a tag is found |
|
178 |
# permissions checking is by-passed and supposed to be ok |
|
179 |
rpermissions_overrides = RelationTags(use_set=True) |
|
180 |
||
181 |
@classmethod |
|
182 |
def registered(cls, registry): |
|
183 |
"""build class using descriptor at registration time""" |
|
184 |
super(AutomaticEntityForm, cls).registered(registry) |
|
185 |
cls.init_rtags_category() |
|
186 |
return cls |
|
187 |
||
188 |
@classmethod |
|
189 |
def init_rtags_category(cls): |
|
190 |
"""set default category tags for relations where it's not yet defined in |
|
191 |
the category relation tags |
|
192 |
""" |
|
193 |
for eschema in cls.schema.entities(): |
|
194 |
for rschema, tschemas, role in eschema.relation_definitions(True): |
|
195 |
for tschema in tschemas: |
|
196 |
if role == 'subject': |
|
197 |
X, Y = eschema, tschema |
|
198 |
card = rschema.rproperty(X, Y, 'cardinality')[0] |
|
199 |
composed = rschema.rproperty(X, Y, 'composite') == 'object' |
|
200 |
else: |
|
201 |
X, Y = tschema, eschema |
|
202 |
card = rschema.rproperty(X, Y, 'cardinality')[1] |
|
203 |
composed = rschema.rproperty(X, Y, 'composite') == 'subject' |
|
204 |
if not cls.rcategories.rtag(role, rschema, X, Y): |
|
205 |
if card in '1+': |
|
206 |
if not rschema.is_final() and composed: |
|
207 |
category = 'generated' |
|
208 |
else: |
|
209 |
category = 'primary' |
|
210 |
elif rschema.is_final(): |
|
211 |
category = 'secondary' |
|
212 |
else: |
|
213 |
category = 'generic' |
|
214 |
cls.rcategories.set_rtag(category, role, rschema, X, Y) |
|
215 |
||
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
216 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
217 |
def __init__(self, *args, **kwargs): |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
218 |
super(AutomaticEntityForm, self).__init__(*args, **kwargs) |
1147 | 219 |
if self.edited_entity.has_eid(): |
220 |
self.edited_entity.complete() |
|
221 |
for rschema, role in self.editable_attributes(): |
|
222 |
wdgname = self.rwidgets.etype_rtag(self.edited_entity.id, role, rschema) |
|
223 |
if wdgname: |
|
224 |
field = guess_field(self.edited_entity.__class__, rschema, role, |
|
225 |
eidparam=True, widget=getattr(formwidgets, wdgname)) |
|
226 |
else: |
|
227 |
field = guess_field(self.edited_entity.__class__, rschema, role, |
|
228 |
eidparam=True) |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
229 |
self.fields.append(field) |
1147 | 230 |
|
231 |
def action(self): |
|
232 |
"""return the form's action attribute""" |
|
233 |
try: |
|
234 |
return self._action |
|
235 |
except AttributeError: |
|
236 |
return self.build_url('validateform') |
|
237 |
||
238 |
def set_action(self, value): |
|
239 |
self._action = value |
|
240 |
||
241 |
action = property(action, set_action) |
|
242 |
||
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
243 |
def form_buttons(self): |
1147 | 244 |
"""return the form's buttons (as string)""" |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
245 |
return [self.button_ok(tabindex=self.req.next_tabindex()), |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
246 |
self.button_apply(tabindex=self.req.next_tabindex()), |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
247 |
self.button_cancel(tabindex=self.req.next_tabindex())] |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
248 |
|
1147 | 249 |
def editable_attributes(self): |
250 |
"""return a list of (relation schema, role) to edit for the entity |
|
251 |
""" |
|
252 |
return [(rschema, x) for rschema, _, x in self.relations_by_category(self.attrcategories, 'add') |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
253 |
if rschema != 'eid'] |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
254 |
|
1147 | 255 |
def relations_by_category(self, categories=None, permission=None): |
256 |
"""return a list of (relation schema, target schemas, role) matching |
|
257 |
categories and permission |
|
258 |
""" |
|
259 |
if categories is not None: |
|
260 |
if not isinstance(categories, (list, tuple, set, frozenset)): |
|
261 |
categories = (categories,) |
|
262 |
if not isinstance(categories, (set, frozenset)): |
|
263 |
categories = frozenset(categories) |
|
264 |
eschema = self.edited_entity.e_schema |
|
265 |
rtags = self.rcategories |
|
266 |
permsoverrides = self.rpermissions_overrides |
|
267 |
if self.edited_entity.has_eid(): |
|
268 |
eid = self.edited_entity.eid |
|
269 |
else: |
|
270 |
eid = None |
|
271 |
for rschema, targetschemas, role in eschema.relation_definitions(True): |
|
272 |
if rschema in ('identity', 'has_text'): |
|
273 |
continue |
|
274 |
# check category first, potentially lower cost than checking |
|
275 |
# permission which may imply rql queries |
|
276 |
if categories is not None: |
|
277 |
targetschemas = [tschema for tschema in targetschemas |
|
278 |
if rtags.etype_rtag(eschema, role, rschema, tschema) in categories] |
|
279 |
if not targetschemas: |
|
280 |
continue |
|
281 |
if permission is not None: |
|
282 |
# tag allowing to hijack the permission machinery when |
|
283 |
# permission is not verifiable until the entity is actually |
|
284 |
# created... |
|
285 |
if eid is None and '%s_on_new' % permission in permsoverrides.etype_rtags(eschema, role, rschema): |
|
286 |
yield (rschema, targetschemas, role) |
|
287 |
continue |
|
288 |
if rschema.is_final(): |
|
289 |
if not rschema.has_perm(self.req, permission, eid): |
|
290 |
continue |
|
291 |
elif role == 'subject': |
|
292 |
if not ((eid is None and rschema.has_local_role(permission)) or |
|
293 |
rschema.has_perm(self.req, permission, fromeid=eid)): |
|
294 |
continue |
|
295 |
# on relation with cardinality 1 or ?, we need delete perm as well |
|
296 |
# if the relation is already set |
|
297 |
if (permission == 'add' |
|
298 |
and rschema.cardinality(eschema, targetschemas[0], role) in '1?' |
|
299 |
and eid and self.edited_entity.related(rschema.type, role) |
|
300 |
and not rschema.has_perm(self.req, 'delete', fromeid=eid, |
|
301 |
toeid=self.edited_entity.related(rschema.type, role)[0][0])): |
|
302 |
continue |
|
303 |
elif role == 'object': |
|
304 |
if not ((eid is None and rschema.has_local_role(permission)) or |
|
305 |
rschema.has_perm(self.req, permission, toeid=eid)): |
|
306 |
continue |
|
307 |
# on relation with cardinality 1 or ?, we need delete perm as well |
|
308 |
# if the relation is already set |
|
309 |
if (permission == 'add' |
|
310 |
and rschema.cardinality(targetschemas[0], eschema, role) in '1?' |
|
311 |
and eid and self.edited_entity.related(rschema.type, role) |
|
312 |
and not rschema.has_perm(self.req, 'delete', toeid=eid, |
|
313 |
fromeid=self.edited_entity.related(rschema.type, role)[0][0])): |
|
314 |
continue |
|
315 |
yield (rschema, targetschemas, role) |
|
316 |
||
317 |
def srelations_by_category(self, categories=None, permission=None): |
|
318 |
"""filter out result of relations_by_category(categories, permission) by |
|
319 |
removing final relations |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
320 |
|
1147 | 321 |
return a list of (relation's label, relation'schema, role) |
322 |
""" |
|
323 |
result = [] |
|
324 |
for rschema, ttypes, role in self.relations_by_category(categories, |
|
325 |
permission): |
|
326 |
if rschema.is_final(): |
|
327 |
continue |
|
328 |
result.append( (rschema.display_name(self.req, role), rschema, role) ) |
|
329 |
return sorted(result) |
|
330 |
||
331 |
def relations_table(self): |
|
332 |
"""yiels 3-tuples (rtype, target, related_list) |
|
333 |
where <related_list> itself a list of : |
|
334 |
- node_id (will be the entity element's DOM id) |
|
335 |
- appropriate javascript's togglePendingDelete() function call |
|
336 |
- status 'pendingdelete' or '' |
|
337 |
- oneline view of related entity |
|
338 |
""" |
|
339 |
entity = self.edited_entity |
|
340 |
pending_deletes = self.req.get_pending_deletes(entity.eid) |
|
1151
b20677336ee6
turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
341 |
for label, rschema, role in self.srelations_by_category('generic'), 'add'): |
1147 | 342 |
relatedrset = entity.related(rschema, role, limit=self.limit) |
343 |
if rschema.has_perm(self.req, 'delete'): |
|
344 |
toggable_rel_link_func = toggable_relation_link |
|
345 |
else: |
|
346 |
toggable_rel_link_func = lambda x, y, z: u'' |
|
347 |
related = [] |
|
348 |
for row in xrange(relatedrset.rowcount): |
|
349 |
nodeid = relation_id(entity.eid, rschema, role, |
|
350 |
relatedrset[row][0]) |
|
351 |
if nodeid in pending_deletes: |
|
352 |
status = u'pendingDelete' |
|
353 |
label = '+' |
|
354 |
else: |
|
355 |
status = u'' |
|
356 |
label = 'x' |
|
357 |
dellink = toggable_rel_link_func(entity.eid, nodeid, label) |
|
358 |
eview = self.view('oneline', relatedrset, row=row) |
|
359 |
related.append((nodeid, dellink, status, eview)) |
|
360 |
yield (rschema, role, related) |
|
361 |
||
362 |
def restore_pending_inserts(self, cell=False): |
|
363 |
"""used to restore edition page as it was before clicking on |
|
364 |
'search for <some entity type>' |
|
365 |
""" |
|
366 |
eid = self.edited_entity.eid |
|
367 |
cell = cell and "div_insert_" or "tr" |
|
368 |
pending_inserts = set(self.req.get_pending_inserts(eid)) |
|
369 |
for pendingid in pending_inserts: |
|
370 |
eidfrom, rtype, eidto = pendingid.split(':') |
|
371 |
if typed_eid(eidfrom) == eid: # subject |
|
372 |
label = display_name(self.req, rtype, 'subject') |
|
373 |
reid = eidto |
|
374 |
else: |
|
375 |
label = display_name(self.req, rtype, 'object') |
|
376 |
reid = eidfrom |
|
377 |
jscall = "javascript: cancelPendingInsert('%s', '%s', null, %s);" \ |
|
378 |
% (pendingid, cell, eid) |
|
379 |
rset = self.req.eid_rset(reid) |
|
380 |
eview = self.view('text', rset, row=0) |
|
381 |
# XXX find a clean way to handle baskets |
|
382 |
if rset.description[0][0] == 'Basket': |
|
383 |
eview = '%s (%s)' % (eview, display_name(self.req, 'Basket')) |
|
384 |
yield rtype, pendingid, jscall, label, reid, eview |
|
385 |
||
386 |
# should_* method extracted to allow overriding |
|
387 |
||
388 |
def should_inline_relation_form(self, rschema, targettype, role): |
|
389 |
"""return true if the given relation with entity has role and a |
|
390 |
targettype target should be inlined |
|
391 |
""" |
|
1151
b20677336ee6
turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
392 |
return self.rinlined.etype_rtag(self.edited_entity.id, role, rschema, targettype) |
1147 | 393 |
|
394 |
def should_display_inline_creation_form(self, rschema, existant, card): |
|
395 |
"""return true if a creation form should be inlined |
|
396 |
||
397 |
by default true if there is no related entity and we need at least one |
|
398 |
""" |
|
399 |
return not existant and card in '1+' |
|
400 |
||
401 |
def should_display_add_new_relation_link(self, rschema, existant, card): |
|
402 |
"""return true if we should add a link to add a new creation form |
|
403 |
(through ajax call) |
|
404 |
||
405 |
by default true if there is no related entity or if the relation has |
|
406 |
multiple cardinality |
|
407 |
""" |
|
408 |
return not existant or card in '+*' |
|
409 |
||
410 |
||
411 |
class EditionFormView(EntityView): |
|
412 |
"""display primary entity edition form""" |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
413 |
id = 'edition' |
1147 | 414 |
# add yes() so it takes precedence over deprecated views in baseforms, |
415 |
# though not baseforms based customized view |
|
416 |
__select__ = one_line_rset() & non_final_entity() & yes() |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
417 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
418 |
title = _('edition') |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
419 |
controller = 'edit' |
1147 | 420 |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
421 |
def cell_call(self, row, col, **kwargs): |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
422 |
entity = self.complete_entity(row, col) |
1147 | 423 |
self.render_form(entity) |
424 |
||
425 |
def render_form(self, entity): |
|
426 |
"""fetch and render the form""" |
|
427 |
self.form_title(entity) |
|
428 |
form = self.vreg.select_object('forms', 'edition', self.req, self.rset, |
|
429 |
row=self.row, col=self.col, entity=entity, |
|
430 |
domid=self.id, submitmsg=self.submited_message()) |
|
431 |
self.init_form(form, entity) |
|
432 |
self.w(form.form_render(renderer=EntityFormRenderer(), formvid=u'edition')) |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
433 |
|
1147 | 434 |
def init_form(self, form, entity): |
435 |
"""customize your form before rendering here""" |
|
436 |
form.form_add_hidden(u'__maineid', entity.eid) |
|
437 |
||
438 |
def form_title(self, entity): |
|
439 |
"""the form view title""" |
|
440 |
ptitle = self.req._(self.title) |
|
441 |
self.w(u'<div class="formTitle"><span>%s %s</span></div>' % ( |
|
442 |
entity.dc_type(), ptitle and '(%s)' % ptitle)) |
|
443 |
||
444 |
def submited_message(self): |
|
445 |
"""return the message that will be displayed on successful edition""" |
|
446 |
return self.req._('entity edited') |
|
447 |
||
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
448 |
|
1147 | 449 |
class CreationFormView(EditionFormView): |
450 |
"""display primary entity creation form""" |
|
451 |
id = 'creation' |
|
452 |
__select__ = specified_etype_implements('Any') & yes() |
|
453 |
||
454 |
title = _('creation') |
|
455 |
||
456 |
def call(self, **kwargs): |
|
457 |
"""creation view for an entity""" |
|
458 |
etype = kwargs.pop('etype', self.req.form.get('etype')) |
|
459 |
try: |
|
460 |
entity = self.vreg.etype_class(etype)(self.req, None, None) |
|
461 |
except: |
|
462 |
self.w(self.req._('no such entity type %s') % etype) |
|
463 |
else: |
|
464 |
self.initialize_varmaker() |
|
465 |
entity.eid = self.varmaker.next() |
|
466 |
self.render_form(entity) |
|
467 |
||
468 |
def form_title(self, entity): |
|
469 |
"""the form view title""" |
|
470 |
if '__linkto' in self.req.form: |
|
471 |
if isinstance(self.req.form['__linkto'], list): |
|
472 |
# XXX which one should be considered (case: add a ticket to a |
|
473 |
# version in jpl) |
|
474 |
rtype, linkto_eid, role = self.req.form['__linkto'][0].split(':') |
|
475 |
else: |
|
476 |
rtype, linkto_eid, role = self.req.form['__linkto'].split(':') |
|
477 |
linkto_rset = self.req.eid_rset(linkto_eid) |
|
478 |
linkto_type = linkto_rset.description[0][0] |
|
479 |
if role == 'subject': |
|
480 |
title = self.req.__('creating %s (%s %s %s %%(linkto)s)' % ( |
|
481 |
entity.e_schema, entity.e_schema, rtype, linkto_type)) |
|
482 |
else: |
|
483 |
title = self.req.__('creating %s (%s %%(linkto)s %s %s)' % ( |
|
484 |
entity.e_schema, linkto_type, rtype, entity.e_schema)) |
|
485 |
msg = title % {'linkto' : self.view('incontext', linkto_rset)} |
|
486 |
self.w(u'<div class="formTitle notransform"><span>%s</span></div>' % msg) |
|
487 |
else: |
|
488 |
super(CreationFormView, self).form_title(entity) |
|
489 |
||
490 |
def url(self): |
|
491 |
"""return the url associated with this view""" |
|
492 |
return self.create_url(self.req.form.get('etype')) |
|
493 |
||
494 |
def submited_message(self): |
|
495 |
"""return the message that will be displayed on successful edition""" |
|
496 |
return self.req._('entity created') |
|
497 |
||
498 |
||
499 |
class CopyFormView(EditionFormView): |
|
500 |
"""display primary entity creation form initialized with values from another |
|
501 |
entity |
|
502 |
""" |
|
503 |
id = 'copy' |
|
504 |
def render_form(self, entity): |
|
505 |
"""fetch and render the form""" |
|
506 |
# make a copy of entity to avoid altering the entity in the |
|
507 |
# request's cache. |
|
508 |
self.newentity = copy(entity) |
|
509 |
self.copying = self.newentity.eid |
|
510 |
self.newentity.eid = None |
|
511 |
self.w(u'<script type="text/javascript">updateMessage("%s");</script>\n' |
|
512 |
% self.req._('Please note that this is only a shallow copy')) |
|
513 |
super(CopyFormView, self).render_form(entity) |
|
514 |
del self.newentity |
|
515 |
||
516 |
def init_form(self, form, entity): |
|
517 |
"""customize your form before rendering here""" |
|
518 |
super(CopyFormView, self).init_form(form, entity) |
|
519 |
if entity.eid == self.newentity.eid: |
|
520 |
form.form_add_hidden('__cloned_eid', self.copying, eidparam=True) |
|
521 |
||
522 |
def submited_message(self): |
|
523 |
"""return the message that will be displayed on successful edition""" |
|
524 |
return self.req._('entity copied') |
|
525 |
||
526 |
||
527 |
class TableEditForm(CompositeForm): |
|
528 |
id = 'muledit' |
|
529 |
onsubmit = "return validateForm('entityForm', null);" |
|
530 |
||
531 |
def __init__(self, *args, **kwargs): |
|
532 |
super(TableEditForm, self).__init__(*args, **kwargs) |
|
533 |
for row in xrange(len(self.rset)): |
|
534 |
form = self.vreg.select_object('forms', 'edition', self.req, self.rset, |
|
535 |
row=row, domid=self.id, |
|
536 |
attrcategories=('primary',), |
|
537 |
set_error_url=False) |
|
538 |
# XXX rely on the MultipleEntityFormRenderer to put the eid input |
|
539 |
form.remove_field(form.field_by_name('eid')) |
|
540 |
self.form_add_subform(form) |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
541 |
|
1147 | 542 |
def form_buttons(self): |
543 |
"""return the form's buttons (as string)""" |
|
544 |
okt = self.req._('validate modifications on selected items').capitalize() |
|
545 |
resett = self.req._('revert changes').capitalize() |
|
546 |
return [self.button_ok(title=okt), self.button_reset(title=resett)] |
|
547 |
||
548 |
||
549 |
class TableEditFormView(EntityView): |
|
550 |
id = 'muledit' |
|
551 |
__select__ = EntityView.__select__ & yes() |
|
552 |
title = _('multiple edit') |
|
553 |
||
554 |
def call(self, **kwargs): |
|
555 |
"""a view to edit multiple entities of the same type the first column |
|
556 |
should be the eid |
|
557 |
""" |
|
558 |
#self.form_title(entity) |
|
559 |
form = self.vreg.select_object('forms', self.id, self.req, self.rset, |
|
560 |
domid=self.id) |
|
561 |
self.w(form.form_render(renderer=EntityCompositeFormRenderer())) |
|
562 |
||
563 |
||
564 |
class InlineEntityEditionFormView(EntityView): |
|
565 |
id = 'inline-edition' |
|
566 |
__select__ = non_final_entity() & match_kwargs('peid', 'rtype') |
|
567 |
removejs = "removeInlinedEntity('%s', '%s', '%s')" |
|
568 |
||
569 |
def call(self, **kwargs): |
|
570 |
"""redefine default call() method to avoid automatic |
|
571 |
insertions of <div class="section"> between each row of |
|
572 |
the resultset |
|
573 |
""" |
|
574 |
rset = self.rset |
|
575 |
for i in xrange(len(rset)): |
|
576 |
self.wview(self.id, rset, row=i, **kwargs) |
|
577 |
||
578 |
def cell_call(self, row, col, peid, rtype, role='subject', **kwargs): |
|
579 |
""" |
|
580 |
:param peid: the parent entity's eid hosting the inline form |
|
581 |
:param rtype: the relation bridging `etype` and `peid` |
|
582 |
:param role: the role played by the `peid` in the relation |
|
583 |
""" |
|
584 |
entity = self.entity(row, col) |
|
585 |
divonclick = "restoreInlinedEntity('%s', '%s', '%s')" % (peid, rtype, |
|
586 |
entity.eid) |
|
587 |
self.render_form(entity, peid, rtype, role, divonclick=divonclick) |
|
588 |
||
589 |
def render_form(self, entity, peid, rtype, role, **kwargs): |
|
590 |
"""fetch and render the form""" |
|
591 |
rschema = self.schema.rschema(rtype) |
|
592 |
divid = '%s-%s-%s' % (peid, rtype, entity.eid) |
|
593 |
title = rschema.display_name(self.req, role) |
|
594 |
form = self.vreg.select_object('forms', 'edition', self.req, |
|
595 |
entity=entity) |
|
596 |
removejs = self.removejs % (peid, rtype,entity.eid) |
|
597 |
if self.keep_entity(entity, peid, rtype): |
|
598 |
if entity.has_eid(): |
|
599 |
rval = entity.eid |
|
600 |
else: |
|
601 |
rval = INTERNAL_FIELD_VALUE |
|
602 |
form.form_add_hidden('edit%s-%s:%s' % (role[0], rtype, peid), rval) |
|
603 |
form.form_add_hidden(name='%s:%s' % (rtype, peid), value=entity.eid, |
|
604 |
id='rel-%s-%s-%s' % (peid, rtype, entity.eid)) |
|
605 |
self.w(form.form_render(renderer=EntityInlinedFormRenderer(), divid=divid, |
|
606 |
title=title, removejs=removejs,**kwargs)) |
|
607 |
||
608 |
def keep_entity(self, entity, peid, rtype): |
|
609 |
if not entity.has_eid(): |
|
610 |
return True |
|
611 |
# are we regenerating form because of a validation error ? |
|
612 |
erroneous_post = self.req.data.get('formvalues') |
|
613 |
if erroneous_post: |
|
614 |
cdvalues = self.req.list_form_param('%s:%s' % (rtype, peid), |
|
615 |
erroneous_post) |
|
616 |
if unicode(entity.eid) not in cdvalues: |
|
617 |
return False |
|
618 |
return True |
|
619 |
||
620 |
||
621 |
class InlineEntityCreationFormView(InlineEntityEditionFormView): |
|
622 |
id = 'inline-creation' |
|
623 |
__select__ = (match_kwargs('peid', 'rtype') |
|
624 |
& specified_etype_implements('Any')) |
|
625 |
||
626 |
def call(self, etype, peid, rtype, role='subject', **kwargs): |
|
627 |
""" |
|
628 |
:param etype: the entity type being created in the inline form |
|
629 |
:param peid: the parent entity's eid hosting the inline form |
|
630 |
:param rtype: the relation bridging `etype` and `peid` |
|
631 |
:param role: the role played by the `peid` in the relation |
|
632 |
""" |
|
633 |
try: |
|
634 |
entity = self.vreg.etype_class(etype)(self.req, None, None) |
|
635 |
except: |
|
636 |
self.w(self.req._('no such entity type %s') % etype) |
|
637 |
return |
|
638 |
self.initialize_varmaker() |
|
639 |
entity.eid = self.varmaker.next() |
|
640 |
self.render_form(entity, peid, rtype, role) |