mail.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Thu, 12 Aug 2010 16:50:52 +0200
branchstable
changeset 6106 1e6d93f70d14
parent 5736 375819ec7d43
child 7815 2a164a9cf81c
permissions -rw-r--r--
[selectors] fix match_transition & introduce new edited_attribute selector
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5733
d00d1fab42af cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5579
diff changeset
    18
"""Common utilies to format / semd emails."""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
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
    22
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
    23
from time import time
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
from email.MIMEMultipart import MIMEMultipart
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
from email.MIMEText import MIMEText
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
from email.MIMEImage import MIMEImage
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
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
    28
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
    29
    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
    30
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
    31
    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
    32
        return 'XXX'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
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
    34
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
    35
from cubicweb.entity import Entity
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
def header(ustring):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
    return Header(ustring.encode('UTF-8'), 'UTF-8')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
def addrheader(uaddr, uname=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
    # 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
    42
    # automatic tests may generate non ascii email address
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1977
diff changeset
    43
    addr = uaddr.encode('UTF-8')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
    if uname:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
        return '%s <%s>' % (header(uname).encode(), addr)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
    return addr
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
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
    49
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
    50
    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
    51
        addrpart = 'eid=%s&timestamp=%.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
    52
    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
    53
        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
    54
    # 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
    55
    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
    56
    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
    57
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
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
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
    60
    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
    61
        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
    62
    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
    63
        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
    64
    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
    65
        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
    66
        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
    67
        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
    68
        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
    69
        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
    70
    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
    71
        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
    72
    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
    73
        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
    74
    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
    75
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
    76
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
def format_mail(uinfo, to_addrs, content, subject="",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
                cc_addrs=(), msgid=None, references=(), config=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
    """Sends an Email to 'e_addr' with content 'content', and subject 'subject'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
    to_addrs and cc_addrs are expected to be a list of email address without
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
    name
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
    assert type(content) is unicode, repr(content)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
    msg = MIMEText(content.encode('UTF-8'), 'plain', 'UTF-8')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
    # safety: keep only the first newline
5579
297b2002a083 fix 978144: exception with view send-email if empty subject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    87
    try:
297b2002a083 fix 978144: exception with view send-email if empty subject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    88
        subject = subject.splitlines()[0]
297b2002a083 fix 978144: exception with view send-email if empty subject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    89
        msg['Subject'] = header(subject)
297b2002a083 fix 978144: exception with view send-email if empty subject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    90
    except IndexError:
297b2002a083 fix 978144: exception with view send-email if empty subject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    91
        pass # no subject
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
    if uinfo.get('email'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
        email = uinfo['email']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
    elif config and config['sender-addr']:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
        email = unicode(config['sender-addr'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
        email = u''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
    if uinfo.get('name'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
        name = uinfo['name']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
    elif config and config['sender-addr']:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
        name = unicode(config['sender-name'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
        name = u''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
    msg['From'] = addrheader(email, name)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
    if config and config['sender-addr'] and config['sender-addr'] != email:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
        appaddr = addrheader(config['sender-addr'], config['sender-name'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
        msg['Reply-to'] = '%s, %s' % (msg['From'], appaddr)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
    elif email:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
        msg['Reply-to'] = msg['From']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
    if config is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
        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
   112
    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
   113
    msg['To'] = ', '.join(addrheader(addr) for addr in unique_addrs(to_addrs))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
    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
   115
        msg['Cc'] = ', '.join(addrheader(addr) for addr in unique_addrs(cc_addrs))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
    if msgid:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
        msg['Message-id'] = msgid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
    if references:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
        msg['References'] = ', '.join(references)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
    return msg
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
class HtmlEmail(MIMEMultipart):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
    def __init__(self, subject, textcontent, htmlcontent,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
                 sendermail=None, sendername=None, recipients=None, ccrecipients=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
        MIMEMultipart.__init__(self, 'related')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
        self['Subject'] = header(subject)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
        self.preamble = 'This is a multi-part message in MIME format.'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
        # Attach alternative text message
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
        alternative = MIMEMultipart('alternative')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
        self.attach(alternative)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
        msgtext = MIMEText(textcontent.encode('UTF-8'), 'plain', 'UTF-8')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
        alternative.attach(msgtext)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
        # Attach html message
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
        msghtml = MIMEText(htmlcontent.encode('UTF-8'), 'html', 'UTF-8')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
        alternative.attach(msghtml)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
        if sendermail or sendername:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
            self['From'] = addrheader(sendermail, sendername)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
        if recipients:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
            self['To'] = ', '.join(addrheader(addr) for addr in recipients if addr is not None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
        if ccrecipients:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
            self['Cc'] = ', '.join(addrheader(addr) for addr in ccrecipients if addr is not None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
    def attach_image(self, data, htmlId):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
        image = MIMEImage(data)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
        image.add_header('Content-ID', '<%s>' % htmlId)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
        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
   149
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
   150
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
   151
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
   152
    """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
   153
    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
   154
    """
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
   155
    # 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
   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
    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
   158
3052
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   159
    # 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
   160
    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
   161
        """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
   162
        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
   163
        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
   164
            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
   165
                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
   166
            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
   167
                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
   168
            else:
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   169
                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
   170
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
    def cell_call(self, row, col=0, **kwargs):
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3370
diff changeset
   172
        self.w(self._cw._(self.content) % self.context(**kwargs))
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
   173
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
    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
   175
        """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
   176
        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
   177
        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
   178
        if not recipients:
3438
207dac2b03d4 use __regid__, not id
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3418
diff changeset
   179
            self.info('skipping %s notification, no recipients', self.__regid__)
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
   180
            return
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3370
diff changeset
   181
        if self.cw_rset is not None:
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3370
diff changeset
   182
            entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
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
   183
            # 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
   184
            # 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
   185
            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
   186
                refs = [self.construct_message_id(eid)
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   187
                        for eid in entity.cw_adapt_to('INotifiable').notification_references(self)]
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
   188
            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
   189
                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
   190
            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
   191
        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
   192
            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
   193
            msgid = None
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3370
diff changeset
   194
        req = self._cw
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
   195
        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
   196
        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
   197
        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
   198
            if isinstance(something, Entity):
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3370
diff changeset
   199
                # hi-jack self._cw to get a session for the returned user
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3370
diff changeset
   200
                self._cw = self._cw.hijack_user(something)
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   201
                emailaddr = something.cw_adapt_to('IEmailable').get_email()
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
   202
            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
   203
                emailaddr, lang = something
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3370
diff changeset
   204
                self._cw.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
   205
            # 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
   206
            # 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
   207
            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
   208
            # 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
   209
            #     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
   210
            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
   211
                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
   212
                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
   213
            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
   214
                continue
3960
8cbf18c703be don't fail on error while render a notification mail
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3370
diff changeset
   215
            except Exception, ex:
8cbf18c703be don't fail on error while render a notification mail
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3370
diff changeset
   216
                # shouldn't make the whole transaction fail because of rendering
4583
356f08325072 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   217
                # error (unauthorized or such) XXX check it doesn't actually
356f08325072 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   218
                # occurs due to rollback on such error
3960
8cbf18c703be don't fail on error while render a notification mail
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3370
diff changeset
   219
                self.exception(str(ex))
8cbf18c703be don't fail on error while render a notification mail
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3370
diff changeset
   220
                continue
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
   221
            msg = format_mail(self.user_data, [emailaddr], content, subject,
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3370
diff changeset
   222
                              config=self._cw.vreg.config, msgid=msgid, references=refs)
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
   223
            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
   224
        # 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
   225
        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
   226
3052
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   227
    # 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
   228
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   229
    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
   230
        """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
   231
        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
   232
        """
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   233
        finder = self._cw.vreg['components'].select(
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   234
            'recipients_finder', self._cw, rset=self.cw_rset,
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   235
            row=self.cw_row or 0, col=self.cw_col or 0)
3052
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 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
   237
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
   238
    def send_now(self, recipients, msg):
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3370
diff changeset
   239
        self._cw.vreg.config.sendmails([(msg, 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
   240
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
   241
    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
   242
        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
   243
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
   244
    send = send_now
3114
f87fd632e3f6 missing exception
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3113
diff changeset
   245
3052
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   246
    # 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
   247
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   248
    def construct_message_id(self, eid):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   249
        return construct_message_id(self._cw.vreg.config.appid, eid,
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   250
                                    self.msgid_timestamp)
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
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   252
    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
   253
        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
   254
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   255
    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
   256
        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
   257
            '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
   258
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   259
    def subject(self):
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3370
diff changeset
   260
        entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3370
diff changeset
   261
        subject = self._cw._(self.message)
3052
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   262
        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
   263
        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
   264
        login = self.user_data['login']
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3370
diff changeset
   265
        return self._cw._('%(subject)s %(etype)s #%(eid)s (%(login)s)') % locals()
3052
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   266
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   267
    def context(self, **kwargs):
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3370
diff changeset
   268
        entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
3052
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   269
        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
   270
            if val and isinstance(val, unicode) and val.strip():
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3370
diff changeset
   271
               kwargs[key] = self._cw._(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
   272
        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
   273
                       '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
   274
                       '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
   275
                       '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
   276
                       '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
   277
        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
   278
3114
f87fd632e3f6 missing exception
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3113
diff changeset
   279
f87fd632e3f6 missing exception
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3113
diff changeset
   280
class SkipEmail(Exception):
f87fd632e3f6 missing exception
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3113
diff changeset
   281
    """raise this if you decide to skip an email during its generation"""