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