summaryrefslogtreecommitdiff
path: root/pgcommitfest/commitfest/management/commands/send_notifications.py
blob: 634af587e980647f07b9d16f4c6416d7f38f1e20 (plain)
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
from django.core.management.base import BaseCommand
from django.db import transaction
from django.conf import settings

from StringIO import StringIO

from pgcommitfest.commitfest.models import PendingNotification
from pgcommitfest.mailqueue.util import send_template_mail

class Command(BaseCommand):
	help = "Send queued notifications"

	def handle(self, *args, **options):
		with transaction.atomic():
			# Django doesn't do proper group by in the ORM, so we have to
			# build our own.
			matches = {}
			for n in PendingNotification.objects.all().order_by('user', 'history__patch__id', 'history__id'):
				if not matches.has_key(n.user.id):
					matches[n.user.id] = {'user': n.user, 'patches': {}}
				if not matches[n.user.id]['patches'].has_key(n.history.patch.id):
					matches[n.user.id]['patches'][n.history.patch.id] = {'patch': n.history.patch, 'entries': []}
				matches[n.user.id]['patches'][n.history.patch.id]['entries'].append(n.history)
				n.delete()

			# Ok, now let's build emails from this
			for v in matches.values():
				user = v['user']
				email = user.email
				if user.userprofile and user.userprofile.notifyemail:
					email = user.userprofile.notifyemail.email

				send_template_mail(settings.NOTIFICATION_FROM,
								   None,
								   email,
								   "PostgreSQL commitfest updates",
								   'mail/patch_notify.txt',
								   {
									   'user': user,
									   'patches': v['patches'],
								   },
								   )