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