author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 05 Jun 2009 19:04:20 +0200 | |
changeset 2059 | af33833d7571 |
parent 1977 | 606923dff11b |
child 2102 | 268659907769 |
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 smtplib import SMTP |
|
11 |
from threading import Lock |
|
12 |
||
13 |
from cubicweb import RepositoryError |
|
1132 | 14 |
from cubicweb.server.pool import SingleLastOperation |
0 | 15 |
|
16 |
||
17 |
def entity_name(session, eid): |
|
18 |
"""return the "name" attribute of the entity with the given eid""" |
|
19 |
return entity_attr(session, eid, 'name') |
|
20 |
||
21 |
def entity_attr(session, eid, attr): |
|
22 |
"""return an arbitrary attribute of the entity with the given eid""" |
|
23 |
rset = session.execute('Any N WHERE X eid %%(x)s, X %s N' % attr, |
|
24 |
{'x': eid}, 'x') |
|
25 |
return rset[0][0] |
|
26 |
||
27 |
def rproperty(session, rtype, eidfrom, eidto, rprop): |
|
28 |
rschema = session.repo.schema[rtype] |
|
29 |
subjtype = session.describe(eidfrom)[0] |
|
30 |
objtype = session.describe(eidto)[0] |
|
31 |
return rschema.rproperty(subjtype, objtype, rprop) |
|
32 |
||
33 |
def check_internal_entity(session, eid, internal_names): |
|
34 |
"""check that the entity's name is not in the internal_names list. |
|
35 |
raise a RepositoryError if so, else return the entity's name |
|
36 |
""" |
|
37 |
name = entity_name(session, eid) |
|
38 |
if name in internal_names: |
|
39 |
raise RepositoryError('%s entity can\'t be deleted' % name) |
|
40 |
return name |
|
41 |
||
42 |
def get_user_sessions(repo, ueid): |
|
43 |
for session in repo._sessions.values(): |
|
44 |
if ueid == session.user.eid: |
|
45 |
yield session |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
46 |
|
0 | 47 |
|
48 |
# mail related ################################################################ |
|
49 |
||
50 |
SMTP_LOCK = Lock() |
|
51 |
||
52 |
class SendMailOp(SingleLastOperation): |
|
53 |
def __init__(self, session, msg=None, recipients=None, **kwargs): |
|
54 |
# may not specify msg yet, as |
|
55 |
# `cubicweb.sobjects.supervision.SupervisionMailOp` |
|
56 |
if msg is not None: |
|
57 |
assert recipients |
|
58 |
self.to_send = [(msg, recipients)] |
|
59 |
else: |
|
60 |
assert recipients is None |
|
61 |
self.to_send = [] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
62 |
super(SendMailOp, self).__init__(session, **kwargs) |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
63 |
|
0 | 64 |
def register(self, session): |
65 |
previous = super(SendMailOp, self).register(session) |
|
66 |
if previous: |
|
67 |
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
|
68 |
|
0 | 69 |
def commit_event(self): |
70 |
self.repo.threaded_task(self.sendmails) |
|
71 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
72 |
def sendmails(self): |
0 | 73 |
server, port = self.config['smtp-host'], self.config['smtp-port'] |
74 |
SMTP_LOCK.acquire() |
|
75 |
try: |
|
76 |
try: |
|
77 |
smtp = SMTP(server, port) |
|
78 |
except Exception, ex: |
|
79 |
self.exception("can't connect to smtp server %s:%s (%s)", |
|
80 |
server, port, ex) |
|
81 |
return |
|
82 |
heloaddr = '%s <%s>' % (self.config['sender-name'], |
|
83 |
self.config['sender-addr']) |
|
84 |
for msg, recipients in self.to_send: |
|
85 |
try: |
|
86 |
smtp.sendmail(heloaddr, recipients, msg.as_string()) |
|
87 |
except Exception, ex: |
|
88 |
self.exception("error sending mail to %s (%s)", |
|
89 |
recipients, ex) |
|
90 |
smtp.close() |
|
91 |
finally: |
|
92 |
SMTP_LOCK.release() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
93 |
|
0 | 94 |
|
95 |
# state related ############################################################### |
|
96 |
||
97 |
def previous_state(session, eid): |
|
98 |
"""return the state of the entity with the given eid, |
|
99 |
usually since it's changing in the current transaction. Due to internal |
|
100 |
relation hooks, the relation may has been deleted at this point, so |
|
101 |
we have handle that |
|
102 |
""" |
|
103 |
for eidfrom, rtype, eidto in reversed(session.query_data('pendingrelations', ())): |
|
104 |
if rtype == 'in_state' and eidfrom == eid: |
|
105 |
rset = session.execute('Any S,N WHERE S eid %(x)s, S name N', |
|
106 |
{'x': eidto}, 'x') |
|
107 |
return rset.get_entity(0, 0) |
|
108 |
rset = session.execute('Any S,N WHERE X eid %(x)s, X in_state S, S name N', |
|
109 |
{'x': eid}, 'x') |
|
110 |
if rset: |
|
111 |
return rset.get_entity(0, 0) |