diff options
author | Jeremy Kerr <jk@ozlabs.org> | 2011-09-19 09:42:44 +0800 |
---|---|---|
committer | Jeremy Kerr <jk@ozlabs.org> | 2011-09-19 09:42:44 +0800 |
commit | 75d8cf966034e673afe0077ba393d8b2eb3e9b93 (patch) | |
tree | 3f6cf6b9f87ad313d23de77b4c33422e7444a1df /apps/patchwork/utils.py | |
parent | 539b6596dc1bf1d3118631095625e354026da373 (diff) | |
parent | f1e5f6a2c9d737f12290f5bd5a934b74c362616f (diff) | |
download | patchwork-75d8cf966034e673afe0077ba393d8b2eb3e9b93.tar.bz2 patchwork-75d8cf966034e673afe0077ba393d8b2eb3e9b93.tar.xz |
Merge branch 'notifications'
Diffstat (limited to 'apps/patchwork/utils.py')
-rw-r--r-- | apps/patchwork/utils.py | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/apps/patchwork/utils.py b/apps/patchwork/utils.py index 5a8e4c0..e7619c3 100644 --- a/apps/patchwork/utils.py +++ b/apps/patchwork/utils.py @@ -18,8 +18,17 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -from patchwork.models import Bundle, Project, BundlePatch +import itertools +import datetime from django.shortcuts import get_object_or_404 +from django.template.loader import render_to_string +from django.contrib.sites.models import Site +from django.conf import settings +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, EmailOptout def get_patch_ids(d, prefix = 'patch_id'): ids = [] @@ -136,3 +145,60 @@ def set_bundle(user, project, action, data, patches, context): bundle.save() return [] + +def send_notifications(): + date_limit = datetime.datetime.now() - \ + datetime.timedelta(minutes = + settings.NOTIFICATION_DELAY_MINUTES) + + # This gets funky: we want to filter out any notifications that should + # be grouped with other notifications that aren't ready to go out yet. To + # do that, we join back onto PatchChangeNotification (PCN -> Patch -> + # Person -> Patch -> max(PCN.last_modified)), filtering out any maxima + # that are with the date_limit. + qs = PatchChangeNotification.objects \ + .annotate(m = Max('patch__submitter__patch__patchchangenotification' + '__last_modified')) \ + .filter(m__lt = date_limit) + + groups = itertools.groupby(qs.order_by('patch__submitter'), + lambda n: n.patch.submitter) + + errors = [] + + 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, + 'notifications': notifications, + } + subject = render_to_string( + 'patchwork/patch-change-notification-subject.text', + context).strip() + content = render_to_string('patchwork/patch-change-notification.mail', + context) + + message = EmailMessage(subject = subject, body = content, + from_email = settings.NOTIFICATION_FROM_EMAIL, + to = [recipient.email], + headers = {'Precedence': 'bulk'}) + + try: + message.send() + except ex: + errors.append((recipient, ex)) + continue + + delete_notifications() + + return errors |