author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Sat, 06 Feb 2010 10:34:35 +0100 | |
branch | stable |
changeset 4486 | 9a4ef69bdef7 |
parent 4212 | ab6573088b4a |
child 4252 | 6c4f109c2b03 |
permissions | -rw-r--r-- |
0 | 1 |
"""abstract box classes for CubicWeb web client |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4205
diff
changeset
|
4 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1885
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
2159 | 9 |
_ = unicode |
0 | 10 |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2159
diff
changeset
|
11 |
from logilab.mtconverter import xml_escape |
0 | 12 |
|
1149 | 13 |
from cubicweb import Unauthorized, role as get_role, target as get_target |
2710
40789c3044f3
[web.box] remove deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
14 |
from cubicweb.schema import display_name |
652
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
640
diff
changeset
|
15 |
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
|
16 |
match_context_prop, partial_has_related_entities, |
816
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
809
diff
changeset
|
17 |
accepts_compat, has_relation_compat, |
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
809
diff
changeset
|
18 |
condition_compat, require_group_compat) |
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
809
diff
changeset
|
19 |
from cubicweb.view import View, 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 |
||
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 |
1493 | 28 |
|
0 | 29 |
actions. Various classes attributes may be used to control the box |
30 |
rendering. |
|
1493 | 31 |
|
0 | 32 |
You may override on of the formatting callbacks is this is not necessary |
33 |
for your custom box. |
|
1493 | 34 |
|
0 | 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() |
1181
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1149
diff
changeset
|
42 |
registered = classmethod(require_group_compat(View.registered)) |
1493 | 43 |
|
0 | 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: |
|
3220
11b6016e3970
cleanup, futur warning fixes :)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2757
diff
changeset
|
63 |
actions_by_cat.setdefault(action.category, []).append( |
11b6016e3970
cleanup, futur warning fixes :)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2757
diff
changeset
|
64 |
(action.title, action) ) |
0 | 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: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2159
diff
changeset
|
79 |
title = xml_escape(title) |
0 | 80 |
return self.box_action(self._action(title, path, **kwargs)) |
1493 | 81 |
|
0 | 82 |
def _action(self, title, path, **kwargs): |
1493 | 83 |
return UnregisteredAction(self.req, self.rset, title, path, **kwargs) |
0 | 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)) |
|
1493 | 96 |
|
0 | 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. |
|
1493 | 101 |
|
0 | 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 |
""" |
|
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 |
|
1493 | 109 |
|
0 | 110 |
def to_display_rql(self): |
111 |
assert self.rql is not None, self.id |
|
112 |
return (self.rql,) |
|
1493 | 113 |
|
0 | 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 |
||
1493 | 128 |
|
0 | 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') |
|
1493 | 137 |
|
0 | 138 |
|
139 |
class EntityBoxTemplate(BoxTemplate): |
|
140 |
"""base class for boxes related to a single entity""" |
|
809 | 141 |
__select__ = BoxTemplate.__select__ & one_line_rset() & primary_view() |
773 | 142 |
registered = accepts_compat(has_relation_compat(condition_compat(BoxTemplate.registered))) |
0 | 143 |
context = 'incontext' |
1493 | 144 |
|
0 | 145 |
def call(self, row=0, col=0, **kwargs): |
526 | 146 |
"""classes inheriting from EntityBoxTemplate should define cell_call""" |
0 | 147 |
self.cell_call(row, col, **kwargs) |
148 |
||
149 |
||
250
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
150 |
class RelatedEntityBoxTemplate(EntityBoxTemplate): |
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
816
diff
changeset
|
151 |
__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
|
152 |
|
250
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
153 |
def cell_call(self, row, col, **kwargs): |
3220
11b6016e3970
cleanup, futur warning fixes :)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2757
diff
changeset
|
154 |
entity = self.rset.get_entity(row, col) |
250
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
155 |
limit = self.req.property_value('navigation.related-limit') + 1 |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
156 |
role = get_role(self) |
1885
c2011d238e98
replace sideRelated with sideBox
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1696
diff
changeset
|
157 |
self.w(u'<div class="sideBox">') |
250
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
158 |
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
|
159 |
title=display_name(self.req, self.rtype, role)) |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
160 |
self.w(u'</div>') |
7fd7a0d387d7
new RelatedEntityBoxTemplate base class
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
161 |
|
0 | 162 |
|
163 |
class EditRelationBoxTemplate(ReloadableMixIn, EntityBoxTemplate): |
|
164 |
"""base class for boxes which let add or remove entities linked |
|
165 |
by a given relation |
|
166 |
||
167 |
subclasses should define at least id, rtype and target |
|
168 |
class attributes. |
|
169 |
""" |
|
1493 | 170 |
|
1624
baf484a182cd
should take arbitrary arguments
sylvain.thenault@logilab.fr
parents:
1493
diff
changeset
|
171 |
def cell_call(self, row, col, view=None, **kwargs): |
0 | 172 |
self.req.add_js('cubicweb.ajax.js') |
3220
11b6016e3970
cleanup, futur warning fixes :)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2757
diff
changeset
|
173 |
entity = self.rset.get_entity(row, col) |
0 | 174 |
box = SideBoxWidget(display_name(self.req, self.rtype), self.id) |
2757
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
175 |
related = self.related_boxitems(entity) |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
176 |
unrelated = self.unrelated_boxitems(entity) |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
177 |
box.extend(related) |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
178 |
if related and unrelated: |
0 | 179 |
box.append(BoxSeparator()) |
2757
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
180 |
box.extend(unrelated) |
0 | 181 |
box.render(self.w) |
182 |
||
183 |
def div_id(self): |
|
184 |
return self.id |
|
1493 | 185 |
|
0 | 186 |
def box_item(self, entity, etarget, rql, label): |
187 |
"""builds HTML link to edit relation between `entity` and `etarget` |
|
188 |
""" |
|
1149 | 189 |
role, target = get_role(self), get_target(self) |
190 |
args = {role[0] : entity.eid, target[0] : etarget.eid} |
|
0 | 191 |
url = self.user_rql_callback((rql, args)) |
192 |
# for each target, provide a link to edit the relation |
|
4205
4458c7cc193b
must escape user_rql_callback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3220
diff
changeset
|
193 |
label = u'[<a href="%s">%s</a>] %s' % (xml_escape(url), label, |
0 | 194 |
etarget.view('incontext')) |
195 |
return RawBoxItem(label, liclass=u'invisible') |
|
1493 | 196 |
|
2757
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
197 |
def related_boxitems(self, entity): |
0 | 198 |
rql = 'DELETE S %s O WHERE S eid %%(s)s, O eid %%(o)s' % self.rtype |
2757
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
199 |
related = [] |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
200 |
for etarget in self.related_entities(entity): |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
201 |
related.append(self.box_item(entity, etarget, rql, u'-')) |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
202 |
return related |
1493 | 203 |
|
2757
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
204 |
def unrelated_boxitems(self, entity): |
0 | 205 |
rql = 'SET S %s O WHERE S eid %%(s)s, O eid %%(o)s' % self.rtype |
2757
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
206 |
unrelated = [] |
0 | 207 |
for etarget in self.unrelated_entities(entity): |
2757
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
208 |
unrelated.append(self.box_item(entity, etarget, rql, u'+')) |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
209 |
return unrelated |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
210 |
|
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
211 |
def related_entities(self, entity): |
c8e28e1754f0
B [web.box] fix EditRelationBoxTemplate in case neither related nor unrelated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2710
diff
changeset
|
212 |
return entity.related(self.rtype, get_role(self), entities=True) |
0 | 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 |
# use entity.unrelated if we've been asked for a particular etype |
|
221 |
if hasattr(self, 'etype'): |
|
1149 | 222 |
return entity.unrelated(self.rtype, self.etype, get_role(self)).entities() |
0 | 223 |
# in other cases, use vocabulary functions |
224 |
entities = [] |
|
2710
40789c3044f3
[web.box] remove deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
225 |
form = self.vreg['forms'].select('edition', self.req, rset=self.rset, |
40789c3044f3
[web.box] remove deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
226 |
row=self.row or 0) |
1695
9a9822f3cb6c
update implementation to get vocab from form instead of entity
sylvain.thenault@logilab.fr
parents:
1624
diff
changeset
|
227 |
field = form.field_by_name(self.rtype, get_role(self), entity.e_schema) |
9a9822f3cb6c
update implementation to get vocab from form instead of entity
sylvain.thenault@logilab.fr
parents:
1624
diff
changeset
|
228 |
for _, eid in form.form_field_vocabulary(field): |
0 | 229 |
if eid is not None: |
230 |
rset = self.req.eid_rset(eid) |
|
231 |
entities.append(rset.get_entity(0, 0)) |
|
232 |
return entities |
|
1493 | 233 |