author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 31 Jul 2009 23:55:50 +0200 | |
changeset 2609 | a0f6fa90cc32 |
parent 2221 | d9b85a7b0bdd |
child 2647 | b0a2e779845c |
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 |
""" |
|
2609
a0f6fa90cc32
[F workflow repo] don't try to get previous state of newly created entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2221
diff
changeset
|
79 |
if eid in session.transaction_data.get('neweids', ()): |
a0f6fa90cc32
[F workflow repo] don't try to get previous state of newly created entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2221
diff
changeset
|
80 |
return |
2102
268659907769
finish to update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
81 |
pending = session.transaction_data.get('pendingrelations', ()) |
268659907769
finish to update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
82 |
for eidfrom, rtype, eidto in reversed(pending): |
0 | 83 |
if rtype == 'in_state' and eidfrom == eid: |
84 |
rset = session.execute('Any S,N WHERE S eid %(x)s, S name N', |
|
85 |
{'x': eidto}, 'x') |
|
86 |
return rset.get_entity(0, 0) |
|
87 |
rset = session.execute('Any S,N WHERE X eid %(x)s, X in_state S, S name N', |
|
88 |
{'x': eid}, 'x') |
|
89 |
if rset: |
|
90 |
return rset.get_entity(0, 0) |