author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 22 Jul 2009 18:35:21 +0200 | |
changeset 2423 | f6757021018f |
parent 2144 | 51c84d585456 |
child 2613 | 5e19c2bb370e |
permissions | -rw-r--r-- |
0 | 1 |
"""mixins of entity/views organized somewhat in a graph or tree structure |
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 |
||
1175
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
11 |
from logilab.common.deprecation import obsolete |
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 |
0 | 16 |
from cubicweb.interfaces import IWorkflowable, IEmailable, ITree |
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 __iter__(self): |
148 |
return self.iterchildren() |
|
149 |
||
150 |
def is_leaf(self): |
|
151 |
return len(self.children()) == 0 |
|
152 |
||
153 |
def is_root(self): |
|
154 |
return self.parent() is None |
|
155 |
||
156 |
def root(self): |
|
157 |
"""return the root object""" |
|
158 |
return self.req.eid_rset(self.path()[0]).get_entity(0, 0) |
|
159 |
||
160 |
||
161 |
class WorkflowableMixIn(object): |
|
162 |
"""base mixin providing workflow helper methods for workflowable entities. |
|
163 |
This mixin will be automatically set on class supporting the 'in_state' |
|
164 |
relation (which implies supporting 'wf_info_for' as well) |
|
165 |
""" |
|
166 |
__implements__ = (IWorkflowable,) |
|
1451 | 167 |
|
0 | 168 |
@property |
169 |
def state(self): |
|
441 | 170 |
try: |
171 |
return self.in_state[0].name |
|
172 |
except IndexError: |
|
173 |
self.warning('entity %s has no state', self) |
|
174 |
return None |
|
1451 | 175 |
|
0 | 176 |
@property |
177 |
def displayable_state(self): |
|
178 |
return self.req._(self.state) |
|
179 |
||
180 |
def wf_state(self, statename): |
|
960
040f9a2b6a36
oops, I though those methods had been tested, seems not...
sylvain.thenault@logilab.fr
parents:
959
diff
changeset
|
181 |
rset = self.req.execute('Any S, SN WHERE S name SN, S name %(n)s, S state_of E, E name %(e)s', |
0 | 182 |
{'n': statename, 'e': str(self.e_schema)}) |
183 |
if rset: |
|
184 |
return rset.get_entity(0, 0) |
|
185 |
return None |
|
1451 | 186 |
|
0 | 187 |
def wf_transition(self, trname): |
960
040f9a2b6a36
oops, I though those methods had been tested, seems not...
sylvain.thenault@logilab.fr
parents:
959
diff
changeset
|
188 |
rset = self.req.execute('Any T, TN WHERE T name TN, T name %(n)s, T transition_of E, E name %(e)s', |
0 | 189 |
{'n': trname, 'e': str(self.e_schema)}) |
190 |
if rset: |
|
191 |
return rset.get_entity(0, 0) |
|
192 |
return None |
|
843 | 193 |
|
2117
b451bd56c9cd
fix change_state to allow state as eid/state name/state entity/
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
194 |
def change_state(self, state, trcomment=None, trcommentformat=None): |
0 | 195 |
"""change the entity's state according to a state defined in given |
196 |
parameters |
|
197 |
""" |
|
2117
b451bd56c9cd
fix change_state to allow state as eid/state name/state entity/
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
198 |
if isinstance(state, basestring): |
b451bd56c9cd
fix change_state to allow state as eid/state name/state entity/
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
199 |
state = self.wf_state(state) |
b451bd56c9cd
fix change_state to allow state as eid/state name/state entity/
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
200 |
assert state is not None, 'not a %s state: %s' % (self.id, state) |
b451bd56c9cd
fix change_state to allow state as eid/state name/state entity/
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
201 |
if hasattr(state, 'eid'): |
b451bd56c9cd
fix change_state to allow state as eid/state name/state entity/
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
202 |
stateeid = state.eid |
b451bd56c9cd
fix change_state to allow state as eid/state name/state entity/
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
203 |
else: |
b451bd56c9cd
fix change_state to allow state as eid/state name/state entity/
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
204 |
stateeid = state |
b451bd56c9cd
fix change_state to allow state as eid/state name/state entity/
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
205 |
stateeid = typed_eid(stateeid) |
0 | 206 |
if trcomment: |
207 |
self.req.set_shared_data('trcomment', trcomment) |
|
208 |
if trcommentformat: |
|
209 |
self.req.set_shared_data('trcommentformat', trcommentformat) |
|
210 |
self.req.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
|
211 |
{'x': self.eid, 's': stateeid}, 'x') |
|
1451 | 212 |
|
0 | 213 |
def can_pass_transition(self, trname): |
214 |
"""return the Transition instance if the current user can pass the |
|
215 |
transition with the given name, else None |
|
216 |
""" |
|
217 |
stateeid = self.in_state[0].eid |
|
218 |
rset = self.req.execute('Any T,N,DS WHERE S allowed_transition T,' |
|
219 |
'S eid %(x)s,T name %(trname)s,ET name %(et)s,' |
|
220 |
'T name N,T destination_state DS,T transition_of ET', |
|
221 |
{'x': stateeid, 'et': str(self.e_schema), |
|
222 |
'trname': trname}, 'x') |
|
223 |
for tr in rset.entities(): |
|
224 |
if tr.may_be_passed(self.eid, stateeid): |
|
225 |
return tr |
|
1451 | 226 |
|
0 | 227 |
def latest_trinfo(self): |
228 |
"""return the latest transition information for this entity""" |
|
229 |
return self.reverse_wf_info_for[-1] |
|
1451 | 230 |
|
0 | 231 |
# __method methods ######################################################## |
1451 | 232 |
|
0 | 233 |
def set_state(self, params=None): |
234 |
"""change the entity's state according to a state defined in given |
|
235 |
parameters, used to be called using __method controler facility |
|
236 |
""" |
|
237 |
params = params or self.req.form |
|
1266 | 238 |
self.change_state(typed_eid(params.pop('state')), |
239 |
params.get('trcomment'), |
|
0 | 240 |
params.get('trcommentformat')) |
241 |
self.req.set_message(self.req._('__msg state changed')) |
|
1451 | 242 |
|
1175
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
243 |
# specific vocabulary methods ############################################# |
1451 | 244 |
|
1267 | 245 |
@obsolete('use EntityFieldsForm.subject_in_state_vocabulary') |
1175
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
246 |
def subject_in_state_vocabulary(self, rschema, limit=None): |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
247 |
form = self.vreg.select('forms', 'edition', self.req, entity=self) |
2005
e8032965f37a
turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
248 |
return form.subject_in_state_vocabulary(rschema, limit) |
0 | 249 |
|
250 |
||
251 |
||
252 |
class EmailableMixIn(object): |
|
253 |
"""base mixin providing the default get_email() method used by |
|
254 |
the massmailing view |
|
255 |
||
256 |
NOTE: The default implementation is based on the |
|
257 |
primary_email / use_email scheme |
|
258 |
""" |
|
259 |
__implements__ = (IEmailable,) |
|
1451 | 260 |
|
0 | 261 |
def get_email(self): |
262 |
if getattr(self, 'primary_email', None): |
|
263 |
return self.primary_email[0].address |
|
264 |
if getattr(self, 'use_email', None): |
|
265 |
return self.use_email[0].address |
|
266 |
return None |
|
267 |
||
268 |
@classmethod |
|
269 |
def allowed_massmail_keys(cls): |
|
270 |
"""returns a set of allowed email substitution keys |
|
271 |
||
272 |
The default is to return the entity's attribute list but an |
|
273 |
entity class might override this method to allow extra keys. |
|
274 |
For instance, the Person class might want to return a `companyname` |
|
275 |
key. |
|
276 |
""" |
|
277 |
return set(rs.type for rs, _ in cls.e_schema.attribute_definitions()) |
|
278 |
||
279 |
def as_email_context(self): |
|
280 |
"""returns the dictionary as used by the sendmail controller to |
|
281 |
build email bodies. |
|
1451 | 282 |
|
0 | 283 |
NOTE: the dictionary keys should match the list returned by the |
284 |
`allowed_massmail_keys` method. |
|
285 |
""" |
|
286 |
return dict( (attr, getattr(self, attr)) for attr in self.allowed_massmail_keys() ) |
|
287 |
||
288 |
||
1451 | 289 |
|
0 | 290 |
MI_REL_TRIGGERS = { |
291 |
('in_state', 'subject'): WorkflowableMixIn, |
|
292 |
('primary_email', 'subject'): EmailableMixIn, |
|
293 |
('use_email', 'subject'): EmailableMixIn, |
|
294 |
} |
|
295 |
||
296 |
||
297 |
||
298 |
def _done_init(done, view, row, col): |
|
299 |
"""handle an infinite recursion safety belt""" |
|
300 |
if done is None: |
|
301 |
done = set() |
|
302 |
entity = view.entity(row, col) |
|
303 |
if entity.eid in done: |
|
62
ef06f71533d9
use named substitutions in i18n strings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
304 |
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
|
305 |
'rel': entity.tree_attribute, |
ef06f71533d9
use named substitutions in i18n strings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
306 |
'eid': entity.eid |
ef06f71533d9
use named substitutions in i18n strings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
307 |
} |
0 | 308 |
return None, msg |
309 |
done.add(entity.eid) |
|
310 |
return done, entity |
|
311 |
||
312 |
||
313 |
class TreeViewMixIn(object): |
|
314 |
"""a recursive tree view""" |
|
315 |
id = 'tree' |
|
316 |
item_vid = 'treeitem' |
|
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
317 |
__select__ = implements(ITree) |
0 | 318 |
|
319 |
def call(self, done=None, **kwargs): |
|
320 |
if done is None: |
|
321 |
done = set() |
|
322 |
super(TreeViewMixIn, self).call(done=done, **kwargs) |
|
1451 | 323 |
|
0 | 324 |
def cell_call(self, row, col=0, vid=None, done=None, **kwargs): |
325 |
done, entity = _done_init(done, self, row, col) |
|
326 |
if done is None: |
|
327 |
# entity is actually an error message |
|
328 |
self.w(u'<li class="badcontent">%s</li>' % entity) |
|
329 |
return |
|
330 |
self.open_item(entity) |
|
331 |
entity.view(vid or self.item_vid, w=self.w, **kwargs) |
|
332 |
relatedrset = entity.children(entities=False) |
|
333 |
self.wview(self.id, relatedrset, 'null', done=done, **kwargs) |
|
334 |
self.close_item(entity) |
|
335 |
||
336 |
def open_item(self, entity): |
|
337 |
self.w(u'<li class="%s">\n' % entity.id.lower()) |
|
338 |
def close_item(self, entity): |
|
339 |
self.w(u'</li>\n') |
|
340 |
||
341 |
||
342 |
class TreePathMixIn(object): |
|
343 |
"""a recursive path view""" |
|
344 |
id = 'path' |
|
345 |
item_vid = 'oneline' |
|
346 |
separator = u' > ' |
|
347 |
||
348 |
def call(self, **kwargs): |
|
349 |
self.w(u'<div class="pathbar">') |
|
350 |
super(TreePathMixIn, self).call(**kwargs) |
|
351 |
self.w(u'</div>') |
|
1451 | 352 |
|
0 | 353 |
def cell_call(self, row, col=0, vid=None, done=None, **kwargs): |
354 |
done, entity = _done_init(done, self, row, col) |
|
355 |
if done is None: |
|
356 |
# entity is actually an error message |
|
357 |
self.w(u'<span class="badcontent">%s</span>' % entity) |
|
358 |
return |
|
359 |
parent = entity.parent() |
|
360 |
if parent: |
|
361 |
parent.view(self.id, w=self.w, done=done) |
|
362 |
self.w(self.separator) |
|
363 |
entity.view(vid or self.item_vid, w=self.w) |
|
364 |
||
365 |
||
366 |
class ProgressMixIn(object): |
|
367 |
"""provide default implementations for IProgress interface methods""" |
|
368 |
||
369 |
@property |
|
370 |
def cost(self): |
|
371 |
return self.progress_info()['estimated'] |
|
372 |
||
373 |
@property |
|
374 |
def revised_cost(self): |
|
375 |
return self.progress_info().get('estimatedcorrected', self.cost) |
|
376 |
||
377 |
@property |
|
378 |
def done(self): |
|
379 |
return self.progress_info()['done'] |
|
380 |
||
381 |
@property |
|
382 |
def todo(self): |
|
383 |
return self.progress_info()['todo'] |
|
384 |
||
385 |
@cached |
|
386 |
def progress_info(self): |
|
387 |
raise NotImplementedError() |
|
388 |
||
389 |
def finished(self): |
|
390 |
return not self.in_progress() |
|
391 |
||
392 |
def in_progress(self): |
|
393 |
raise NotImplementedError() |
|
1451 | 394 |
|
0 | 395 |
def progress(self): |
396 |
try: |
|
397 |
return 100. * self.done / self.revised_cost |
|
398 |
except ZeroDivisionError: |
|
399 |
# total cost is 0 : if everything was estimated, task is completed |
|
961 | 400 |
if self.progress_info().get('notestimated'): |
0 | 401 |
return 0. |
402 |
return 100 |