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