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

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

Moves the package version to 0.2.0 (for future updates)

  • Property svn:mime-type set to text/plain
RevLine 
[8864]1__version__ = '0.2.0'
[8791]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
[8808]12def cookie_domain(c_domain = None):
[8802]13    try:
[8808]14        return getattr(settings, 'COOKIE_DOMAIN', c_domain)
[8802]15    except AttributeError:
16        import socket
17        return socket.gethostname()
[8791]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.