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