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