author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 18 Aug 2009 09:25:26 +0200 | |
changeset 2901 | 8f4d495c94c2 |
parent 2819 | b864288fd316 |
child 3230 | 1d25e928c299 |
permissions | -rw-r--r-- |
0 | 1 |
"""abstract box classes for CubicWeb web client |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1885
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1885
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
2159 | 9 |
_ = unicode |
0 | 10 |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2159
diff
changeset
|
11 |
from logilab.mtconverter import xml_escape |
0 | 12 |
|
1149 | 13 |
from cubicweb import Unauthorized, role as get_role, target as get_target |
2710
40789c3044f3
[web.box] remove deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
14 |
from cubicweb.schema import display_name |
652
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
640
diff
changeset
|
15 |
from cubicweb.selectors import (one_line_rset, primary_view, |
2819
b864288fd316
remove more 3.2 deprecated code, reintroduce checkbox used by formrenderers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2799
diff
changeset
|
16 |
match_context_prop, partial_has_related_entities) |
816
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
809
diff
changeset
|
17 |
from cubicweb.view import View, ReloadableMixIn |
0 | 18 |
|
19 |
from cubicweb.web.htmlwidgets import (BoxLink, BoxWidget, SideBoxWidget, |
|
250
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
20 |
RawBoxItem, BoxSeparator) |
0 | 21 |
from cubicweb.web.action import UnregisteredAction |
22 |
||
23 |
||
816
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
809
diff
changeset
|
24 |
class BoxTemplate(View): |
0 | 25 |
"""base template for boxes, usually a (contextual) list of possible |
1493 | 26 |
|
0 | 27 |
actions. Various classes attributes may be used to control the box |
28 |
rendering. |
|
1493 | 29 |
|
0 | 30 |
You may override on of the formatting callbacks is this is not necessary |
31 |
for your custom box. |
|
1493 | 32 |
|
0 | 33 |
Classes inheriting from this class usually only have to override call |
34 |
to fetch desired actions, and then to do something like :: |
|
35 |
||
36 |
box.render(self.w) |
|
37 |
""" |
|
38 |
__registry__ = 'boxes' |
|
809 | 39 |
__select__ = match_context_prop() |
1493 | 40 |
|
0 | 41 |
categories_in_order = () |
2799
b703639614e7
refactor property handling to avoid name conflicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2789
diff
changeset
|
42 |
cw_property_defs = { |
0 | 43 |
_('visible'): dict(type='Boolean', default=True, |
44 |
help=_('display the box or not')), |
|
45 |
_('order'): dict(type='Int', default=99, |
|
46 |
help=_('display order of the box')), |
|
47 |
# XXX 'incontext' boxes are handled by the default primary view |
|
48 |
_('context'): dict(type='String', default='left', |
|
49 |
vocabulary=(_('left'), _('incontext'), _('right')), |
|
50 |
help=_('context where this box should be displayed')), |
|
51 |
} |
|
52 |
context = 'left' |
|
53 |
htmlitemclass = 'boxItem' |
|
54 |
||
55 |
def sort_actions(self, actions): |
|
56 |
"""return a list of (category, actions_sorted_by_title)""" |
|
57 |
result = [] |
|
58 |
actions_by_cat = {} |
|
59 |
for action in actions: |
|
60 |
actions_by_cat.setdefault(action.category, []).append((action.title, action)) |
|
61 |
for key, values in actions_by_cat.items(): |
|
62 |
actions_by_cat[key] = [act for title, act in sorted(values)] |
|
63 |
for cat in self.categories_in_order: |
|
64 |
if cat in actions_by_cat: |
|
65 |
result.append( (cat, actions_by_cat[cat]) ) |
|
66 |
for item in sorted(actions_by_cat.items()): |
|
67 |
result.append(item) |
|
68 |
return result |
|
69 |
||
70 |
def mk_action(self, title, path, escape=True, **kwargs): |
|
71 |
"""factory function to create dummy actions compatible with the |
|
72 |
.format_actions method |
|
73 |
""" |
|
74 |
if escape: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2159
diff
changeset
|
75 |
title = xml_escape(title) |
0 | 76 |
return self.box_action(self._action(title, path, **kwargs)) |
1493 | 77 |
|
0 | 78 |
def _action(self, title, path, **kwargs): |
1493 | 79 |
return UnregisteredAction(self.req, self.rset, title, path, **kwargs) |
0 | 80 |
|
81 |
# formating callbacks |
|
82 |
||
83 |
def boxitem_link_tooltip(self, action): |
|
84 |
if action.id: |
|
85 |
return u'keyword: %s' % action.id |
|
86 |
return u'' |
|
87 |
||
88 |
def box_action(self, action): |
|
89 |
cls = getattr(action, 'html_class', lambda: None)() or self.htmlitemclass |
|
90 |
return BoxLink(action.url(), self.req._(action.title), |
|
91 |
cls, self.boxitem_link_tooltip(action)) |
|
1493 | 92 |
|
0 | 93 |
|
94 |
class RQLBoxTemplate(BoxTemplate): |
|
95 |
"""abstract box for boxes displaying the content of a rql query not |
|
96 |
related to the current result set. |
|
1493 | 97 |
|
0 | 98 |
It rely on etype, rtype (both optional, usable to control registration |
99 |
according to application schema and display according to connected |
|
100 |
user's rights) and rql attributes |
|
101 |
""" |
|
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
526
diff
changeset
|
102 |
#XXX __selectors__ = BoxTemplate.__selectors__ + (etype_rtype_selector,) |
0 | 103 |
|
104 |
rql = None |
|
1493 | 105 |
|
0 | 106 |
def to_display_rql(self): |
107 |
assert self.rql is not None, self.id |
|
108 |
return (self.rql,) |
|
1493 | 109 |
|
0 | 110 |
def call(self, **kwargs): |
111 |
try: |
|
112 |
rset = self.req.execute(*self.to_display_rql()) |
|
113 |
except Unauthorized: |
|
114 |
# can't access to something in the query, forget this box |
|
115 |
return |
|
116 |
if len(rset) == 0: |
|
117 |
return |
|
118 |
box = BoxWidget(self.req._(self.title), self.id) |
|
119 |
for i, (teid, tname) in enumerate(rset): |
|
120 |
entity = rset.get_entity(i, 0) |
|
121 |
box.append(self.mk_action(tname, entity.absolute_url())) |
|
122 |
box.render(w=self.w) |
|
123 |
||
1493 | 124 |
|
0 | 125 |
class UserRQLBoxTemplate(RQLBoxTemplate): |
126 |
"""same as rql box template but the rql is build using the eid of the |
|
127 |
request's user |
|
128 |
""" |
|
129 |
||
130 |
def to_display_rql(self): |
|
131 |
assert self.rql is not None, self.id |
|
132 |
return (self.rql, {'x': self.req.user.eid}, 'x') |
|
1493 | 133 |
|
0 | 134 |
|
135 |
class EntityBoxTemplate(BoxTemplate): |
|
136 |
"""base class for boxes related to a single entity""" |
|
809 | 137 |
__select__ = BoxTemplate.__select__ & one_line_rset() & primary_view() |
0 | 138 |
context = 'incontext' |
1493 | 139 |
|
0 | 140 |
def call(self, row=0, col=0, **kwargs): |
526 | 141 |
"""classes inheriting from EntityBoxTemplate should define cell_call""" |
0 | 142 |
self.cell_call(row, col, **kwargs) |
143 |
||
144 |
||
250
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
145 |
class RelatedEntityBoxTemplate(EntityBoxTemplate): |
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
816
diff
changeset
|
146 |
__select__ = EntityBoxTemplate.__select__ & partial_has_related_entities() |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
670
diff
changeset
|
147 |
|
250
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
148 |
def cell_call(self, row, col, **kwargs): |
2789
39712da6f397
R propagate deprecation of AppObject.entity()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2757
diff
changeset
|
149 |
entity = self.rset.get_entity(row, col) |
250
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
150 |
limit = self.req.property_value('navigation.related-limit') + 1 |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
151 |
role = get_role(self) |
1885
c2011d238e98
replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1696
diff
changeset
|
152 |
self.w(u'<div class="sideBox">') |
250
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
153 |
self.wview('sidebox', entity.related(self.rtype, role, limit=limit), |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
154 |
title=display_name(self.req, self.rtype, role)) |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
155 |
self.w(u'</div>') |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
156 |
|
0 | 157 |
|
158 |
class EditRelationBoxTemplate(ReloadableMixIn, EntityBoxTemplate): |
|
159 |
"""base class for boxes which let add or remove entities linked |
|
160 |
by a given relation |
|
161 |
||
162 |
subclasses should define at least id, rtype and target |
|
163 |
class attributes. |
|
164 |
""" |
|
1493 | 165 |
|
1624
baf484a182cd
should take arbitrary arguments
sylvain.thenault@logilab.fr
parents:
1493
diff
changeset
|
166 |
def cell_call(self, row, col, view=None, **kwargs): |
0 | 167 |
self.req.add_js('cubicweb.ajax.js') |
2789
39712da6f397
R propagate deprecation of AppObject.entity()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2757
diff
changeset
|
168 |
entity = self.rset.get_entity(row, col) |
0 | 169 |
box = SideBoxWidget(display_name(self.req, self.rtype), self.id) |
2757
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
170 |
related = self.related_boxitems(entity) |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
171 |
unrelated = self.unrelated_boxitems(entity) |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
172 |
box.extend(related) |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
173 |
if related and unrelated: |
0 | 174 |
box.append(BoxSeparator()) |
2757
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
175 |
box.extend(unrelated) |
0 | 176 |
box.render(self.w) |
177 |
||
178 |
def div_id(self): |
|
179 |
return self.id |
|
1493 | 180 |
|
0 | 181 |
def box_item(self, entity, etarget, rql, label): |
182 |
"""builds HTML link to edit relation between `entity` and `etarget` |
|
183 |
""" |
|
1149 | 184 |
role, target = get_role(self), get_target(self) |
185 |
args = {role[0] : entity.eid, target[0] : etarget.eid} |
|
0 | 186 |
url = self.user_rql_callback((rql, args)) |
187 |
# for each target, provide a link to edit the relation |
|
188 |
label = u'[<a href="%s">%s</a>] %s' % (url, label, |
|
189 |
etarget.view('incontext')) |
|
190 |
return RawBoxItem(label, liclass=u'invisible') |
|
1493 | 191 |
|
2757
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
192 |
def related_boxitems(self, entity): |
0 | 193 |
rql = 'DELETE S %s O WHERE S eid %%(s)s, O eid %%(o)s' % self.rtype |
2757
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
194 |
related = [] |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
195 |
for etarget in self.related_entities(entity): |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
196 |
related.append(self.box_item(entity, etarget, rql, u'-')) |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
197 |
return related |
1493 | 198 |
|
2757
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
199 |
def unrelated_boxitems(self, entity): |
0 | 200 |
rql = 'SET S %s O WHERE S eid %%(s)s, O eid %%(o)s' % self.rtype |
2757
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
201 |
unrelated = [] |
0 | 202 |
for etarget in self.unrelated_entities(entity): |
2757
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
203 |
unrelated.append(self.box_item(entity, etarget, rql, u'+')) |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
204 |
return unrelated |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
205 |
|
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
206 |
def related_entities(self, entity): |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
207 |
return entity.related(self.rtype, get_role(self), entities=True) |
0 | 208 |
|
209 |
def unrelated_entities(self, entity): |
|
210 |
"""returns the list of unrelated entities |
|
211 |
||
212 |
if etype is not defined on the Box's class, the default |
|
213 |
behaviour is to use the entity's appropraite vocabulary function |
|
214 |
""" |
|
215 |
# use entity.unrelated if we've been asked for a particular etype |
|
216 |
if hasattr(self, 'etype'): |
|
1149 | 217 |
return entity.unrelated(self.rtype, self.etype, get_role(self)).entities() |
0 | 218 |
# in other cases, use vocabulary functions |
219 |
entities = [] |
|
2710
40789c3044f3
[web.box] remove deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
220 |
form = self.vreg['forms'].select('edition', self.req, rset=self.rset, |
40789c3044f3
[web.box] remove deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
221 |
row=self.row or 0) |
1695
9a9822f3cb6c
update implementation to get vocab from form instead of entity
sylvain.thenault@logilab.fr
parents:
1624
diff
changeset
|
222 |
field = form.field_by_name(self.rtype, get_role(self), entity.e_schema) |
9a9822f3cb6c
update implementation to get vocab from form instead of entity
sylvain.thenault@logilab.fr
parents:
1624
diff
changeset
|
223 |
for _, eid in form.form_field_vocabulary(field): |
0 | 224 |
if eid is not None: |
225 |
rset = self.req.eid_rset(eid) |
|
226 |
entities.append(rset.get_entity(0, 0)) |
|
227 |
return entities |
|
1493 | 228 |