Mercurial > public > sg101
comparison gpp/core/functions.py @ 513:9e7ae8462f3f
Using celery to send mail out of the request/response cycle.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Tue, 13 Dec 2011 02:07:55 +0000 |
parents | 20af29454e83 |
children | 82b97697312e |
comparison
equal
deleted
inserted
replaced
512:3fd369e1197f | 513:9e7ae8462f3f |
---|---|
1 """This file houses various core utility functions for GPP""" | 1 """This file houses various core utility functions for GPP""" |
2 import datetime | 2 import datetime |
3 import re | 3 import re |
4 import logging | 4 import logging |
5 | 5 |
6 import django.core.mail | |
7 from django.contrib.sites.models import Site | 6 from django.contrib.sites.models import Site |
8 from django.conf import settings | 7 from django.conf import settings |
9 | 8 |
10 import mailer | 9 import core.tasks |
11 | 10 |
12 | 11 |
13 def send_mail(subject, message, from_email, recipient_list, | 12 def send_mail(subject, message, from_email, recipient_list, **kwargs): |
14 fail_silently=False, auth_user=None, auth_password=None, | |
15 expedite=False): | |
16 """The main gpp send email function. | |
17 Use this function to send email from the site. It will obey debug settings and | |
18 log all emails. The expedite flag, when True, will bypass the mail queue. | |
19 """ | 13 """ |
14 The main send email function. Use this function to send email from the | |
15 site. All applications should use this function instead of calling | |
16 Django's directly. | |
17 If settings.GPP_SEND_EMAIL is true, the email will be sent to a Celery task | |
18 to actually send the email. Otherwise it is dropped. In any event, the | |
19 email will be logged at the DEBUG level. | |
20 | |
21 """ | |
22 # Guard against empty email addresses | |
20 recipient_list = [dest for dest in recipient_list if dest] | 23 recipient_list = [dest for dest in recipient_list if dest] |
21 if not recipient_list: | 24 if not recipient_list: |
22 logging.warning("Empty recipient_list in send_mail") | 25 logging.warning("Empty recipient_list in send_mail") |
23 return | 26 return |
24 | 27 |
25 if settings.MAILER_ENQUEUE_MAIL and not expedite: | |
26 mailer.enqueue_mail(subject, message, from_email, recipient_list) | |
27 elif settings.GPP_SEND_EMAIL: | |
28 django.core.mail.send_mail(subject, message, from_email, recipient_list, | |
29 fail_silently, auth_user, auth_password) | |
30 | |
31 logging.debug('EMAIL:\nFrom: %s\nTo: %s\nSubject: %s\nMessage:\n%s', | 28 logging.debug('EMAIL:\nFrom: %s\nTo: %s\nSubject: %s\nMessage:\n%s', |
32 from_email, str(recipient_list), subject, message) | 29 from_email, str(recipient_list), subject, message) |
30 | |
31 if settings.GPP_SEND_EMAIL: | |
32 core.tasks.send_mail.delay(subject, message, from_email, recipient_list, | |
33 **kwargs) | |
33 | 34 |
34 | 35 |
35 def email_admins(subject, message): | 36 def email_admins(subject, message): |
36 """Emails the site admins. Goes through the site send_mail function.""" | 37 """Emails the site admins. Goes through the site send_mail function.""" |
37 site = Site.objects.get_current() | 38 site = Site.objects.get_current() |