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