author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Wed, 07 Jan 2009 12:25:18 +0100 | |
changeset 344 | e0d5879a613e |
parent 250 | 7fd7a0d387d7 |
child 431 | 18b4dd650ef8 |
permissions | -rw-r--r-- |
0 | 1 |
"""abstract box classes for CubicWeb web client |
2 |
||
3 |
:organization: Logilab |
|
4 |
:copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
9 |
from logilab.common.decorators import cached |
|
10 |
from logilab.mtconverter import html_escape |
|
11 |
||
250
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
12 |
from cubicweb import Unauthorized, role as get_role |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
13 |
from cubicweb.common.registerers import ( |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
14 |
accepts_registerer, extresources_registerer, |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
15 |
etype_rtype_priority_registerer) |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
16 |
from cubicweb.common.selectors import ( |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
17 |
etype_rtype_selector, one_line_rset, accept_selector, accept_rtype_selector, |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
18 |
primaryview_selector, contextprop_selector, has_related_entities, |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
19 |
_rqlcondition_selector) |
0 | 20 |
from cubicweb.common.view import Template |
21 |
from cubicweb.common.appobject import ReloadableMixIn |
|
22 |
||
23 |
from cubicweb.web.htmlwidgets import (BoxLink, BoxWidget, SideBoxWidget, |
|
250
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
24 |
RawBoxItem, BoxSeparator) |
0 | 25 |
from cubicweb.web.action import UnregisteredAction |
26 |
||
27 |
_ = unicode |
|
28 |
||
29 |
||
30 |
class BoxTemplate(Template): |
|
31 |
"""base template for boxes, usually a (contextual) list of possible |
|
32 |
|
|
33 |
actions. Various classes attributes may be used to control the box |
|
34 |
rendering. |
|
35 |
|
|
36 |
You may override on of the formatting callbacks is this is not necessary |
|
37 |
for your custom box. |
|
38 |
|
|
39 |
Classes inheriting from this class usually only have to override call |
|
40 |
to fetch desired actions, and then to do something like :: |
|
41 |
||
42 |
box.render(self.w) |
|
43 |
""" |
|
44 |
__registry__ = 'boxes' |
|
45 |
__selectors__ = Template.__selectors__ + (contextprop_selector,) |
|
46 |
||
47 |
categories_in_order = () |
|
48 |
property_defs = { |
|
49 |
_('visible'): dict(type='Boolean', default=True, |
|
50 |
help=_('display the box or not')), |
|
51 |
_('order'): dict(type='Int', default=99, |
|
52 |
help=_('display order of the box')), |
|
53 |
# XXX 'incontext' boxes are handled by the default primary view |
|
54 |
_('context'): dict(type='String', default='left', |
|
55 |
vocabulary=(_('left'), _('incontext'), _('right')), |
|
56 |
help=_('context where this box should be displayed')), |
|
57 |
} |
|
58 |
context = 'left' |
|
59 |
htmlitemclass = 'boxItem' |
|
60 |
||
61 |
def sort_actions(self, actions): |
|
62 |
"""return a list of (category, actions_sorted_by_title)""" |
|
63 |
result = [] |
|
64 |
actions_by_cat = {} |
|
65 |
for action in actions: |
|
66 |
actions_by_cat.setdefault(action.category, []).append((action.title, action)) |
|
67 |
for key, values in actions_by_cat.items(): |
|
68 |
actions_by_cat[key] = [act for title, act in sorted(values)] |
|
69 |
for cat in self.categories_in_order: |
|
70 |
if cat in actions_by_cat: |
|
71 |
result.append( (cat, actions_by_cat[cat]) ) |
|
72 |
for item in sorted(actions_by_cat.items()): |
|
73 |
result.append(item) |
|
74 |
return result |
|
75 |
||
76 |
def mk_action(self, title, path, escape=True, **kwargs): |
|
77 |
"""factory function to create dummy actions compatible with the |
|
78 |
.format_actions method |
|
79 |
""" |
|
80 |
if escape: |
|
81 |
title = html_escape(title) |
|
82 |
return self.box_action(self._action(title, path, **kwargs)) |
|
83 |
||
84 |
def _action(self, title, path, **kwargs): |
|
85 |
return UnregisteredAction(self.req, self.rset, title, path, **kwargs) |
|
86 |
||
87 |
# formating callbacks |
|
88 |
||
89 |
def boxitem_link_tooltip(self, action): |
|
90 |
if action.id: |
|
91 |
return u'keyword: %s' % action.id |
|
92 |
return u'' |
|
93 |
||
94 |
def box_action(self, action): |
|
95 |
cls = getattr(action, 'html_class', lambda: None)() or self.htmlitemclass |
|
96 |
return BoxLink(action.url(), self.req._(action.title), |
|
97 |
cls, self.boxitem_link_tooltip(action)) |
|
98 |
||
99 |
||
100 |
class RQLBoxTemplate(BoxTemplate): |
|
101 |
"""abstract box for boxes displaying the content of a rql query not |
|
102 |
related to the current result set. |
|
103 |
|
|
104 |
It rely on etype, rtype (both optional, usable to control registration |
|
105 |
according to application schema and display according to connected |
|
106 |
user's rights) and rql attributes |
|
107 |
""" |
|
108 |
__registerer__ = etype_rtype_priority_registerer |
|
109 |
__selectors__ = BoxTemplate.__selectors__ + (etype_rtype_selector,) |
|
110 |
||
111 |
rql = None |
|
112 |
||
113 |
def to_display_rql(self): |
|
114 |
assert self.rql is not None, self.id |
|
115 |
return (self.rql,) |
|
116 |
||
117 |
def call(self, **kwargs): |
|
118 |
try: |
|
119 |
rset = self.req.execute(*self.to_display_rql()) |
|
120 |
except Unauthorized: |
|
121 |
# can't access to something in the query, forget this box |
|
122 |
return |
|
123 |
if len(rset) == 0: |
|
124 |
return |
|
125 |
box = BoxWidget(self.req._(self.title), self.id) |
|
126 |
for i, (teid, tname) in enumerate(rset): |
|
127 |
entity = rset.get_entity(i, 0) |
|
128 |
box.append(self.mk_action(tname, entity.absolute_url())) |
|
129 |
box.render(w=self.w) |
|
130 |
||
131 |
||
132 |
class UserRQLBoxTemplate(RQLBoxTemplate): |
|
133 |
"""same as rql box template but the rql is build using the eid of the |
|
134 |
request's user |
|
135 |
""" |
|
136 |
||
137 |
def to_display_rql(self): |
|
138 |
assert self.rql is not None, self.id |
|
139 |
return (self.rql, {'x': self.req.user.eid}, 'x') |
|
140 |
||
141 |
||
142 |
class ExtResourcesBoxTemplate(BoxTemplate): |
|
143 |
"""base class for boxes displaying external resources such as the RSS logo. |
|
144 |
It should list necessary resources with the .need_resources attribute. |
|
145 |
""" |
|
146 |
__registerer__ = extresources_registerer |
|
147 |
need_resources = () |
|
148 |
||
149 |
||
150 |
class EntityBoxTemplate(BoxTemplate): |
|
151 |
"""base class for boxes related to a single entity""" |
|
152 |
__registerer__ = accepts_registerer |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
183
diff
changeset
|
153 |
__selectors__ = (one_line_rset, primaryview_selector, |
0 | 154 |
contextprop_selector, etype_rtype_selector, |
175
5c7bb5f1ede0
add _rql_condition selector to default EntityBox selectors
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
155 |
accept_rtype_selector, accept_selector, |
5c7bb5f1ede0
add _rql_condition selector to default EntityBox selectors
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
156 |
_rqlcondition_selector) |
0 | 157 |
accepts = ('Any',) |
158 |
context = 'incontext' |
|
183
fe4d6025f712
if EntityBoxTemplate now uses _rqlcondition_selector, it must also define a default condition attribute
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
175
diff
changeset
|
159 |
condition = None |
0 | 160 |
|
161 |
def call(self, row=0, col=0, **kwargs): |
|
162 |
"""classes inheriting from EntityBoxTemplate should defined cell_call, |
|
163 |
""" |
|
164 |
self.cell_call(row, col, **kwargs) |
|
165 |
||
166 |
||
250
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
167 |
class RelatedEntityBoxTemplate(EntityBoxTemplate): |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
168 |
__selectors__ = EntityBoxTemplate.__selectors__ + (has_related_entities,) |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
169 |
|
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
170 |
def cell_call(self, row, col, **kwargs): |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
171 |
entity = self.entity(row, col) |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
172 |
limit = self.req.property_value('navigation.related-limit') + 1 |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
173 |
role = get_role(self) |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
174 |
self.w(u'<div class="sideRelated">') |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
175 |
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
|
176 |
title=display_name(self.req, self.rtype, role)) |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
177 |
self.w(u'</div>') |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
178 |
|
0 | 179 |
|
180 |
class EditRelationBoxTemplate(ReloadableMixIn, EntityBoxTemplate): |
|
181 |
"""base class for boxes which let add or remove entities linked |
|
182 |
by a given relation |
|
183 |
||
184 |
subclasses should define at least id, rtype and target |
|
185 |
class attributes. |
|
186 |
""" |
|
187 |
||
188 |
def cell_call(self, row, col): |
|
189 |
self.req.add_js('cubicweb.ajax.js') |
|
190 |
entity = self.entity(row, col) |
|
191 |
box = SideBoxWidget(display_name(self.req, self.rtype), self.id) |
|
192 |
count = self.w_related(box, entity) |
|
193 |
if count: |
|
194 |
box.append(BoxSeparator()) |
|
195 |
self.w_unrelated(box, entity) |
|
196 |
box.render(self.w) |
|
197 |
||
198 |
def div_id(self): |
|
199 |
return self.id |
|
200 |
||
201 |
@cached |
|
202 |
def xtarget(self): |
|
203 |
if self.target == 'subject': |
|
204 |
return 'object', 'subject' |
|
205 |
return 'subject', 'object' |
|
206 |
||
207 |
def box_item(self, entity, etarget, rql, label): |
|
208 |
"""builds HTML link to edit relation between `entity` and `etarget` |
|
209 |
""" |
|
210 |
x, target = self.xtarget() |
|
211 |
args = {x[0] : entity.eid, target[0] : etarget.eid} |
|
212 |
url = self.user_rql_callback((rql, args)) |
|
213 |
# for each target, provide a link to edit the relation |
|
214 |
label = u'[<a href="%s">%s</a>] %s' % (url, label, |
|
215 |
etarget.view('incontext')) |
|
216 |
return RawBoxItem(label, liclass=u'invisible') |
|
217 |
||
218 |
def w_related(self, box, entity): |
|
219 |
"""appends existing relations to the `box`""" |
|
220 |
rql = 'DELETE S %s O WHERE S eid %%(s)s, O eid %%(o)s' % self.rtype |
|
221 |
related = self.related_entities(entity) |
|
222 |
for etarget in related: |
|
223 |
box.append(self.box_item(entity, etarget, rql, u'-')) |
|
224 |
return len(related) |
|
225 |
||
226 |
def w_unrelated(self, box, entity): |
|
227 |
"""appends unrelated entities to the `box`""" |
|
228 |
rql = 'SET S %s O WHERE S eid %%(s)s, O eid %%(o)s' % self.rtype |
|
229 |
for etarget in self.unrelated_entities(entity): |
|
230 |
box.append(self.box_item(entity, etarget, rql, u'+')) |
|
231 |
||
232 |
def unrelated_entities(self, entity): |
|
233 |
"""returns the list of unrelated entities |
|
234 |
||
235 |
if etype is not defined on the Box's class, the default |
|
236 |
behaviour is to use the entity's appropraite vocabulary function |
|
237 |
""" |
|
238 |
x, target = self.xtarget() |
|
239 |
# use entity.unrelated if we've been asked for a particular etype |
|
240 |
if hasattr(self, 'etype'): |
|
241 |
return entity.unrelated(self.rtype, self.etype, x).entities() |
|
242 |
# in other cases, use vocabulary functions |
|
243 |
entities = [] |
|
244 |
for _, eid in entity.vocabulary(self.rtype, x): |
|
245 |
if eid is not None: |
|
246 |
rset = self.req.eid_rset(eid) |
|
247 |
entities.append(rset.get_entity(0, 0)) |
|
248 |
return entities |
|
249 |
||
250 |
def related_entities(self, entity): |
|
251 |
x, target = self.xtarget() |
|
252 |
return entity.related(self.rtype, x, entities=True) |
|
253 |