author | sylvain.thenault@logilab.fr |
Thu, 12 Mar 2009 16:33:47 +0100 | |
branch | tls-sprint |
changeset 1096 | e1fe98850bf7 |
parent 838 | f2c56312b03a |
child 1149 | 1e19b6ef53a1 |
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, |
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
816
diff
changeset
|
14 |
match_context_prop, partial_has_related_entities, |
816
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
809
diff
changeset
|
15 |
accepts_compat, has_relation_compat, |
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
809
diff
changeset
|
16 |
condition_compat, require_group_compat) |
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
809
diff
changeset
|
17 |
from cubicweb.view import View, ReloadableMixIn |
0 | 18 |
|
19 |
from cubicweb.web.htmlwidgets import (BoxLink, BoxWidget, SideBoxWidget, |
|
250
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
20 |
RawBoxItem, BoxSeparator) |
0 | 21 |
from cubicweb.web.action import UnregisteredAction |
22 |
||
23 |
_ = unicode |
|
24 |
||
25 |
||
816
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
809
diff
changeset
|
26 |
class BoxTemplate(View): |
0 | 27 |
"""base template for boxes, usually a (contextual) list of possible |
28 |
|
|
29 |
actions. Various classes attributes may be used to control the box |
|
30 |
rendering. |
|
31 |
|
|
32 |
You may override on of the formatting callbacks is this is not necessary |
|
33 |
for your custom box. |
|
34 |
|
|
35 |
Classes inheriting from this class usually only have to override call |
|
36 |
to fetch desired actions, and then to do something like :: |
|
37 |
||
38 |
box.render(self.w) |
|
39 |
""" |
|
40 |
__registry__ = 'boxes' |
|
809 | 41 |
__select__ = match_context_prop() |
816
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
809
diff
changeset
|
42 |
registered = require_group_compat(View.registered) |
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 |
""" |
|
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
526
diff
changeset
|
105 |
#XXX __selectors__ = BoxTemplate.__selectors__ + (etype_rtype_selector,) |
0 | 106 |
|
107 |
rql = None |
|
108 |
||
109 |
def to_display_rql(self): |
|
110 |
assert self.rql is not None, self.id |
|
111 |
return (self.rql,) |
|
112 |
||
113 |
def call(self, **kwargs): |
|
114 |
try: |
|
115 |
rset = self.req.execute(*self.to_display_rql()) |
|
116 |
except Unauthorized: |
|
117 |
# can't access to something in the query, forget this box |
|
118 |
return |
|
119 |
if len(rset) == 0: |
|
120 |
return |
|
121 |
box = BoxWidget(self.req._(self.title), self.id) |
|
122 |
for i, (teid, tname) in enumerate(rset): |
|
123 |
entity = rset.get_entity(i, 0) |
|
124 |
box.append(self.mk_action(tname, entity.absolute_url())) |
|
125 |
box.render(w=self.w) |
|
126 |
||
127 |
||
128 |
class UserRQLBoxTemplate(RQLBoxTemplate): |
|
129 |
"""same as rql box template but the rql is build using the eid of the |
|
130 |
request's user |
|
131 |
""" |
|
132 |
||
133 |
def to_display_rql(self): |
|
134 |
assert self.rql is not None, self.id |
|
135 |
return (self.rql, {'x': self.req.user.eid}, 'x') |
|
136 |
||
137 |
||
138 |
class EntityBoxTemplate(BoxTemplate): |
|
139 |
"""base class for boxes related to a single entity""" |
|
809 | 140 |
__select__ = BoxTemplate.__select__ & one_line_rset() & primary_view() |
773 | 141 |
registered = accepts_compat(has_relation_compat(condition_compat(BoxTemplate.registered))) |
0 | 142 |
context = 'incontext' |
143 |
||
144 |
def call(self, row=0, col=0, **kwargs): |
|
526 | 145 |
"""classes inheriting from EntityBoxTemplate should define cell_call""" |
0 | 146 |
self.cell_call(row, col, **kwargs) |
147 |
||
148 |
||
250
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
149 |
class RelatedEntityBoxTemplate(EntityBoxTemplate): |
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
816
diff
changeset
|
150 |
__select__ = EntityBoxTemplate.__select__ & partial_has_related_entities() |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
670
diff
changeset
|
151 |
|
250
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
152 |
def cell_call(self, row, col, **kwargs): |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
153 |
entity = self.entity(row, col) |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
154 |
limit = self.req.property_value('navigation.related-limit') + 1 |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
155 |
role = get_role(self) |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
156 |
self.w(u'<div class="sideRelated">') |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
157 |
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
|
158 |
title=display_name(self.req, self.rtype, role)) |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
159 |
self.w(u'</div>') |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
160 |
|
0 | 161 |
|
162 |
class EditRelationBoxTemplate(ReloadableMixIn, EntityBoxTemplate): |
|
163 |
"""base class for boxes which let add or remove entities linked |
|
164 |
by a given relation |
|
165 |
||
166 |
subclasses should define at least id, rtype and target |
|
167 |
class attributes. |
|
168 |
""" |
|
169 |
||
454
c35c318e6be5
fix EditRelationBoxTemplate: the box might be passed an additional 'view' paraemter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
170 |
def cell_call(self, row, col, view=None): |
0 | 171 |
self.req.add_js('cubicweb.ajax.js') |
172 |
entity = self.entity(row, col) |
|
173 |
box = SideBoxWidget(display_name(self.req, self.rtype), self.id) |
|
174 |
count = self.w_related(box, entity) |
|
175 |
if count: |
|
176 |
box.append(BoxSeparator()) |
|
177 |
self.w_unrelated(box, entity) |
|
178 |
box.render(self.w) |
|
179 |
||
180 |
def div_id(self): |
|
181 |
return self.id |
|
182 |
||
183 |
@cached |
|
184 |
def xtarget(self): |
|
185 |
if self.target == 'subject': |
|
186 |
return 'object', 'subject' |
|
187 |
return 'subject', 'object' |
|
188 |
||
189 |
def box_item(self, entity, etarget, rql, label): |
|
190 |
"""builds HTML link to edit relation between `entity` and `etarget` |
|
191 |
""" |
|
192 |
x, target = self.xtarget() |
|
193 |
args = {x[0] : entity.eid, target[0] : etarget.eid} |
|
194 |
url = self.user_rql_callback((rql, args)) |
|
195 |
# for each target, provide a link to edit the relation |
|
196 |
label = u'[<a href="%s">%s</a>] %s' % (url, label, |
|
197 |
etarget.view('incontext')) |
|
198 |
return RawBoxItem(label, liclass=u'invisible') |
|
199 |
||
200 |
def w_related(self, box, entity): |
|
201 |
"""appends existing relations to the `box`""" |
|
202 |
rql = 'DELETE S %s O WHERE S eid %%(s)s, O eid %%(o)s' % self.rtype |
|
203 |
related = self.related_entities(entity) |
|
204 |
for etarget in related: |
|
205 |
box.append(self.box_item(entity, etarget, rql, u'-')) |
|
206 |
return len(related) |
|
207 |
||
208 |
def w_unrelated(self, box, entity): |
|
209 |
"""appends unrelated entities to the `box`""" |
|
210 |
rql = 'SET S %s O WHERE S eid %%(s)s, O eid %%(o)s' % self.rtype |
|
211 |
for etarget in self.unrelated_entities(entity): |
|
212 |
box.append(self.box_item(entity, etarget, rql, u'+')) |
|
213 |
||
214 |
def unrelated_entities(self, entity): |
|
215 |
"""returns the list of unrelated entities |
|
216 |
||
217 |
if etype is not defined on the Box's class, the default |
|
218 |
behaviour is to use the entity's appropraite vocabulary function |
|
219 |
""" |
|
220 |
x, target = self.xtarget() |
|
221 |
# use entity.unrelated if we've been asked for a particular etype |
|
222 |
if hasattr(self, 'etype'): |
|
223 |
return entity.unrelated(self.rtype, self.etype, x).entities() |
|
224 |
# in other cases, use vocabulary functions |
|
225 |
entities = [] |
|
226 |
for _, eid in entity.vocabulary(self.rtype, x): |
|
227 |
if eid is not None: |
|
228 |
rset = self.req.eid_rset(eid) |
|
229 |
entities.append(rset.get_entity(0, 0)) |
|
230 |
return entities |
|
231 |
||
232 |
def related_entities(self, entity): |
|
233 |
x, target = self.xtarget() |
|
234 |
return entity.related(self.rtype, x, entities=True) |
|
235 |