]> git.ozlabs.org Git - patchwork/commitdiff
notifications: implement opt-out notifications
authorJeremy Kerr <jk@ozlabs.org>
Thu, 14 Apr 2011 11:37:55 +0000 (19:37 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Thu, 14 Apr 2011 11:37:55 +0000 (19:37 +0800)
Check for opt-out status before sending notification mail.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
apps/patchwork/models.py
apps/patchwork/tests/notifications.py
apps/patchwork/utils.py

index 17a68db8077ea6332e53aba419cca2e024c41a2c..22062c2189f468eb20e4b8ad2ff1f9e1a16b3632 100644 (file)
@@ -408,6 +408,11 @@ class EmailOptout(models.Model):
     def __unicode__(self):
         return self.email
 
+    @classmethod
+    def is_optout(cls, email):
+        email = email.lower().strip()
+        return cls.objects.filter(email = email).count() > 0
+
 class PatchChangeNotification(models.Model):
     patch = models.ForeignKey(Patch, primary_key = True)
     last_modified = models.DateTimeField(default = datetime.datetime.now)
index ae379889bb100f52b75b9e0a586318d9280324fe..f14b30b5c2837638c15cab88d4d55777cb770b63 100644 (file)
@@ -23,7 +23,7 @@ from django.core.urlresolvers import reverse
 from django.core import mail
 from django.conf import settings
 from django.db.utils import IntegrityError
-from patchwork.models import Patch, State, PatchChangeNotification
+from patchwork.models import Patch, State, PatchChangeNotification, EmailOptout
 from patchwork.tests.utils import defaults, create_maintainer
 from patchwork.utils import send_notifications
 
@@ -172,6 +172,18 @@ class PatchNotificationEmailTest(TestCase):
         self.assertEquals(msg.to, [self.submitter.email])
         self.assertTrue(self.patch.get_absolute_url() in msg.body)
 
+    def testNotificationOptout(self):
+        """ensure opt-out addresses don't get notifications"""
+        PatchChangeNotification(patch = self.patch,
+                               orig_state = self.patch.state).save()
+        self._expireNotifications()
+
+        EmailOptout(email = self.submitter.email).save()
+
+        errors = send_notifications()
+        self.assertEquals(errors, [])
+        self.assertEquals(len(mail.outbox), 0)
+
     def testNotificationMerge(self):
         patches = [self.patch,
                    Patch(project = self.project, msgid = 'testpatch-2',
index 58edb194564f1f4167dbac2e338629ce61f90f5e..5cb45e8eeaa41191955ead4a9fea3ca434dbb81d 100644 (file)
@@ -28,7 +28,7 @@ from django.core.mail import EmailMessage
 from django.db.models import Max
 from patchwork.forms import MultiplePatchForm
 from patchwork.models import Bundle, Project, BundlePatch, UserProfile, \
-        PatchChangeNotification
+        PatchChangeNotification, EmailOptout
 
 def get_patch_ids(d, prefix = 'patch_id'):
     ids = []
@@ -169,6 +169,15 @@ def send_notifications():
 
     for (recipient, notifications) in groups:
         notifications = list(notifications)
+
+        def delete_notifications():
+            PatchChangeNotification.objects.filter(
+                                pk__in = notifications).delete()
+
+        if EmailOptout.is_optout(recipient.email):
+            delete_notifications()
+            continue
+
         context = {
             'site': Site.objects.get_current(),
             'person': recipient,
@@ -191,6 +200,6 @@ def send_notifications():
             errors.append((recipient, ex))
             continue
 
-        PatchChangeNotification.objects.filter(pk__in = notifications).delete()
+        delete_notifications()
 
     return errors