author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Wed, 21 Oct 2009 19:32:53 +0200 | |
branch | stable |
changeset 3780 | 4ff37d54aae1 |
parent 3583 | 385d013c7adb |
child 3589 | a5432f99f2d9 |
child 3830 | 3b6bbb3a3c3e |
permissions | -rw-r--r-- |
2997 | 1 |
"""mixins of entity/views organized somewhat in a graph or tree structure |
0 | 2 |
|
3 |
||
4 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1451
diff
changeset
|
5 |
:copyright: 2001-2009 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] |
|
41 |
return res.filtered_rset(lambda x: x.e_schema != self.e_schema, self.col) |
|
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] |
|
53 |
return res.filtered_rset(lambda x: x.e_schema == self.e_schema, self.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: |
|
60 |
self.error('loop in %s tree', self.id.lower()) |
|
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: |
|
86 |
self.error('loop in %s tree', self.id.lower()) |
|
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 |
|
173
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
101 |
def iterparents(self): |
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 |
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
109 |
return _uptoroot(self) |
a4a9e1a7e40f
TreeMixin : provide an iterator on the parents
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
62
diff
changeset
|
110 |
|
0 | 111 |
def notification_references(self, view): |
112 |
"""used to control References field of email send on notification |
|
113 |
for this entity. `view` is the notification view. |
|
1451 | 114 |
|
0 | 115 |
Should return a list of eids which can be used to generate message ids |
116 |
of previously sent email |
|
117 |
""" |
|
118 |
return self.path()[:-1] |
|
119 |
||
120 |
||
121 |
## ITree interface ######################################################## |
|
122 |
def parent(self): |
|
123 |
"""return the parent entity if any, else None (e.g. if we are on the |
|
124 |
root |
|
125 |
""" |
|
126 |
try: |
|
127 |
return self.related(self.tree_attribute, self.parent_target, |
|
128 |
entities=True)[0] |
|
129 |
except (KeyError, IndexError): |
|
130 |
return None |
|
131 |
||
132 |
def children(self, entities=True, sametype=False): |
|
133 |
"""return children entities |
|
134 |
||
135 |
according to the `entities` parameter, return entity objects or the |
|
136 |
equivalent result set |
|
137 |
""" |
|
138 |
if sametype: |
|
139 |
return self.same_type_children(entities) |
|
140 |
else: |
|
141 |
return self.related(self.tree_attribute, self.children_target, |
|
142 |
entities=entities) |
|
143 |
||
144 |
def children_rql(self): |
|
145 |
return self.related_rql(self.tree_attribute, self.children_target) |
|
1451 | 146 |
|
0 | 147 |
def is_leaf(self): |
148 |
return len(self.children()) == 0 |
|
149 |
||
150 |
def is_root(self): |
|
151 |
return self.parent() is None |
|
152 |
||
153 |
def root(self): |
|
154 |
"""return the root object""" |
|
2680
66472d85d548
[R] use req.entity_from_eid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
155 |
return self.req.entity_from_eid(self.path()[0]) |
0 | 156 |
|
157 |
||
158 |
class EmailableMixIn(object): |
|
159 |
"""base mixin providing the default get_email() method used by |
|
160 |
the massmailing view |
|
161 |
||
162 |
NOTE: The default implementation is based on the |
|
163 |
primary_email / use_email scheme |
|
164 |
""" |
|
165 |
__implements__ = (IEmailable,) |
|
1451 | 166 |
|
0 | 167 |
def get_email(self): |
168 |
if getattr(self, 'primary_email', None): |
|
169 |
return self.primary_email[0].address |
|
170 |
if getattr(self, 'use_email', None): |
|
171 |
return self.use_email[0].address |
|
172 |
return None |
|
173 |
||
174 |
@classmethod |
|
175 |
def allowed_massmail_keys(cls): |
|
176 |
"""returns a set of allowed email substitution keys |
|
177 |
||
178 |
The default is to return the entity's attribute list but an |
|
179 |
entity class might override this method to allow extra keys. |
|
180 |
For instance, the Person class might want to return a `companyname` |
|
181 |
key. |
|
182 |
""" |
|
183 |
return set(rs.type for rs, _ in cls.e_schema.attribute_definitions()) |
|
184 |
||
185 |
def as_email_context(self): |
|
186 |
"""returns the dictionary as used by the sendmail controller to |
|
187 |
build email bodies. |
|
1451 | 188 |
|
0 | 189 |
NOTE: the dictionary keys should match the list returned by the |
190 |
`allowed_massmail_keys` method. |
|
191 |
""" |
|
192 |
return dict( (attr, getattr(self, attr)) for attr in self.allowed_massmail_keys() ) |
|
193 |
||
194 |
||
1451 | 195 |
|
0 | 196 |
MI_REL_TRIGGERS = { |
197 |
('primary_email', 'subject'): EmailableMixIn, |
|
198 |
('use_email', 'subject'): EmailableMixIn, |
|
199 |
} |
|
200 |
||
201 |
||
202 |
||
203 |
def _done_init(done, view, row, col): |
|
204 |
"""handle an infinite recursion safety belt""" |
|
205 |
if done is None: |
|
206 |
done = set() |
|
207 |
entity = view.entity(row, col) |
|
208 |
if entity.eid in done: |
|
62
ef06f71533d9
use named substitutions in i18n strings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
209 |
msg = entity.req._('loop in %(rel)s relation (%(eid)s)') % { |
ef06f71533d9
use named substitutions in i18n strings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
210 |
'rel': entity.tree_attribute, |
ef06f71533d9
use named substitutions in i18n strings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
211 |
'eid': entity.eid |
ef06f71533d9
use named substitutions in i18n strings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
212 |
} |
0 | 213 |
return None, msg |
214 |
done.add(entity.eid) |
|
215 |
return done, entity |
|
216 |
||
217 |
||
218 |
class TreeViewMixIn(object): |
|
219 |
"""a recursive tree view""" |
|
220 |
id = 'tree' |
|
221 |
item_vid = 'treeitem' |
|
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
222 |
__select__ = implements(ITree) |
0 | 223 |
|
224 |
def call(self, done=None, **kwargs): |
|
225 |
if done is None: |
|
226 |
done = set() |
|
227 |
super(TreeViewMixIn, self).call(done=done, **kwargs) |
|
1451 | 228 |
|
0 | 229 |
def cell_call(self, row, col=0, vid=None, done=None, **kwargs): |
230 |
done, entity = _done_init(done, self, row, col) |
|
231 |
if done is None: |
|
232 |
# entity is actually an error message |
|
233 |
self.w(u'<li class="badcontent">%s</li>' % entity) |
|
234 |
return |
|
235 |
self.open_item(entity) |
|
236 |
entity.view(vid or self.item_vid, w=self.w, **kwargs) |
|
237 |
relatedrset = entity.children(entities=False) |
|
238 |
self.wview(self.id, relatedrset, 'null', done=done, **kwargs) |
|
239 |
self.close_item(entity) |
|
240 |
||
241 |
def open_item(self, entity): |
|
242 |
self.w(u'<li class="%s">\n' % entity.id.lower()) |
|
243 |
def close_item(self, entity): |
|
244 |
self.w(u'</li>\n') |
|
245 |
||
246 |
||
247 |
class TreePathMixIn(object): |
|
248 |
"""a recursive path view""" |
|
249 |
id = 'path' |
|
250 |
item_vid = 'oneline' |
|
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2748
diff
changeset
|
251 |
separator = u' > ' |
0 | 252 |
|
253 |
def call(self, **kwargs): |
|
254 |
self.w(u'<div class="pathbar">') |
|
255 |
super(TreePathMixIn, self).call(**kwargs) |
|
256 |
self.w(u'</div>') |
|
1451 | 257 |
|
0 | 258 |
def cell_call(self, row, col=0, vid=None, done=None, **kwargs): |
259 |
done, entity = _done_init(done, self, row, col) |
|
260 |
if done is None: |
|
261 |
# entity is actually an error message |
|
262 |
self.w(u'<span class="badcontent">%s</span>' % entity) |
|
263 |
return |
|
264 |
parent = entity.parent() |
|
265 |
if parent: |
|
266 |
parent.view(self.id, w=self.w, done=done) |
|
267 |
self.w(self.separator) |
|
268 |
entity.view(vid or self.item_vid, w=self.w) |
|
269 |
||
270 |
||
271 |
class ProgressMixIn(object): |
|
272 |
"""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
|
273 |
# This is an adapter isn't it ? |
0 | 274 |
|
275 |
@property |
|
276 |
def cost(self): |
|
277 |
return self.progress_info()['estimated'] |
|
278 |
||
279 |
@property |
|
280 |
def revised_cost(self): |
|
281 |
return self.progress_info().get('estimatedcorrected', self.cost) |
|
282 |
||
283 |
@property |
|
284 |
def done(self): |
|
285 |
return self.progress_info()['done'] |
|
286 |
||
287 |
@property |
|
288 |
def todo(self): |
|
289 |
return self.progress_info()['todo'] |
|
290 |
||
291 |
@cached |
|
292 |
def progress_info(self): |
|
293 |
raise NotImplementedError() |
|
294 |
||
295 |
def finished(self): |
|
296 |
return not self.in_progress() |
|
297 |
||
298 |
def in_progress(self): |
|
299 |
raise NotImplementedError() |
|
1451 | 300 |
|
0 | 301 |
def progress(self): |
302 |
try: |
|
303 |
return 100. * self.done / self.revised_cost |
|
304 |
except ZeroDivisionError: |
|
305 |
# total cost is 0 : if everything was estimated, task is completed |
|
961 | 306 |
if self.progress_info().get('notestimated'): |
0 | 307 |
return 0. |
308 |
return 100 |