forked from pinax/pinax-notifications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.py
57 lines (46 loc) · 1.92 KB
/
email.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from django.conf import settings
from django.db.models.loading import get_app
from django.core.urlresolvers import reverse
from django.template.loader import render_to_string
from django.utils.translation import ugettext
from django.contrib.sites.models import Site
from django.core.exceptions import ImproperlyConfigured
from notification import backends
from notification.message import message_to_text
# favour django-mailer but fall back to django.core.mail
try:
mailer = get_app("mailer")
from mailer import send_mail
except ImproperlyConfigured:
from django.core.mail import send_mail
class EmailBackend(backends.BaseBackend):
def can_send(self, user, notice_type):
if should_send(user, notice_type, "1") and user.email:
return True
return False
def deliver(self, recipient, notice_type, extra_context):
# TODO: require this to be passed in extra_context
current_site = Site.objects.get_current()
notices_url = u"http://%s%s" % (
unicode(Site.objects.get_current()),
reverse("notification_notices"),
)
# update context with user specific translations
context = Context({
"user": recipient,
"notice": ugettext(notice_type.display),
"notices_url": notices_url,
"current_site": current_site,
})
context.update(extra_context)
messages = self.get_formatted_messages((
"short.txt",
"full.txt"
), notice_type.label, context)
subject = "".join(render_to_string("notification/email_subject.txt", {
"message": messages["short.txt"],
}, context).splitlines())
body = render_to_string("notification/email_body.txt", {
"message": messages["full.txt"],
}, context)
send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, [recipient])