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