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