author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 30 Sep 2009 16:05:43 +0200 | |
branch | stable |
changeset 3526 | dfb2ebb765e2 |
parent 3370 | 62ed9981a770 |
child 3418 | 7b49fa7e942d |
child 3960 | 8cbf18c703be |
permissions | -rw-r--r-- |
0 | 1 |
"""Common utilies to format / semd emails. |
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 |
||
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
10 |
from base64 import b64encode, b64decode |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
11 |
from itertools import repeat |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
12 |
from time import time |
0 | 13 |
from email.MIMEMultipart import MIMEMultipart |
14 |
from email.MIMEText import MIMEText |
|
15 |
from email.MIMEImage import MIMEImage |
|
16 |
from email.Header import Header |
|
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
17 |
try: |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
18 |
from socket import gethostname |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
19 |
except ImportError: |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
20 |
def gethostname(): # gae |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
21 |
return 'XXX' |
0 | 22 |
|
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
23 |
from cubicweb.view import EntityView |
3112
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
24 |
from cubicweb.entity import Entity |
0 | 25 |
|
26 |
def header(ustring): |
|
27 |
return Header(ustring.encode('UTF-8'), 'UTF-8') |
|
28 |
||
29 |
def addrheader(uaddr, uname=None): |
|
30 |
# even if an email address should be ascii, encode it using utf8 since |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2172
diff
changeset
|
31 |
# automatic tests may generate non ascii email address |
2172
cf8f9180e63e
delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
32 |
addr = uaddr.encode('UTF-8') |
0 | 33 |
if uname: |
34 |
return '%s <%s>' % (header(uname).encode(), addr) |
|
35 |
return addr |
|
36 |
||
37 |
||
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
38 |
def construct_message_id(appid, eid, withtimestamp=True): |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
39 |
if withtimestamp: |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
40 |
addrpart = 'eid=%s×tamp=%.10f' % (eid, time()) |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
41 |
else: |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
42 |
addrpart = 'eid=%s' % eid |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
43 |
# we don't want any equal sign nor trailing newlines |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
44 |
leftpart = b64encode(addrpart, '.-').rstrip().rstrip('=') |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
45 |
return '<%s@%s.%s>' % (leftpart, appid, gethostname()) |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
46 |
|
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
47 |
|
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
48 |
def parse_message_id(msgid, appid): |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
49 |
if msgid[0] == '<': |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
50 |
msgid = msgid[1:] |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
51 |
if msgid[-1] == '>': |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
52 |
msgid = msgid[:-1] |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
53 |
try: |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
54 |
values, qualif = msgid.split('@') |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
55 |
padding = len(values) % 4 |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
56 |
values = b64decode(str(values + '='*padding), '.-') |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
57 |
values = dict(v.split('=') for v in values.split('&')) |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
58 |
fromappid, host = qualif.split('.', 1) |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
59 |
except: |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
60 |
return None |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
61 |
if appid != fromappid or host != gethostname(): |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
62 |
return None |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
63 |
return values |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
64 |
|
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
65 |
|
0 | 66 |
def format_mail(uinfo, to_addrs, content, subject="", |
67 |
cc_addrs=(), msgid=None, references=(), config=None): |
|
68 |
"""Sends an Email to 'e_addr' with content 'content', and subject 'subject' |
|
69 |
||
70 |
to_addrs and cc_addrs are expected to be a list of email address without |
|
71 |
name |
|
72 |
""" |
|
73 |
assert type(content) is unicode, repr(content) |
|
74 |
msg = MIMEText(content.encode('UTF-8'), 'plain', 'UTF-8') |
|
75 |
# safety: keep only the first newline |
|
76 |
subject = subject.splitlines()[0] |
|
77 |
msg['Subject'] = header(subject) |
|
78 |
if uinfo.get('email'): |
|
79 |
email = uinfo['email'] |
|
80 |
elif config and config['sender-addr']: |
|
81 |
email = unicode(config['sender-addr']) |
|
82 |
else: |
|
83 |
email = u'' |
|
84 |
if uinfo.get('name'): |
|
85 |
name = uinfo['name'] |
|
86 |
elif config and config['sender-addr']: |
|
87 |
name = unicode(config['sender-name']) |
|
88 |
else: |
|
89 |
name = u'' |
|
90 |
msg['From'] = addrheader(email, name) |
|
91 |
if config and config['sender-addr'] and config['sender-addr'] != email: |
|
92 |
appaddr = addrheader(config['sender-addr'], config['sender-name']) |
|
93 |
msg['Reply-to'] = '%s, %s' % (msg['From'], appaddr) |
|
94 |
elif email: |
|
95 |
msg['Reply-to'] = msg['From'] |
|
96 |
if config is not None: |
|
97 |
msg['X-CW'] = config.appid |
|
425
cc9e8986d55e
sort email addresses and remove doubled ones before sending the email
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
98 |
unique_addrs = lambda addrs: sorted(set(addr for addr in addrs if addr is not None)) |
cc9e8986d55e
sort email addresses and remove doubled ones before sending the email
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
99 |
msg['To'] = ', '.join(addrheader(addr) for addr in unique_addrs(to_addrs)) |
0 | 100 |
if cc_addrs: |
425
cc9e8986d55e
sort email addresses and remove doubled ones before sending the email
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
101 |
msg['Cc'] = ', '.join(addrheader(addr) for addr in unique_addrs(cc_addrs)) |
0 | 102 |
if msgid: |
103 |
msg['Message-id'] = msgid |
|
104 |
if references: |
|
105 |
msg['References'] = ', '.join(references) |
|
106 |
return msg |
|
107 |
||
108 |
||
109 |
class HtmlEmail(MIMEMultipart): |
|
110 |
||
111 |
def __init__(self, subject, textcontent, htmlcontent, |
|
112 |
sendermail=None, sendername=None, recipients=None, ccrecipients=None): |
|
113 |
MIMEMultipart.__init__(self, 'related') |
|
114 |
self['Subject'] = header(subject) |
|
115 |
self.preamble = 'This is a multi-part message in MIME format.' |
|
116 |
# Attach alternative text message |
|
117 |
alternative = MIMEMultipart('alternative') |
|
118 |
self.attach(alternative) |
|
119 |
msgtext = MIMEText(textcontent.encode('UTF-8'), 'plain', 'UTF-8') |
|
120 |
alternative.attach(msgtext) |
|
121 |
# Attach html message |
|
122 |
msghtml = MIMEText(htmlcontent.encode('UTF-8'), 'html', 'UTF-8') |
|
123 |
alternative.attach(msghtml) |
|
124 |
if sendermail or sendername: |
|
125 |
self['From'] = addrheader(sendermail, sendername) |
|
126 |
if recipients: |
|
127 |
self['To'] = ', '.join(addrheader(addr) for addr in recipients if addr is not None) |
|
128 |
if ccrecipients: |
|
129 |
self['Cc'] = ', '.join(addrheader(addr) for addr in ccrecipients if addr is not None) |
|
130 |
||
131 |
def attach_image(self, data, htmlId): |
|
132 |
image = MIMEImage(data) |
|
133 |
image.add_header('Content-ID', '<%s>' % htmlId) |
|
134 |
self.attach(image) |
|
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
135 |
|
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
136 |
|
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
137 |
class NotificationView(EntityView): |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
138 |
"""abstract view implementing the "email" API (eg to simplify sending |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
139 |
notification) |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
140 |
""" |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
141 |
# XXX refactor this class to work with len(rset) > 1 |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
142 |
|
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
143 |
msgid_timestamp = True |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
144 |
|
3052
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
145 |
# this is usually the method to call |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
146 |
def render_and_send(self, **kwargs): |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
147 |
"""generate and send an email message for this view""" |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
148 |
delayed = kwargs.pop('delay_to_commit', None) |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
149 |
for recipients, msg in self.render_emails(**kwargs): |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
150 |
if delayed is None: |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
151 |
self.send(recipients, msg) |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
152 |
elif delayed: |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
153 |
self.send_on_commit(recipients, msg) |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
154 |
else: |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
155 |
self.send_now(recipients, msg) |
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
156 |
|
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
157 |
def cell_call(self, row, col=0, **kwargs): |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
158 |
self.w(self.req._(self.content) % self.context(**kwargs)) |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
159 |
|
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
160 |
def render_emails(self, **kwargs): |
3112
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
161 |
"""generate and send emails for this view (one per recipient)""" |
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
162 |
self._kwargs = kwargs |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
163 |
recipients = self.recipients() |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
164 |
if not recipients: |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
165 |
self.info('skipping %s notification, no recipients', self.id) |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
166 |
return |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
167 |
if self.rset is not None: |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
168 |
entity = self.entity(self.row or 0, self.col or 0) |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
169 |
# if the view is using timestamp in message ids, no way to reference |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
170 |
# previous email |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
171 |
if not self.msgid_timestamp: |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
172 |
refs = [self.construct_message_id(eid) |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
173 |
for eid in entity.notification_references(self)] |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
174 |
else: |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
175 |
refs = () |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
176 |
msgid = self.construct_message_id(entity.eid) |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
177 |
else: |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
178 |
refs = () |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
179 |
msgid = None |
3112
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
180 |
req = self.req |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
181 |
self.user_data = req.user_data() |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
182 |
origlang = req.lang |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
183 |
for something in recipients: |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
184 |
if isinstance(something, Entity): |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
185 |
# hi-jack self.req to get a session for the returned user |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
186 |
self.req = self.req.hijack_user(something) |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
187 |
emailaddr = something.get_email() |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
188 |
else: |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
189 |
emailaddr, lang = something |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
190 |
self.req.set_language(lang) |
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
191 |
# since the same view (eg self) may be called multiple time and we |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
192 |
# need a fresh stream at each iteration, reset it explicitly |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
193 |
self.w = None |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
194 |
# XXX call render before subject to set .row/.col attributes on the |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
195 |
# view |
3112
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
196 |
try: |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
197 |
content = self.render(row=0, col=0, **kwargs) |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
198 |
subject = self.subject() |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
199 |
except SkipEmail: |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
200 |
continue |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
201 |
msg = format_mail(self.user_data, [emailaddr], content, subject, |
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
202 |
config=self.config, msgid=msgid, references=refs) |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
203 |
yield [emailaddr], msg |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
204 |
# restore language |
3112
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
205 |
req.set_language(origlang) |
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
206 |
|
3052
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
207 |
# recipients / email sending ############################################### |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
208 |
|
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
209 |
def recipients(self): |
3110
757d36162235
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3052
diff
changeset
|
210 |
"""return a list of either 2-uple (email, language) or user entity to |
757d36162235
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3052
diff
changeset
|
211 |
who this email should be sent |
3052
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
212 |
""" |
3370
62ed9981a770
[notification] use super_session to select the recipients finder
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3120
diff
changeset
|
213 |
# use super_session when available, we don't want to consider security |
62ed9981a770
[notification] use super_session to select the recipients finder
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3120
diff
changeset
|
214 |
# when selecting recipients_finder |
62ed9981a770
[notification] use super_session to select the recipients finder
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3120
diff
changeset
|
215 |
try: |
62ed9981a770
[notification] use super_session to select the recipients finder
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3120
diff
changeset
|
216 |
req = self.req.super_session |
62ed9981a770
[notification] use super_session to select the recipients finder
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3120
diff
changeset
|
217 |
except AttributeError: |
62ed9981a770
[notification] use super_session to select the recipients finder
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3120
diff
changeset
|
218 |
req = self.req |
62ed9981a770
[notification] use super_session to select the recipients finder
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3120
diff
changeset
|
219 |
finder = self.vreg['components'].select('recipients_finder', req, |
3052
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
220 |
rset=self.rset, |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
221 |
row=self.row or 0, |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
222 |
col=self.col or 0) |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
223 |
return finder.recipients() |
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
224 |
|
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
225 |
def send_now(self, recipients, msg): |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
226 |
self.config.sendmails([(msg, recipients)]) |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
227 |
|
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
228 |
def send_on_commit(self, recipients, msg): |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
229 |
raise NotImplementedError |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
230 |
|
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
231 |
send = send_now |
3114
f87fd632e3f6
missing exception
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3113
diff
changeset
|
232 |
|
3052
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
233 |
# email generation helpers ################################################# |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
234 |
|
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
235 |
def construct_message_id(self, eid): |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
236 |
return construct_message_id(self.config.appid, eid, self.msgid_timestamp) |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
237 |
|
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
238 |
def format_field(self, attr, value): |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
239 |
return ':%(attr)s: %(value)s' % {'attr': attr, 'value': value} |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
240 |
|
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
241 |
def format_section(self, attr, value): |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
242 |
return '%(attr)s\n%(ul)s\n%(value)s\n' % { |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
243 |
'attr': attr, 'ul': '-'*len(attr), 'value': value} |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
244 |
|
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
245 |
def subject(self): |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
246 |
entity = self.entity(self.row or 0, self.col or 0) |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
247 |
subject = self.req._(self.message) |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
248 |
etype = entity.dc_type() |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
249 |
eid = entity.eid |
3110
757d36162235
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3052
diff
changeset
|
250 |
login = self.user_data['login'] |
3052
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
251 |
return self.req._('%(subject)s %(etype)s #%(eid)s (%(login)s)') % locals() |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
252 |
|
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
253 |
def context(self, **kwargs): |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
254 |
entity = self.entity(self.row or 0, self.col or 0) |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
255 |
for key, val in kwargs.iteritems(): |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
256 |
if val and isinstance(val, unicode) and val.strip(): |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
257 |
kwargs[key] = self.req._(val) |
3110
757d36162235
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3052
diff
changeset
|
258 |
kwargs.update({'user': self.user_data['login'], |
3052
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
259 |
'eid': entity.eid, |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
260 |
'etype': entity.dc_type(), |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
261 |
'url': entity.absolute_url(), |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
262 |
'title': entity.dc_long_title(),}) |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
263 |
return kwargs |
73b1d3746193
reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2879
diff
changeset
|
264 |
|
3114
f87fd632e3f6
missing exception
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3113
diff
changeset
|
265 |
|
f87fd632e3f6
missing exception
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3113
diff
changeset
|
266 |
class SkipEmail(Exception): |
f87fd632e3f6
missing exception
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3113
diff
changeset
|
267 |
"""raise this if you decide to skip an email during its generation""" |