author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 21 Jul 2009 23:09:27 +0200 | |
branch | stable |
changeset 2413 | b1bea4cab594 |
parent 2221 | d9b85a7b0bdd |
child 2609 | a0f6fa90cc32 |
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""" |
|
20 |
rset = session.execute('Any N WHERE X eid %%(x)s, X %s N' % attr, |
|
21 |
{'x': eid}, 'x') |
|
22 |
return rset[0][0] |
|
23 |
||
24 |
def rproperty(session, rtype, eidfrom, eidto, rprop): |
|
25 |
rschema = session.repo.schema[rtype] |
|
26 |
subjtype = session.describe(eidfrom)[0] |
|
27 |
objtype = session.describe(eidto)[0] |
|
28 |
return rschema.rproperty(subjtype, objtype, rprop) |
|
29 |
||
30 |
def check_internal_entity(session, eid, internal_names): |
|
31 |
"""check that the entity's name is not in the internal_names list. |
|
32 |
raise a RepositoryError if so, else return the entity's name |
|
33 |
""" |
|
34 |
name = entity_name(session, eid) |
|
35 |
if name in internal_names: |
|
36 |
raise RepositoryError('%s entity can\'t be deleted' % name) |
|
37 |
return name |
|
38 |
||
39 |
def get_user_sessions(repo, ueid): |
|
40 |
for session in repo._sessions.values(): |
|
41 |
if ueid == session.user.eid: |
|
42 |
yield session |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
43 |
|
0 | 44 |
|
45 |
# mail related ################################################################ |
|
46 |
||
47 |
class SendMailOp(SingleLastOperation): |
|
48 |
def __init__(self, session, msg=None, recipients=None, **kwargs): |
|
49 |
# may not specify msg yet, as |
|
50 |
# `cubicweb.sobjects.supervision.SupervisionMailOp` |
|
51 |
if msg is not None: |
|
52 |
assert recipients |
|
53 |
self.to_send = [(msg, recipients)] |
|
54 |
else: |
|
55 |
assert recipients is None |
|
56 |
self.to_send = [] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
57 |
super(SendMailOp, self).__init__(session, **kwargs) |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
58 |
|
0 | 59 |
def register(self, session): |
60 |
previous = super(SendMailOp, self).register(session) |
|
61 |
if previous: |
|
62 |
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
|
63 |
|
0 | 64 |
def commit_event(self): |
65 |
self.repo.threaded_task(self.sendmails) |
|
66 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
67 |
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
|
68 |
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
|
69 |
|
0 | 70 |
|
71 |
# state related ############################################################### |
|
72 |
||
73 |
def previous_state(session, eid): |
|
74 |
"""return the state of the entity with the given eid, |
|
75 |
usually since it's changing in the current transaction. Due to internal |
|
76 |
relation hooks, the relation may has been deleted at this point, so |
|
77 |
we have handle that |
|
78 |
""" |
|
2102
268659907769
finish to update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
79 |
pending = session.transaction_data.get('pendingrelations', ()) |
268659907769
finish to update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
80 |
for eidfrom, rtype, eidto in reversed(pending): |
0 | 81 |
if rtype == 'in_state' and eidfrom == eid: |
82 |
rset = session.execute('Any S,N WHERE S eid %(x)s, S name N', |
|
83 |
{'x': eidto}, 'x') |
|
84 |
return rset.get_entity(0, 0) |
|
85 |
rset = session.execute('Any S,N WHERE X eid %(x)s, X in_state S, S name N', |
|
86 |
{'x': eid}, 'x') |
|
87 |
if rset: |
|
88 |
return rset.get_entity(0, 0) |