mail.py
author Alexandre Fayolle <alexandre.fayolle@logilab.fr>
Fri, 23 Apr 2010 12:19:54 +0200
branchstable
changeset 5389 809d3b5b3d31
parent 4835 13b0b96d7982
child 5421 8167de96c523
permissions -rw-r--r--
[python2.6] careful: json-py provides a json package. We don't want that. To simplify usage, import json via cubicweb.web
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
"""Common utilies to format / semd emails.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     3
:organization: Logilab
4212
ab6573088b4a update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3960
diff changeset
     4
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     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 time import time
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
from email.MIMEMultipart import MIMEMultipart
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    13
from email.MIMEText import MIMEText
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    14
from email.MIMEImage import MIMEImage
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    15
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
    16
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
    17
    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
    18
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
    19
    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
    20
        return 'XXX'
0
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 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
    23
from cubicweb.entity import Entity
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
def header(ustring):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
    return Header(ustring.encode('UTF-8'), 'UTF-8')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
def addrheader(uaddr, uname=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
    # 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
    30
    # automatic tests may generate non ascii email address
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1977
diff changeset
    31
    addr = uaddr.encode('UTF-8')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
    if uname:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
        return '%s <%s>' % (header(uname).encode(), addr)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
    return addr
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
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
    37
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
    38
    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
    39
        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
    40
    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
    41
        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
    42
    # 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
    43
    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
    44
    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
    45
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
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
    48
    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
    49
        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
    50
    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
    51
        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
    52
    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
    53
        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
    54
        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
    55
        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
    56
        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
    57
        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
    58
    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
    59
        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
    60
    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
    61
        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
    62
    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
    63
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
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
def format_mail(uinfo, to_addrs, content, subject="",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
                cc_addrs=(), msgid=None, references=(), config=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
    """Sends an Email to 'e_addr' with content 'content', and subject 'subject'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
    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
    70
    name
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
    assert type(content) is unicode, repr(content)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
    msg = MIMEText(content.encode('UTF-8'), 'plain', 'UTF-8')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
    # safety: keep only the first newline
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
    subject = subject.splitlines()[0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
    msg['Subject'] = header(subject)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
    if uinfo.get('email'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
        email = uinfo['email']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
    elif config and config['sender-addr']:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
        email = unicode(config['sender-addr'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
        email = u''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
    if uinfo.get('name'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
        name = uinfo['name']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
    elif config and config['sender-addr']:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
        name = unicode(config['sender-name'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
        name = u''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
    msg['From'] = addrheader(email, name)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
    if config and config['sender-addr'] and config['sender-addr'] != email:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
        appaddr = addrheader(config['sender-addr'], config['sender-name'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
        msg['Reply-to'] = '%s, %s' % (msg['From'], appaddr)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
    elif email:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
        msg['Reply-to'] = msg['From']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
    if config is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
        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
    97
    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
    98
    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
    99
    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
   100
        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
   101
    if msgid:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
        msg['Message-id'] = msgid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
    if references:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
        msg['References'] = ', '.join(references)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
    return msg
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
class HtmlEmail(MIMEMultipart):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
    def __init__(self, subject, textcontent, htmlcontent,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
                 sendermail=None, sendername=None, recipients=None, ccrecipients=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
        MIMEMultipart.__init__(self, 'related')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
        self['Subject'] = header(subject)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
        self.preamble = 'This is a multi-part message in MIME format.'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
        # Attach alternative text message
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
        alternative = MIMEMultipart('alternative')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
        self.attach(alternative)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
        msgtext = MIMEText(textcontent.encode('UTF-8'), 'plain', 'UTF-8')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
        alternative.attach(msgtext)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
        # Attach html message
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
        msghtml = MIMEText(htmlcontent.encode('UTF-8'), 'html', 'UTF-8')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
        alternative.attach(msghtml)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
        if sendermail or sendername:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
            self['From'] = addrheader(sendermail, sendername)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
        if recipients:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
            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
   127
        if ccrecipients:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
            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
   129
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
    def attach_image(self, data, htmlId):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
        image = MIMEImage(data)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
        image.add_header('Content-ID', '<%s>' % htmlId)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
        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
   134
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
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
   137
    """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
   138
    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
   139
    """
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
    # 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
   141
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
    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
   143
3052
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   144
    # 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
   145
    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
   146
        """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
   147
        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
   148
        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
   149
            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
   150
                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
   151
            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
   152
                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
   153
            else:
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   154
                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
   155
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
    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
   157
        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
   158
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
    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
   160
        """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
   161
        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
   162
        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
   163
        if not recipients:
3438
207dac2b03d4 use __regid__, not id
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3418
diff changeset
   164
            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
   165
            return
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3370
diff changeset
   166
        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
   167
            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
   168
            # 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
   169
            # 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
   170
            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
   171
                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
   172
                        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
   173
            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
   174
                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
   175
            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
   176
        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
   177
            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
   178
            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
   179
        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
   180
        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
   181
        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
   182
        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
   183
            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
   184
                # 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
   185
                self._cw = self._cw.hijack_user(something)
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
   186
                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
   187
            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
   188
                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
   189
                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
   190
            # 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
   191
            # 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
   192
            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
   193
            # 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
   194
            #     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
   195
            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
   196
                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
   197
                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
   198
            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
   199
                continue
3960
8cbf18c703be don't fail on error while render a notification mail
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3370
diff changeset
   200
            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
   201
                # shouldn't make the whole transaction fail because of rendering
4583
356f08325072 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   202
                # error (unauthorized or such) XXX check it doesn't actually
356f08325072 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   203
                # 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
   204
                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
   205
                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
   206
            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
   207
                              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
   208
            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
   209
        # 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
   210
        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
   211
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
    # 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
   213
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   214
    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
   215
        """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
   216
        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
   217
        """
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
   218
        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
   219
            '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
   220
            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
   221
        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
   222
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
    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
   224
        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
   225
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
    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
   227
        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
   228
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
    send = send_now
3114
f87fd632e3f6 missing exception
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3113
diff changeset
   230
3052
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   231
    # 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
   232
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   233
    def construct_message_id(self, eid):
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3370
diff changeset
   234
        return construct_message_id(self._cw.vreg.config.appid, eid, 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
   235
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   236
    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
   237
        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
   238
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   239
    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
   240
        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
   241
            '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
   242
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   243
    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
   244
        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
   245
        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
   246
        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
   247
        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
   248
        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
   249
        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
   250
73b1d3746193 reorganize, add some docstring an format_field/format_section convenience methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   251
    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
   252
        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
   253
        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
   254
            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
   255
               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
   256
        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
   257
                       '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
   258
                       '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
   259
                       '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
   260
                       '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
   261
        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
   262
3114
f87fd632e3f6 missing exception
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3113
diff changeset
   263
f87fd632e3f6 missing exception
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3113
diff changeset
   264
class SkipEmail(Exception):
f87fd632e3f6 missing exception
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3113
diff changeset
   265
    """raise this if you decide to skip an email during its generation"""