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