Skip to content
Browse files

pluggable-backends: Initial cut at abstracting out the send logic int…

…o backends.

git-svn-id: https://django-notification.googlecode.com/svn/branches/pluggable-backends@56 590c3fc9-4838-0410-bb95-17a0c9b37ca9
  • Loading branch information...
1 parent 3b3c767 commit b9971647b2bdb2802b0eb38e4b55c758a4e9b4d5 @brosner brosner committed May 31, 2008
Showing with 65 additions and 26 deletions.
  1. +2 −0 notification/backends/__init__.py
  2. +22 −0 notification/backends/base.py
  3. +36 −0 notification/backends/email.py
  4. +5 −26 notification/models.py
View
2 notification/backends/__init__.py
@@ -0,0 +1,2 @@
+
+from base import BaseBackend
View
22 notification/backends/base.py
@@ -0,0 +1,22 @@
+
+class BaseBackend(object):
+ """
+ The base backend.
+ """
+ def __init__(self):
+ self.recipients = []
+
+ def can_send(self, user, notice_type):
+ """
+ Determines whether this backend is allowed to send a notification to
+ the given user and notice_type.
+ """
+ return False
+
+ def deliver(self, notice_type):
+ """
+ Called once each recipient has been verified with ``can_send``. This
+ will determine the best way to deliver the notification to all
+ recipients.
+ """
+ raise NotImplemented()
View
36 notification/backends/email.py
@@ -0,0 +1,36 @@
+
+from django.conf import settings
+from django.core.urlresolvers import reverse
+from django.template.loader import render_to_string
+from django.utils.translation import ugettext
+
+from notification import backends
+
+# favour django-mailer but fall back to django.core.mail
+try:
+ from mailer import send_mail
+except ImportError:
+ 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, notice_type):
+ notices_url = u"http://%s%s" % (
+ unicode(Site.objects.get_current()),
+ reverse("notification_notices"),
+ )
+ subject = render_to_string("notification/notification_subject.txt", {
+ "display": ugettext(notice_type.display),
+ })
+ message_body = render_to_string("notification/notification_body.txt", {
+ "message": message_to_text(message),
+ "notices_url": notices_url,
+ "contact_email": settings.CONTACT_EMAIL,
+ })
+ send_mail(subject, message_body, settings.DEFAULT_FROM_EMAIL,
+ self.recipients)
View
31 notification/models.py
@@ -4,21 +4,13 @@
from django.conf import settings
from django.db.models import Q
from django.db.models import get_model
-from django.core.urlresolvers import reverse
-from django.template.loader import render_to_string
from django.contrib.sites.models import Site
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ugettext
-# favour django-mailer but fall back to django.core.mail
-try:
- from mailer import send_mail
-except ImportError:
- from django.core.mail import send_mail
-
class NoticeType(models.Model):
@@ -254,29 +246,16 @@ def send(users, notice_type_label, message_template, object_list=None, issue_not
"""
notice_type = NoticeType.objects.get(label=notice_type_label)
message = encode_message(message_template, object_list)
- recipients = []
-
- notices_url = u"http://%s%s" % (
- unicode(Site.objects.get_current()),
- reverse("notification_notices"),
- )
-
- subject = render_to_string("notification/notification_subject.txt", {
- "display": ugettext(notice_type.display),
- })
- message_body = render_to_string("notification/notification_body.txt", {
- "message": message_to_text(message),
- "notices_url": notices_url,
- "contact_email": settings.CONTACT_EMAIL,
- })
for user in users:
if issue_notice:
notice = Notice(user=user, message=message, notice_type=notice_type)
notice.save()
- if should_send(user, notice_type, "1") and user.email: # Email
- recipients.append(user.email)
- send_mail(subject, message_body, settings.DEFAULT_FROM_EMAIL, recipients)
+ for backend in backends:
+ if backend.can_send(user, notice_type):
+ backend.recipients.append(user)
+ for backend in backends:
+ backend.deliver(notice_type)
def notices_for(user, archived=False):

0 comments on commit b997164

Please sign in to comment.
Something went wrong with that request. Please try again.