]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/utils.py
notifications: implement opt-out
[patchwork] / apps / patchwork / utils.py
index 94b3f53851613fdf244cf607e8df0892034ec76e..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,
@@ -181,7 +190,7 @@ def send_notifications():
                                 context)
 
         message = EmailMessage(subject = subject, body = content,
-                               from_email = settings.DEFAULT_FROM_EMAIL,
+                               from_email = settings.NOTIFICATION_FROM_EMAIL,
                                to = [recipient.email],
                                headers = {'Precedence': 'bulk'})
 
@@ -191,6 +200,6 @@ def send_notifications():
             errors.append((recipient, ex))
             continue
 
-        PatchChangeNotification.objects.filter(pk__in = notifications).delete()
+        delete_notifications()
 
     return errors