author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 02 Apr 2010 09:56:40 +0200 | |
branch | stable |
changeset 5135 | f15286235aef |
parent 4719 | aaed3f813ef8 |
child 5309 | e8567135a927 |
permissions | -rw-r--r-- |
2997 | 1 |
"""mixins of entity/views organized somewhat in a graph or tree structure |
0 | 2 |
|
3 |
||
4 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3926
diff
changeset
|
5 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
: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:
1451
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
||
4401
4d973c834eb3
missing import
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4395
diff
changeset
|
11 |
from itertools import chain |
4d973c834eb3
missing import
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4395
diff
changeset
|
12 |
|
0 | 13 |
from logilab.common.decorators import cached |
14 |
||
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
|
15 |
from cubicweb.selectors import implements |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2748
diff
changeset
|
16 |
from cubicweb.interfaces import IEmailable, ITree |
0 | 17 |
|
18 |
||
19 |
class TreeMixIn(object): |
|
20 |
"""base tree-mixin providing the tree interface |
|
21 |
||
22 |
This mixin has to be inherited explicitly and configured using the |
|
23 |
tree_attribute, parent_target and children_target class attribute to |
|
24 |
benefit from this default implementation |
|
25 |
""" |
|
26 |
tree_attribute = None |
|
27 |
# XXX misnamed |
|
28 |
parent_target = 'subject' |
|
29 |
children_target = 'object' |
|
1451 | 30 |
|
0 | 31 |
def different_type_children(self, entities=True): |
32 |
"""return children entities of different type as this entity. |
|
1451 | 33 |
|
0 | 34 |
according to the `entities` parameter, return entity objects or the |
35 |
equivalent result set |
|
36 |
""" |
|
37 |
res = self.related(self.tree_attribute, self.children_target, |
|
38 |
entities=entities) |
|
39 |
if entities: |
|
40 |
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
|
41 |
return res.filtered_rset(lambda x: x.e_schema != self.e_schema, self.cw_col) |
0 | 42 |
|
43 |
def same_type_children(self, entities=True): |
|
44 |
"""return children entities of the same type as this entity. |
|
1451 | 45 |
|
0 | 46 |
according to the `entities` parameter, return entity objects or the |
47 |
equivalent result set |
|
48 |
""" |
|
49 |
res = self.related(self.tree_attribute, self.children_target, |
|
50 |
entities=entities) |
|
51 |
if entities: |
|
52 |
return [e for e in res if e.e_schema == self.e_schema] |
|
4395 | 53 |
return res.filtered_rset(lambda x: x.e_schema is self.e_schema, self.cw_col) |
1451 | 54 |
|
0 | 55 |
def iterchildren(self, _done=None): |
56 |
if _done is None: |
|
57 |
_done = set() |
|
58 |
for child in self.children(): |
|
59 |
if child.eid in _done: |
|
3457
0924d0d08d60
[api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
60 |
self.error('loop in %s tree', self.__regid__.lower()) |
0 | 61 |
continue |
62 |
yield child |
|
63 |
_done.add(child.eid) |
|
64 |
||
65 |
def prefixiter(self, _done=None): |
|
66 |
if _done is None: |
|
67 |
_done = set() |
|
68 |
if self.eid in _done: |
|
69 |
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
|
70 |
_done.add(self.eid) |
0 | 71 |
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
|
72 |
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
|
73 |
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
|
74 |
yield entity |
1451 | 75 |
|
0 | 76 |
@cached |
77 |
def path(self): |
|
78 |
"""returns the list of eids from the root object to this object""" |
|
79 |
path = [] |
|
80 |
parent = self |
|
81 |
while parent: |
|
82 |
if parent.eid in path: |
|
3457
0924d0d08d60
[api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
83 |
self.error('loop in %s tree', self.__regid__.lower()) |
0 | 84 |
break |
85 |
path.append(parent.eid) |
|
86 |
try: |
|
87 |
# check we are not leaving the tree |
|
88 |
if (parent.tree_attribute != self.tree_attribute or |
|
89 |
parent.parent_target != self.parent_target): |
|
90 |
break |
|
91 |
parent = parent.parent() |
|
92 |
except AttributeError: |
|
93 |
break |
|
94 |
||
95 |
path.reverse() |
|
96 |
return path |
|
1451 | 97 |
|
4351
619c7f9302fc
get back iterparents implementation from folder cubes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
98 |
def iterparents(self, strict=True): |
173
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
99 |
def _uptoroot(self): |
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
100 |
curr = self |
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
101 |
while True: |
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
102 |
curr = curr.parent() |
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
103 |
if curr is None: |
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
104 |
break |
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
105 |
yield curr |
4351
619c7f9302fc
get back iterparents implementation from folder cubes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
106 |
if not strict: |
619c7f9302fc
get back iterparents implementation from folder cubes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
107 |
return chain([self], _uptoroot(self)) |
173
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
108 |
return _uptoroot(self) |
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
109 |
|
0 | 110 |
def notification_references(self, view): |
111 |
"""used to control References field of email send on notification |
|
112 |
for this entity. `view` is the notification view. |
|
1451 | 113 |
|
0 | 114 |
Should return a list of eids which can be used to generate message ids |
115 |
of previously sent email |
|
116 |
""" |
|
117 |
return self.path()[:-1] |
|
118 |
||
119 |
||
120 |
## ITree interface ######################################################## |
|
121 |
def parent(self): |
|
122 |
"""return the parent entity if any, else None (e.g. if we are on the |
|
123 |
root |
|
124 |
""" |
|
125 |
try: |
|
126 |
return self.related(self.tree_attribute, self.parent_target, |
|
127 |
entities=True)[0] |
|
128 |
except (KeyError, IndexError): |
|
129 |
return None |
|
130 |
||
131 |
def children(self, entities=True, sametype=False): |
|
132 |
"""return children entities |
|
133 |
||
134 |
according to the `entities` parameter, return entity objects or the |
|
135 |
equivalent result set |
|
136 |
""" |
|
137 |
if sametype: |
|
138 |
return self.same_type_children(entities) |
|
139 |
else: |
|
140 |
return self.related(self.tree_attribute, self.children_target, |
|
141 |
entities=entities) |
|
142 |
||
143 |
def children_rql(self): |
|
144 |
return self.related_rql(self.tree_attribute, self.children_target) |
|
1451 | 145 |
|
0 | 146 |
def is_leaf(self): |
147 |
return len(self.children()) == 0 |
|
148 |
||
149 |
def is_root(self): |
|
150 |
return self.parent() is None |
|
151 |
||
152 |
def root(self): |
|
153 |
"""return the root object""" |
|
3426
6ea4a2ff01c9
[api] use cw_*
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3023
diff
changeset
|
154 |
return self._cw.entity_from_eid(self.path()[0]) |
0 | 155 |
|
156 |
||
157 |
class EmailableMixIn(object): |
|
158 |
"""base mixin providing the default get_email() method used by |
|
159 |
the massmailing view |
|
160 |
||
161 |
NOTE: The default implementation is based on the |
|
162 |
primary_email / use_email scheme |
|
163 |
""" |
|
164 |
__implements__ = (IEmailable,) |
|
1451 | 165 |
|
0 | 166 |
def get_email(self): |
167 |
if getattr(self, 'primary_email', None): |
|
168 |
return self.primary_email[0].address |
|
169 |
if getattr(self, 'use_email', None): |
|
170 |
return self.use_email[0].address |
|
171 |
return None |
|
172 |
||
173 |
@classmethod |
|
174 |
def allowed_massmail_keys(cls): |
|
175 |
"""returns a set of allowed email substitution keys |
|
176 |
||
177 |
The default is to return the entity's attribute list but an |
|
178 |
entity class might override this method to allow extra keys. |
|
179 |
For instance, the Person class might want to return a `companyname` |
|
180 |
key. |
|
181 |
""" |
|
3830
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3583
diff
changeset
|
182 |
return set(rschema.type |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3583
diff
changeset
|
183 |
for rschema, attrtype in cls.e_schema.attribute_definitions() |
3b6bbb3a3c3e
close #472361: omit password and bytes attributes in allowed mass-mailing keys
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3583
diff
changeset
|
184 |
if attrtype.type not in ('Password', 'Bytes')) |
0 | 185 |
|
186 |
def as_email_context(self): |
|
187 |
"""returns the dictionary as used by the sendmail controller to |
|
188 |
build email bodies. |
|
1451 | 189 |
|
0 | 190 |
NOTE: the dictionary keys should match the list returned by the |
191 |
`allowed_massmail_keys` method. |
|
192 |
""" |
|
193 |
return dict( (attr, getattr(self, attr)) for attr in self.allowed_massmail_keys() ) |
|
194 |
||
195 |
||
3926 | 196 |
"""pluggable mixins system: plug classes registered in MI_REL_TRIGGERS on entity |
197 |
classes which have the relation described by the dict's key. |
|
1451 | 198 |
|
3926 | 199 |
NOTE: pluggable mixins can't override any method of the 'explicit' user classes tree |
200 |
(eg without plugged classes). This includes bases Entity and AnyEntity classes. |
|
201 |
""" |
|
0 | 202 |
MI_REL_TRIGGERS = { |
203 |
('primary_email', 'subject'): EmailableMixIn, |
|
204 |
('use_email', 'subject'): EmailableMixIn, |
|
205 |
} |
|
206 |
||
207 |
||
208 |
||
209 |
def _done_init(done, view, row, col): |
|
210 |
"""handle an infinite recursion safety belt""" |
|
211 |
if done is None: |
|
212 |
done = set() |
|
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3457
diff
changeset
|
213 |
entity = view.cw_rset.get_entity(row, col) |
0 | 214 |
if entity.eid in done: |
3426
6ea4a2ff01c9
[api] use cw_*
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3023
diff
changeset
|
215 |
msg = entity._cw._('loop in %(rel)s relation (%(eid)s)') % { |
62
ef06f71533d9
use named substitutions in i18n strings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
216 |
'rel': entity.tree_attribute, |
ef06f71533d9
use named substitutions in i18n strings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
217 |
'eid': entity.eid |
ef06f71533d9
use named substitutions in i18n strings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
218 |
} |
0 | 219 |
return None, msg |
220 |
done.add(entity.eid) |
|
221 |
return done, entity |
|
222 |
||
223 |
||
224 |
class TreeViewMixIn(object): |
|
225 |
"""a recursive tree view""" |
|
3457
0924d0d08d60
[api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
226 |
__regid__ = 'tree' |
0 | 227 |
item_vid = 'treeitem' |
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
228 |
__select__ = implements(ITree) |
0 | 229 |
|
230 |
def call(self, done=None, **kwargs): |
|
231 |
if done is None: |
|
232 |
done = set() |
|
233 |
super(TreeViewMixIn, self).call(done=done, **kwargs) |
|
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'<li class="badcontent">%s</li>' % entity) |
|
240 |
return |
|
241 |
self.open_item(entity) |
|
242 |
entity.view(vid or self.item_vid, w=self.w, **kwargs) |
|
243 |
relatedrset = entity.children(entities=False) |
|
3457
0924d0d08d60
[api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
244 |
self.wview(self.__regid__, relatedrset, 'null', done=done, **kwargs) |
0 | 245 |
self.close_item(entity) |
246 |
||
247 |
def open_item(self, entity): |
|
3457
0924d0d08d60
[api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
248 |
self.w(u'<li class="%s">\n' % entity.__regid__.lower()) |
0 | 249 |
def close_item(self, entity): |
250 |
self.w(u'</li>\n') |
|
251 |
||
252 |
||
253 |
class TreePathMixIn(object): |
|
254 |
"""a recursive path view""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3426
diff
changeset
|
255 |
__regid__ = 'path' |
0 | 256 |
item_vid = 'oneline' |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2748
diff
changeset
|
257 |
separator = u' > ' |
0 | 258 |
|
259 |
def call(self, **kwargs): |
|
260 |
self.w(u'<div class="pathbar">') |
|
261 |
super(TreePathMixIn, self).call(**kwargs) |
|
262 |
self.w(u'</div>') |
|
1451 | 263 |
|
0 | 264 |
def cell_call(self, row, col=0, vid=None, done=None, **kwargs): |
265 |
done, entity = _done_init(done, self, row, col) |
|
266 |
if done is None: |
|
267 |
# entity is actually an error message |
|
268 |
self.w(u'<span class="badcontent">%s</span>' % entity) |
|
269 |
return |
|
270 |
parent = entity.parent() |
|
271 |
if parent: |
|
3457
0924d0d08d60
[api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
272 |
parent.view(self.__regid__, w=self.w, done=done) |
0 | 273 |
self.w(self.separator) |
274 |
entity.view(vid or self.item_vid, w=self.w) |
|
275 |
||
276 |
||
277 |
class ProgressMixIn(object): |
|
278 |
"""provide default implementations for IProgress interface methods""" |
|
2748
d2fcf19bfb34
F [registry views] add _registry startup view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2728
diff
changeset
|
279 |
# This is an adapter isn't it ? |
0 | 280 |
|
281 |
@property |
|
282 |
def cost(self): |
|
283 |
return self.progress_info()['estimated'] |
|
284 |
||
285 |
@property |
|
286 |
def revised_cost(self): |
|
287 |
return self.progress_info().get('estimatedcorrected', self.cost) |
|
288 |
||
289 |
@property |
|
290 |
def done(self): |
|
291 |
return self.progress_info()['done'] |
|
292 |
||
293 |
@property |
|
294 |
def todo(self): |
|
295 |
return self.progress_info()['todo'] |
|
296 |
||
297 |
@cached |
|
298 |
def progress_info(self): |
|
299 |
raise NotImplementedError() |
|
300 |
||
301 |
def finished(self): |
|
302 |
return not self.in_progress() |
|
303 |
||
304 |
def in_progress(self): |
|
305 |
raise NotImplementedError() |
|
1451 | 306 |
|
0 | 307 |
def progress(self): |
308 |
try: |
|
309 |
return 100. * self.done / self.revised_cost |
|
310 |
except ZeroDivisionError: |
|
311 |
# total cost is 0 : if everything was estimated, task is completed |
|
961 | 312 |
if self.progress_info().get('notestimated'): |
0 | 313 |
return 0. |
314 |
return 100 |