author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Wed, 21 Jan 2009 19:28:03 +0100 | |
changeset 455 | c4b630e7d0f8 |
parent 445 | 18cc518d5e33 |
parent 454 | c35c318e6be5 |
child 526 | 498ff2e16fae |
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 ( |
445 | 17 |
etype_rtype_selector, one_line_rset, accept, has_relation, |
431 | 18 |
primary_view, match_context_prop, has_related_entities, |
445 | 19 |
_rql_condition) |
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' |
|
431 | 45 |
__selectors__ = Template.__selectors__ + (match_context_prop,) |
0 | 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 |
|
431 | 153 |
__selectors__ = (one_line_rset, primary_view, |
154 |
match_context_prop, etype_rtype_selector, |
|
445 | 155 |
has_relation, accept, _rql_condition) |
0 | 156 |
accepts = ('Any',) |
157 |
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
|
158 |
condition = None |
0 | 159 |
|
160 |
def call(self, row=0, col=0, **kwargs): |
|
161 |
"""classes inheriting from EntityBoxTemplate should defined cell_call, |
|
162 |
""" |
|
163 |
self.cell_call(row, col, **kwargs) |
|
164 |
||
165 |
||
250
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
166 |
class RelatedEntityBoxTemplate(EntityBoxTemplate): |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
167 |
__selectors__ = EntityBoxTemplate.__selectors__ + (has_related_entities,) |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
168 |
|
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
169 |
def cell_call(self, row, col, **kwargs): |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
170 |
entity = self.entity(row, col) |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
171 |
limit = self.req.property_value('navigation.related-limit') + 1 |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
172 |
role = get_role(self) |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
173 |
self.w(u'<div class="sideRelated">') |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
174 |
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
|
175 |
title=display_name(self.req, self.rtype, role)) |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
176 |
self.w(u'</div>') |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
177 |
|
0 | 178 |
|
179 |
class EditRelationBoxTemplate(ReloadableMixIn, EntityBoxTemplate): |
|
180 |
"""base class for boxes which let add or remove entities linked |
|
181 |
by a given relation |
|
182 |
||
183 |
subclasses should define at least id, rtype and target |
|
184 |
class attributes. |
|
185 |
""" |
|
186 |
||
454
c35c318e6be5
fix EditRelationBoxTemplate: the box might be passed an additional 'view' paraemter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
187 |
def cell_call(self, row, col, view=None): |
0 | 188 |
self.req.add_js('cubicweb.ajax.js') |
189 |
entity = self.entity(row, col) |
|
190 |
box = SideBoxWidget(display_name(self.req, self.rtype), self.id) |
|
191 |
count = self.w_related(box, entity) |
|
192 |
if count: |
|
193 |
box.append(BoxSeparator()) |
|
194 |
self.w_unrelated(box, entity) |
|
195 |
box.render(self.w) |
|
196 |
||
197 |
def div_id(self): |
|
198 |
return self.id |
|
199 |
||
200 |
@cached |
|
201 |
def xtarget(self): |
|
202 |
if self.target == 'subject': |
|
203 |
return 'object', 'subject' |
|
204 |
return 'subject', 'object' |
|
205 |
||
206 |
def box_item(self, entity, etarget, rql, label): |
|
207 |
"""builds HTML link to edit relation between `entity` and `etarget` |
|
208 |
""" |
|
209 |
x, target = self.xtarget() |
|
210 |
args = {x[0] : entity.eid, target[0] : etarget.eid} |
|
211 |
url = self.user_rql_callback((rql, args)) |
|
212 |
# for each target, provide a link to edit the relation |
|
213 |
label = u'[<a href="%s">%s</a>] %s' % (url, label, |
|
214 |
etarget.view('incontext')) |
|
215 |
return RawBoxItem(label, liclass=u'invisible') |
|
216 |
||
217 |
def w_related(self, box, entity): |
|
218 |
"""appends existing relations to the `box`""" |
|
219 |
rql = 'DELETE S %s O WHERE S eid %%(s)s, O eid %%(o)s' % self.rtype |
|
220 |
related = self.related_entities(entity) |
|
221 |
for etarget in related: |
|
222 |
box.append(self.box_item(entity, etarget, rql, u'-')) |
|
223 |
return len(related) |
|
224 |
||
225 |
def w_unrelated(self, box, entity): |
|
226 |
"""appends unrelated entities to the `box`""" |
|
227 |
rql = 'SET S %s O WHERE S eid %%(s)s, O eid %%(o)s' % self.rtype |
|
228 |
for etarget in self.unrelated_entities(entity): |
|
229 |
box.append(self.box_item(entity, etarget, rql, u'+')) |
|
230 |
||
231 |
def unrelated_entities(self, entity): |
|
232 |
"""returns the list of unrelated entities |
|
233 |
||
234 |
if etype is not defined on the Box's class, the default |
|
235 |
behaviour is to use the entity's appropraite vocabulary function |
|
236 |
""" |
|
237 |
x, target = self.xtarget() |
|
238 |
# use entity.unrelated if we've been asked for a particular etype |
|
239 |
if hasattr(self, 'etype'): |
|
240 |
return entity.unrelated(self.rtype, self.etype, x).entities() |
|
241 |
# in other cases, use vocabulary functions |
|
242 |
entities = [] |
|
243 |
for _, eid in entity.vocabulary(self.rtype, x): |
|
244 |
if eid is not None: |
|
245 |
rset = self.req.eid_rset(eid) |
|
246 |
entities.append(rset.get_entity(0, 0)) |
|
247 |
return entities |
|
248 |
||
249 |
def related_entities(self, entity): |
|
250 |
x, target = self.xtarget() |
|
251 |
return entity.related(self.rtype, x, entities=True) |
|
252 |