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