author | Julien Jehannet <julien.jehannet@logilab.fr> |
Wed, 30 Sep 2009 18:53:18 +0200 | |
branch | stable |
changeset 3537 | 73b5bec579ba |
parent 2876 | b6993462ddb9 |
child 2880 | bfc8e1831290 |
child 3869 | ec6463886ac4 |
permissions | -rw-r--r-- |
0 | 1 |
"""helper functions for application hooks |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
: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:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
from cubicweb import RepositoryError |
|
1132 | 11 |
from cubicweb.server.pool import SingleLastOperation |
0 | 12 |
|
13 |
||
14 |
def entity_name(session, eid): |
|
15 |
"""return the "name" attribute of the entity with the given eid""" |
|
16 |
return entity_attr(session, eid, 'name') |
|
17 |
||
18 |
def entity_attr(session, eid, attr): |
|
19 |
"""return an arbitrary attribute of the entity with the given eid""" |
|
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2609
diff
changeset
|
20 |
return getattr(session.entity_from_eid(eid), attr) |
0 | 21 |
|
22 |
def rproperty(session, rtype, eidfrom, eidto, rprop): |
|
23 |
rschema = session.repo.schema[rtype] |
|
24 |
subjtype = session.describe(eidfrom)[0] |
|
25 |
objtype = session.describe(eidto)[0] |
|
26 |
return rschema.rproperty(subjtype, objtype, rprop) |
|
27 |
||
28 |
def check_internal_entity(session, eid, internal_names): |
|
29 |
"""check that the entity's name is not in the internal_names list. |
|
30 |
raise a RepositoryError if so, else return the entity's name |
|
31 |
""" |
|
32 |
name = entity_name(session, eid) |
|
33 |
if name in internal_names: |
|
34 |
raise RepositoryError('%s entity can\'t be deleted' % name) |
|
35 |
return name |
|
36 |
||
37 |
def get_user_sessions(repo, ueid): |
|
38 |
for session in repo._sessions.values(): |
|
39 |
if ueid == session.user.eid: |
|
40 |
yield session |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
41 |
|
0 | 42 |
|
43 |
# mail related ################################################################ |
|
44 |
||
45 |
class SendMailOp(SingleLastOperation): |
|
46 |
def __init__(self, session, msg=None, recipients=None, **kwargs): |
|
47 |
# may not specify msg yet, as |
|
48 |
# `cubicweb.sobjects.supervision.SupervisionMailOp` |
|
49 |
if msg is not None: |
|
50 |
assert recipients |
|
51 |
self.to_send = [(msg, recipients)] |
|
52 |
else: |
|
53 |
assert recipients is None |
|
54 |
self.to_send = [] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
55 |
super(SendMailOp, self).__init__(session, **kwargs) |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
56 |
|
0 | 57 |
def register(self, session): |
58 |
previous = super(SendMailOp, self).register(session) |
|
59 |
if previous: |
|
60 |
self.to_send = previous.to_send + self.to_send |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
61 |
|
0 | 62 |
def commit_event(self): |
63 |
self.repo.threaded_task(self.sendmails) |
|
64 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
65 |
def sendmails(self): |
2221
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2102
diff
changeset
|
66 |
self.config.sendmails(self.to_send) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
67 |
|
0 | 68 |
|
69 |
# state related ############################################################### |
|
70 |
||
71 |
def previous_state(session, eid): |
|
72 |
"""return the state of the entity with the given eid, |
|
73 |
usually since it's changing in the current transaction. Due to internal |
|
74 |
relation hooks, the relation may has been deleted at this point, so |
|
75 |
we have handle that |
|
76 |
""" |
|
2876
b6993462ddb9
[wf] shouldn't check for neweids while trying to get previous state (detected by forge tests)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
77 |
# don't check eid in session.transaction_data.get('neweids', ()), we don't |
b6993462ddb9
[wf] shouldn't check for neweids while trying to get previous state (detected by forge tests)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
78 |
# want to miss previous state of entity whose state change in the same |
b6993462ddb9
[wf] shouldn't check for neweids while trying to get previous state (detected by forge tests)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
79 |
# transaction as it's being created |
2102
268659907769
finish to update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
80 |
pending = session.transaction_data.get('pendingrelations', ()) |
268659907769
finish to update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
81 |
for eidfrom, rtype, eidto in reversed(pending): |
0 | 82 |
if rtype == 'in_state' and eidfrom == eid: |
83 |
rset = session.execute('Any S,N WHERE S eid %(x)s, S name N', |
|
84 |
{'x': eidto}, 'x') |
|
85 |
return rset.get_entity(0, 0) |
|
86 |
rset = session.execute('Any S,N WHERE X eid %(x)s, X in_state S, S name N', |
|
87 |
{'x': eid}, 'x') |
|
88 |
if rset: |
|
89 |
return rset.get_entity(0, 0) |