source: mauRepo/dj_security/trunk/dj_security/__init__.py @ 8844

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/dj_security/trunk/dj_security/__init__.py@8844
Revision 8844, 1.8 KB checked in by mnagni, 6 years ago (diff)

Advance the version to 0.0.5 for release

  • Property svn:mime-type set to text/plain
Line 
1__version__ = '0.0.5'
2
3from django.core.mail.message import EmailMultiAlternatives
4import smtplib
5import logging
6from django.conf import settings
7
8
9# Get an instance of a logger
10LOGGER = logging.getLogger(__name__)
11
12def cookie_domain(c_domain = None):
13    try:
14        return getattr(settings, 'COOKIE_DOMAIN', c_domain)
15    except AttributeError:
16        import socket
17        return socket.gethostname()
18
19def reset_password():
20    return getattr(settings, 'CC_RESET_PASSWORD', [])
21
22def _redirect_field_name():
23    return getattr(settings, 'REDIRECT_FIELD_NAME', 'r')
24
25def _token_field_name():
26    return getattr(settings, 'TOKEN_FIELD_NAME', 't')
27
28def auth_tkt_name():
29    return getattr(settings, 'AUTH_TKT_NAME', 'auth_tkt')
30
31def shared_secret():
32    return getattr(settings, 'SECURITY_SHAREDSECRET', 'sharedsecret')
33
34def ds_send_mail(mail_subject, msg, mail_from, to_email,
35                 log_msgs = [], attachments = None, send_html=True, cc=None):
36    '''
37    **attachment** is a list of triples (filename, content, mimetype)
38    '''
39    try:       
40        email = EmailMultiAlternatives(mail_subject,
41                                       msg,
42                                       mail_from,
43                                       to_email,
44                                       cc=cc)
45        if attachments:
46            for att in attachments:
47                email.attach(att[0], att[1], att[2])
48       
49        if send_html:       
50            html_msg = "<html><head></head><body>" + msg + "</body></html>"
51            email.attach_alternative(html_msg, "text/html")
52                           
53               
54        email.message().as_string()
55        email.send()       
56    except smtplib.SMTPException:
57        for lmsg in log_msgs:
58            LOGGER.error("Error sending email %s" % lmsg)
59    for lmsg in log_msgs:
60        LOGGER.info(lmsg)
Note: See TracBrowser for help on using the repository browser.