author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 21 May 2010 13:34:52 +0200 | |
changeset 5562 | 4ccd599b5cf0 |
parent 5557 | 1a534c596bff |
child 5569 | cb14af012a96 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5309
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5309
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5309
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5309
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5309
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5309
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5309
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5309
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5309
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5309
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5309
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5309
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5309
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5309
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5309
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5309
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5309
e8567135a927
[doc/book] fix docstrings, add notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4719
diff
changeset
|
18 |
"""mixins of entity/views organized somewhat in a graph or tree structure""" |
0 | 19 |
__docformat__ = "restructuredtext en" |
20 |
||
4401
4d973c834eb3
missing import
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4395
diff
changeset
|
21 |
from itertools import chain |
4d973c834eb3
missing import
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4395
diff
changeset
|
22 |
|
0 | 23 |
from logilab.common.decorators import cached |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
24 |
from logilab.common.deprecation import deprecated, class_deprecated |
0 | 25 |
|
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
471
diff
changeset
|
26 |
from cubicweb.selectors import implements |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
27 |
from cubicweb.interfaces import ITree |
0 | 28 |
|
29 |
||
30 |
class TreeMixIn(object): |
|
5309
e8567135a927
[doc/book] fix docstrings, add notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4719
diff
changeset
|
31 |
"""base tree-mixin implementing the tree interface |
0 | 32 |
|
33 |
This mixin has to be inherited explicitly and configured using the |
|
34 |
tree_attribute, parent_target and children_target class attribute to |
|
35 |
benefit from this default implementation |
|
36 |
""" |
|
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
37 |
__metaclass__ = class_deprecated |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
38 |
__deprecation_warning__ = '[3.9] TreeMixIn is deprecated, use/override ITreeAdapter instead' |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
39 |
|
0 | 40 |
tree_attribute = None |
41 |
# XXX misnamed |
|
42 |
parent_target = 'subject' |
|
43 |
children_target = 'object' |
|
1451 | 44 |
|
0 | 45 |
def different_type_children(self, entities=True): |
46 |
"""return children entities of different type as this entity. |
|
1451 | 47 |
|
0 | 48 |
according to the `entities` parameter, return entity objects or the |
49 |
equivalent result set |
|
50 |
""" |
|
51 |
res = self.related(self.tree_attribute, self.children_target, |
|
52 |
entities=entities) |
|
53 |
if entities: |
|
54 |
return [e for e in res if e.e_schema != self.e_schema] |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3426
diff
changeset
|
55 |
return res.filtered_rset(lambda x: x.e_schema != self.e_schema, self.cw_col) |
0 | 56 |
|
57 |
def same_type_children(self, entities=True): |
|
58 |
"""return children entities of the same type as this entity. |
|
1451 | 59 |
|
0 | 60 |
according to the `entities` parameter, return entity objects or the |
61 |
equivalent result set |
|
62 |
""" |
|
63 |
res = self.related(self.tree_attribute, self.children_target, |
|
64 |
entities=entities) |
|
65 |
if entities: |
|
66 |
return [e for e in res if e.e_schema == self.e_schema] |
|
4395 | 67 |
return res.filtered_rset(lambda x: x.e_schema is self.e_schema, self.cw_col) |
1451 | 68 |
|
0 | 69 |
def iterchildren(self, _done=None): |
70 |
if _done is None: |
|
71 |
_done = set() |
|
72 |
for child in self.children(): |
|
73 |
if child.eid in _done: |
|
3457
0924d0d08d60
[api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
74 |
self.error('loop in %s tree', self.__regid__.lower()) |
0 | 75 |
continue |
76 |
yield child |
|
77 |
_done.add(child.eid) |
|
78 |
||
79 |
def prefixiter(self, _done=None): |
|
80 |
if _done is None: |
|
81 |
_done = set() |
|
82 |
if self.eid in _done: |
|
83 |
return |
|
4383
e62a9efdd90a
it seems that prefixiter is expected to return child *folder* but was relying on a specific .children implementation, fix this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4351
diff
changeset
|
84 |
_done.add(self.eid) |
0 | 85 |
yield self |
4383
e62a9efdd90a
it seems that prefixiter is expected to return child *folder* but was relying on a specific .children implementation, fix this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4351
diff
changeset
|
86 |
for child in self.same_type_children(): |
e62a9efdd90a
it seems that prefixiter is expected to return child *folder* but was relying on a specific .children implementation, fix this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4351
diff
changeset
|
87 |
for entity in child.prefixiter(_done): |
e62a9efdd90a
it seems that prefixiter is expected to return child *folder* but was relying on a specific .children implementation, fix this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4351
diff
changeset
|
88 |
yield entity |
1451 | 89 |
|
0 | 90 |
@cached |
91 |
def path(self): |
|
92 |
"""returns the list of eids from the root object to this object""" |
|
93 |
path = [] |
|
94 |
parent = self |
|
95 |
while parent: |
|
96 |
if parent.eid in path: |
|
3457
0924d0d08d60
[api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
97 |
self.error('loop in %s tree', self.__regid__.lower()) |
0 | 98 |
break |
99 |
path.append(parent.eid) |
|
100 |
try: |
|
101 |
# check we are not leaving the tree |
|
102 |
if (parent.tree_attribute != self.tree_attribute or |
|
103 |
parent.parent_target != self.parent_target): |
|
104 |
break |
|
105 |
parent = parent.parent() |
|
106 |
except AttributeError: |
|
107 |
break |
|
108 |
||
109 |
path.reverse() |
|
110 |
return path |
|
1451 | 111 |
|
4351
619c7f9302fc
get back iterparents implementation from folder cubes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
112 |
def iterparents(self, strict=True): |
173
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
113 |
def _uptoroot(self): |
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
114 |
curr = self |
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
115 |
while True: |
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
116 |
curr = curr.parent() |
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
117 |
if curr is None: |
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
118 |
break |
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
119 |
yield curr |
4351
619c7f9302fc
get back iterparents implementation from folder cubes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
120 |
if not strict: |
619c7f9302fc
get back iterparents implementation from folder cubes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
121 |
return chain([self], _uptoroot(self)) |
173
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
122 |
return _uptoroot(self) |
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
123 |
|
0 | 124 |
## ITree interface ######################################################## |
125 |
def parent(self): |
|
126 |
"""return the parent entity if any, else None (e.g. if we are on the |
|
127 |
root |
|
128 |
""" |
|
129 |
try: |
|
130 |
return self.related(self.tree_attribute, self.parent_target, |
|
131 |
entities=True)[0] |
|
132 |
except (KeyError, IndexError): |
|
133 |
return None |
|
134 |
||
135 |
def children(self, entities=True, sametype=False): |
|
136 |
"""return children entities |
|
137 |
||
138 |
according to the `entities` parameter, return entity objects or the |
|
139 |
equivalent result set |
|
140 |
""" |
|
141 |
if sametype: |
|
142 |
return self.same_type_children(entities) |
|
143 |
else: |
|
144 |
return self.related(self.tree_attribute, self.children_target, |
|
145 |
entities=entities) |
|
146 |
||
147 |
def children_rql(self): |
|
5557
1a534c596bff
[entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
148 |
return self.cw_related_rql(self.tree_attribute, self.children_target) |
1451 | 149 |
|
0 | 150 |
def is_leaf(self): |
151 |
return len(self.children()) == 0 |
|
152 |
||
153 |
def is_root(self): |
|
154 |
return self.parent() is None |
|
155 |
||
156 |
def root(self): |
|
157 |
"""return the root object""" |
|
3426
6ea4a2ff01c9
[api] use cw_*
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3023
diff
changeset
|
158 |
return self._cw.entity_from_eid(self.path()[0]) |
0 | 159 |
|
160 |
||
161 |
class EmailableMixIn(object): |
|
162 |
"""base mixin providing the default get_email() method used by |
|
163 |
the massmailing view |
|
164 |
||
165 |
NOTE: The default implementation is based on the |
|
166 |
primary_email / use_email scheme |
|
167 |
""" |
|
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
168 |
@deprecated("[3.9] use entity.cw_adapt_to('IEmailable').get_email()") |
0 | 169 |
def get_email(self): |
170 |
if getattr(self, 'primary_email', None): |
|
171 |
return self.primary_email[0].address |
|
172 |
if getattr(self, 'use_email', None): |
|
173 |
return self.use_email[0].address |
|
174 |
return None |
|
175 |
||
176 |
||
3926 | 177 |
"""pluggable mixins system: plug classes registered in MI_REL_TRIGGERS on entity |
178 |
classes which have the relation described by the dict's key. |
|
1451 | 179 |
|
3926 | 180 |
NOTE: pluggable mixins can't override any method of the 'explicit' user classes tree |
181 |
(eg without plugged classes). This includes bases Entity and AnyEntity classes. |
|
182 |
""" |
|
0 | 183 |
MI_REL_TRIGGERS = { |
184 |
('primary_email', 'subject'): EmailableMixIn, |
|
185 |
('use_email', 'subject'): EmailableMixIn, |
|
186 |
} |
|
187 |
||
188 |
||
189 |
||
190 |
class TreeViewMixIn(object): |
|
191 |
"""a recursive tree view""" |
|
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
192 |
__metaclass__ = class_deprecated |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
193 |
__deprecation_warning__ = '[3.9] TreeViewMixIn is deprecated, use/override BaseTreeView instead' |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
194 |
|
3457
0924d0d08d60
[api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
195 |
__regid__ = 'tree' |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
196 |
__select__ = implements(ITree) |
0 | 197 |
item_vid = 'treeitem' |
198 |
||
199 |
def call(self, done=None, **kwargs): |
|
200 |
if done is None: |
|
201 |
done = set() |
|
202 |
super(TreeViewMixIn, self).call(done=done, **kwargs) |
|
1451 | 203 |
|
0 | 204 |
def cell_call(self, row, col=0, vid=None, done=None, **kwargs): |
205 |
done, entity = _done_init(done, self, row, col) |
|
206 |
if done is None: |
|
207 |
# entity is actually an error message |
|
208 |
self.w(u'<li class="badcontent">%s</li>' % entity) |
|
209 |
return |
|
210 |
self.open_item(entity) |
|
211 |
entity.view(vid or self.item_vid, w=self.w, **kwargs) |
|
212 |
relatedrset = entity.children(entities=False) |
|
3457
0924d0d08d60
[api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
213 |
self.wview(self.__regid__, relatedrset, 'null', done=done, **kwargs) |
0 | 214 |
self.close_item(entity) |
215 |
||
216 |
def open_item(self, entity): |
|
3457
0924d0d08d60
[api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
217 |
self.w(u'<li class="%s">\n' % entity.__regid__.lower()) |
0 | 218 |
def close_item(self, entity): |
219 |
self.w(u'</li>\n') |
|
220 |
||
221 |
||
222 |
class TreePathMixIn(object): |
|
223 |
"""a recursive path view""" |
|
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
224 |
__metaclass__ = class_deprecated |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
225 |
__deprecation_warning__ = '[3.9] TreePathMixIn is deprecated, use/override TreePathView instead' |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3426
diff
changeset
|
226 |
__regid__ = 'path' |
0 | 227 |
item_vid = 'oneline' |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2748
diff
changeset
|
228 |
separator = u' > ' |
0 | 229 |
|
230 |
def call(self, **kwargs): |
|
231 |
self.w(u'<div class="pathbar">') |
|
232 |
super(TreePathMixIn, self).call(**kwargs) |
|
233 |
self.w(u'</div>') |
|
1451 | 234 |
|
0 | 235 |
def cell_call(self, row, col=0, vid=None, done=None, **kwargs): |
236 |
done, entity = _done_init(done, self, row, col) |
|
237 |
if done is None: |
|
238 |
# entity is actually an error message |
|
239 |
self.w(u'<span class="badcontent">%s</span>' % entity) |
|
240 |
return |
|
241 |
parent = entity.parent() |
|
242 |
if parent: |
|
3457
0924d0d08d60
[api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
243 |
parent.view(self.__regid__, w=self.w, done=done) |
0 | 244 |
self.w(self.separator) |
245 |
entity.view(vid or self.item_vid, w=self.w) |
|
246 |
||
247 |
||
248 |
class ProgressMixIn(object): |
|
5309
e8567135a927
[doc/book] fix docstrings, add notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4719
diff
changeset
|
249 |
"""provide a default implementations for IProgress interface methods""" |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
250 |
__metaclass__ = class_deprecated |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
251 |
__deprecation_warning__ = '[3.9] ProgressMixIn is deprecated, use/override IProgressAdapter instead' |
0 | 252 |
|
253 |
@property |
|
254 |
def cost(self): |
|
255 |
return self.progress_info()['estimated'] |
|
256 |
||
257 |
@property |
|
258 |
def revised_cost(self): |
|
259 |
return self.progress_info().get('estimatedcorrected', self.cost) |
|
260 |
||
261 |
@property |
|
262 |
def done(self): |
|
263 |
return self.progress_info()['done'] |
|
264 |
||
265 |
@property |
|
266 |
def todo(self): |
|
267 |
return self.progress_info()['todo'] |
|
268 |
||
269 |
@cached |
|
270 |
def progress_info(self): |
|
271 |
raise NotImplementedError() |
|
272 |
||
273 |
def finished(self): |
|
274 |
return not self.in_progress() |
|
275 |
||
276 |
def in_progress(self): |
|
277 |
raise NotImplementedError() |
|
1451 | 278 |
|
0 | 279 |
def progress(self): |
280 |
try: |
|
281 |
return 100. * self.done / self.revised_cost |
|
282 |
except ZeroDivisionError: |
|
283 |
# total cost is 0 : if everything was estimated, task is completed |
|
961 | 284 |
if self.progress_info().get('notestimated'): |
0 | 285 |
return 0. |
286 |
return 100 |