summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorJeremy Kerr <jk@ozlabs.org>2011-09-19 09:42:44 +0800
committerJeremy Kerr <jk@ozlabs.org>2011-09-19 09:42:44 +0800
commit75d8cf966034e673afe0077ba393d8b2eb3e9b93 (patch)
tree3f6cf6b9f87ad313d23de77b4c33422e7444a1df /apps
parent539b6596dc1bf1d3118631095625e354026da373 (diff)
parentf1e5f6a2c9d737f12290f5bd5a934b74c362616f (diff)
downloadpatchwork-75d8cf966034e673afe0077ba393d8b2eb3e9b93.tar.bz2
patchwork-75d8cf966034e673afe0077ba393d8b2eb3e9b93.tar.xz
Merge branch 'notifications'
Diffstat (limited to 'apps')
-rwxr-xr-xapps/patchwork/bin/patchwork-cron.py13
-rw-r--r--apps/patchwork/forms.py46
-rw-r--r--apps/patchwork/models.py85
-rw-r--r--apps/patchwork/tests/__init__.py5
-rw-r--r--apps/patchwork/tests/confirm.py67
-rw-r--r--apps/patchwork/tests/mail_settings.py302
-rw-r--r--apps/patchwork/tests/notifications.py241
-rw-r--r--apps/patchwork/tests/registration.py150
-rw-r--r--apps/patchwork/tests/user.py128
-rw-r--r--apps/patchwork/tests/utils.py2
-rw-r--r--apps/patchwork/urls.py26
-rw-r--r--apps/patchwork/utils.py68
-rw-r--r--apps/patchwork/views/base.py27
-rw-r--r--apps/patchwork/views/mail.py119
-rw-r--r--apps/patchwork/views/user.py87
-rw-r--r--apps/settings.py8
-rw-r--r--apps/urls.py10
17 files changed, 1316 insertions, 68 deletions
diff --git a/apps/patchwork/bin/patchwork-cron.py b/apps/patchwork/bin/patchwork-cron.py
new file mode 100755
index 0000000..e9bd0c1
--- /dev/null
+++ b/apps/patchwork/bin/patchwork-cron.py
@@ -0,0 +1,13 @@
+#!/usr/bin/env python
+
+import sys
+from patchwork.utils import send_notifications
+
+def main(args):
+ errors = send_notifications()
+ for (recipient, error) in errors:
+ print "Failed sending to %s: %s" % (recipient.email, ex)
+
+if __name__ == '__main__':
+ sys.exit(main(sys.argv))
+
diff --git a/apps/patchwork/forms.py b/apps/patchwork/forms.py
index 1ff2bd0..d5e51a2 100644
--- a/apps/patchwork/forms.py
+++ b/apps/patchwork/forms.py
@@ -22,34 +22,33 @@ from django.contrib.auth.models import User
from django import forms
from patchwork.models import Patch, State, Bundle, UserProfile
-from registration.forms import RegistrationFormUniqueEmail
-from registration.models import RegistrationProfile
-class RegistrationForm(RegistrationFormUniqueEmail):
+class RegistrationForm(forms.Form):
first_name = forms.CharField(max_length = 30, required = False)
last_name = forms.CharField(max_length = 30, required = False)
- username = forms.CharField(max_length=30, label=u'Username')
+ username = forms.RegexField(regex = r'^\w+$', max_length=30,
+ label=u'Username')
email = forms.EmailField(max_length=100, label=u'Email address')
password = forms.CharField(widget=forms.PasswordInput(),
label='Password')
- password1 = forms.BooleanField(required = False)
- password2 = forms.BooleanField(required = False)
- def save(self, profile_callback = None):
- user = RegistrationProfile.objects.create_inactive_user( \
- username = self.cleaned_data['username'],
- password = self.cleaned_data['password'],
- email = self.cleaned_data['email'],
- profile_callback = profile_callback)
- user.first_name = self.cleaned_data.get('first_name', '')
- user.last_name = self.cleaned_data.get('last_name', '')
- user.save()
-
- # saving the userprofile causes the firstname/lastname to propagate
- # to the person objects.
- user.get_profile().save()
-
- return user
+ def clean_username(self):
+ value = self.cleaned_data['username']
+ try:
+ user = User.objects.get(username__iexact = value)
+ except User.DoesNotExist:
+ return self.cleaned_data['username']
+ raise forms.ValidationError('This username is already taken. ' + \
+ 'Please choose another.')
+
+ def clean_email(self):
+ value = self.cleaned_data['email']
+ try:
+ user = User.objects.get(email__iexact = value)
+ except User.DoesNotExist:
+ return self.cleaned_data['email']
+ raise forms.ValidationError('This email address is already in use ' + \
+ 'for the account "%s".\n' % user.username)
def clean(self):
return self.cleaned_data
@@ -228,5 +227,8 @@ class MultiplePatchForm(forms.Form):
instance.save()
return instance
-class UserPersonLinkForm(forms.Form):
+class EmailForm(forms.Form):
email = forms.EmailField(max_length = 200)
+
+UserPersonLinkForm = EmailForm
+OptinoutRequestForm = EmailForm
diff --git a/apps/patchwork/models.py b/apps/patchwork/models.py
index 6c8fc71..22062c2 100644
--- a/apps/patchwork/models.py
+++ b/apps/patchwork/models.py
@@ -21,6 +21,7 @@ from django.db import models
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.contrib.sites.models import Site
+from django.conf import settings
from patchwork.parser import hash_patch
import re
@@ -63,6 +64,7 @@ class Project(models.Model):
name = models.CharField(max_length=255, unique=True)
listid = models.CharField(max_length=255, unique=True)
listemail = models.CharField(max_length=200)
+ send_notifications = models.BooleanField()
def __unicode__(self):
return self.name
@@ -373,34 +375,83 @@ class BundlePatch(models.Model):
unique_together = [('bundle', 'patch')]
ordering = ['order']
-class UserPersonConfirmation(models.Model):
- user = models.ForeignKey(User)
+class EmailConfirmation(models.Model):
+ validity = datetime.timedelta(days = settings.CONFIRMATION_VALIDITY_DAYS)
+ type = models.CharField(max_length = 20, choices = [
+ ('userperson', 'User-Person association'),
+ ('registration', 'Registration'),
+ ('optout', 'Email opt-out'),
+ ])
email = models.CharField(max_length = 200)
+ user = models.ForeignKey(User, null = True)
key = HashField()
- date = models.DateTimeField(default=datetime.datetime.now)
+ date = models.DateTimeField(default = datetime.datetime.now)
active = models.BooleanField(default = True)
- def confirm(self):
- if not self.active:
- return
- person = None
- try:
- person = Person.objects.get(email__iexact = self.email)
- except Exception:
- pass
- if not person:
- person = Person(email = self.email)
-
- person.link_to_user(self.user)
- person.save()
+ def deactivate(self):
self.active = False
self.save()
+ def is_valid(self):
+ return self.date + self.validity > datetime.datetime.now()
+
def save(self):
max = 1 << 32
if self.key == '':
str = '%s%s%d' % (self.user, self.email, random.randint(0, max))
self.key = self._meta.get_field('key').construct(str).hexdigest()
- super(UserPersonConfirmation, self).save()
+ super(EmailConfirmation, self).save()
+
+class EmailOptout(models.Model):
+ email = models.CharField(max_length = 200, primary_key = True)
+
+ 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)
+ orig_state = models.ForeignKey(State)
+
+def _patch_change_callback(sender, instance, **kwargs):
+ # we only want notification of modified patches
+ if instance.pk is None:
+ return
+
+ if instance.project is None or not instance.project.send_notifications:
+ return
+
+ try:
+ orig_patch = Patch.objects.get(pk = instance.pk)
+ except Patch.DoesNotExist:
+ return
+
+ # If there's no interesting changes, abort without creating the
+ # notification
+ if orig_patch.state == instance.state:
+ return
+
+ notification = None
+ try:
+ notification = PatchChangeNotification.objects.get(patch = instance)
+ except PatchChangeNotification.DoesNotExist:
+ pass
+
+ if notification is None:
+ notification = PatchChangeNotification(patch = instance,
+ orig_state = orig_patch.state)
+
+ elif notification.orig_state == instance.state:
+ # If we're back at the original state, there is no need to notify
+ notification.delete()
+ return
+ notification.last_modified = datetime.datetime.now()
+ notification.save()
+models.signals.pre_save.connect(_patch_change_callback, sender = Patch)
diff --git a/apps/patchwork/tests/__init__.py b/apps/patchwork/tests/__init__.py
index 68fe563..8ae271a 100644
--- a/apps/patchwork/tests/__init__.py
+++ b/apps/patchwork/tests/__init__.py
@@ -23,3 +23,8 @@ from patchwork.tests.bundles import *
from patchwork.tests.mboxviews import *
from patchwork.tests.updates import *
from patchwork.tests.filters import *
+from patchwork.tests.confirm import *
+from patchwork.tests.registration import *
+from patchwork.tests.user import *
+from patchwork.tests.mail_settings import *
+from patchwork.tests.notifications import *
diff --git a/apps/patchwork/tests/confirm.py b/apps/patchwork/tests/confirm.py
new file mode 100644
index 0000000..fad5125
--- /dev/null
+++ b/apps/patchwork/tests/confirm.py
@@ -0,0 +1,67 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2011 Jeremy Kerr <jk@ozlabs.org>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+import unittest
+from django.test import TestCase
+from django.contrib.auth.models import User
+from django.core.urlresolvers import reverse
+from patchwork.models import EmailConfirmation, Person
+
+def _confirmation_url(conf):
+ return reverse('patchwork.views.confirm', kwargs = {'key': conf.key})
+
+class TestUser(object):
+ username = 'testuser'
+ email = 'test@example.com'
+ secondary_email = 'test2@example.com'
+ password = None
+
+ def __init__(self):
+ self.password = User.objects.make_random_password()
+ self.user = User.objects.create_user(self.username,
+ self.email, self.password)
+
+class InvalidConfirmationTest(TestCase):
+ def setUp(self):
+ EmailConfirmation.objects.all().delete()
+ Person.objects.all().delete()
+ self.user = TestUser()
+ self.conf = EmailConfirmation(type = 'userperson',
+ email = self.user.secondary_email,
+ user = self.user.user)
+ self.conf.save()
+
+ def testInactiveConfirmation(self):
+ self.conf.active = False
+ self.conf.save()
+ response = self.client.get(_confirmation_url(self.conf))
+ self.assertEquals(response.status_code, 200)
+ self.assertTemplateUsed(response, 'patchwork/confirm-error.html')
+ self.assertEqual(response.context['error'], 'inactive')
+ self.assertEqual(response.context['conf'], self.conf)
+
+ def testExpiredConfirmation(self):
+ self.conf.date -= self.conf.validity
+ self.conf.save()
+ response = self.client.get(_confirmation_url(self.conf))
+ self.assertEquals(response.status_code, 200)
+ self.assertTemplateUsed(response, 'patchwork/confirm-error.html')
+ self.assertEqual(response.context['error'], 'expired')
+ self.assertEqual(response.context['conf'], self.conf)
+
diff --git a/apps/patchwork/tests/mail_settings.py b/apps/patchwork/tests/mail_settings.py
new file mode 100644
index 0000000..36dc5cc
--- /dev/null
+++ b/apps/patchwork/tests/mail_settings.py
@@ -0,0 +1,302 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2010 Jeremy Kerr <jk@ozlabs.org>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+import unittest
+import re
+from django.test import TestCase
+from django.test.client import Client
+from django.core import mail
+from django.core.urlresolvers import reverse
+from django.contrib.auth.models import User
+from patchwork.models import EmailOptout, EmailConfirmation, Person
+from patchwork.tests.utils import create_user
+
+class MailSettingsTest(TestCase):
+ view = 'patchwork.views.mail.settings'
+ url = reverse(view)
+
+ def testMailSettingsGET(self):
+ response = self.client.get(self.url)
+ self.assertEquals(response.status_code, 200)
+ self.assertTrue(response.context['form'])
+
+ def testMailSettingsPOST(self):
+ email = u'foo@example.com'
+ response = self.client.post(self.url, {'email': email})
+ self.assertEquals(response.status_code, 200)
+ self.assertTemplateUsed(response, 'patchwork/mail-settings.html')
+ self.assertEquals(response.context['email'], email)
+
+ def testMailSettingsPOSTEmpty(self):
+ response = self.client.post(self.url, {'email': ''})
+ self.assertEquals(response.status_code, 200)
+ self.assertTemplateUsed(response, 'patchwork/mail-form.html')
+ self.assertFormError(response, 'form', 'email',
+ 'This field is required.')
+
+ def testMailSettingsPOSTInvalid(self):
+ response = self.client.post(self.url, {'email': 'foo'})
+ self.assertEquals(response.status_code, 200)
+ self.assertTemplateUsed(response, 'patchwork/mail-form.html')
+ self.assertFormError(response, 'form', 'email',
+ 'Enter a valid e-mail address.')
+
+ def testMailSettingsPOSTOptedIn(self):
+ email = u'foo@example.com'
+ response = self.client.post(self.url, {'email': email})
+ self.assertEquals(response.status_code, 200)
+ self.assertTemplateUsed(response, 'patchwork/mail-settings.html')
+ self.assertEquals(response.context['is_optout'], False)
+ self.assertTrue('<strong>may</strong>' in response.content)
+ optout_url = reverse('patchwork.views.mail.optout')
+ self.assertTrue(('action="%s"' % optout_url) in response.content)
+
+ def testMailSettingsPOSTOptedOut(self):
+ email = u'foo@example.com'
+ EmailOptout(email = email).save()
+ response = self.client.post(self.url, {'email': email})
+ self.assertEquals(response.status_code, 200)
+ self.assertTemplateUsed(response, 'patchwork/mail-settings.html')
+ self.assertEquals(response.context['is_optout'], True)
+ self.assertTrue('<strong>may not</strong>' in response.content)
+ optin_url = reverse('patchwork.views.mail.optin')
+ self.assertTrue(('action="%s"' % optin_url) in response.content)
+
+class OptoutRequestTest(TestCase):
+ view = 'patchwork.views.mail.optout'
+ url = reverse(view)
+
+ def testOptOutRequestGET(self):
+ response = self.client.get(self.url)
+ self.assertRedirects(response, reverse('patchwork.views.mail.settings'))
+
+ def testOptoutRequestValidPOST(self):
+ email = u'foo@example.com'
+ response = self.client.post(self.url, {'email': email})
+
+ # check for a confirmation object
+ self.assertEquals(EmailConfirmation.objects.count(), 1)
+ conf = EmailConfirmation.objects.get(email = email)
+
+ # check confirmation page
+ self.assertEquals(response.status_code, 200)
+ self.assertEquals(response.context['confirmation'], conf)
+ self.assertTrue(email in response.content)
+
+ # check email
+ url = reverse('patchwork.views.confirm', kwargs = {'key': conf.key})
+ self.assertEquals(len(mail.outbox), 1)
+ msg = mail.outbox[0]
+ self.assertEquals(msg.to, [email])
+ self.assertEquals(msg.subject, 'Patchwork opt-out confirmation')
+ self.assertTrue(url in msg.body)
+
+ def testOptoutRequestInvalidPOSTEmpty(self):
+ response = self.client.post(self.url, {'email': ''})
+ self.assertEquals(response.status_code, 200)
+ self.assertFormError(response, 'form', 'email',
+ 'This field is required.')
+ self.assertTrue(response.context['error'])
+ self.assertTrue('email_sent' not in response.context)
+ self.assertEquals(len(mail.outbox), 0)
+
+ def testOptoutRequestInvalidPOSTNonEmail(self):
+ response = self.client.post(self.url, {'email': 'foo'})
+ self.assertEquals(response.status_code, 200)
+ self.assertFormError(response, 'form', 'email',
+ 'Enter a valid e-mail address.')
+ self.assertTrue(response.context['error'])
+ self.assertTrue('email_sent' not in response.context)
+ self.assertEquals(len(mail.outbox), 0)
+
+class OptoutTest(TestCase):
+ view = 'patchwork.views.mail.optout'
+ url = reverse(view)
+
+ def setUp(self):
+ self.email = u'foo@example.com'
+ self.conf = EmailConfirmation(type = 'optout', email = self.email)
+ self.conf.save()
+
+ def testOptoutValidHash(self):
+ url = reverse('patchwork.views.confirm',
+ kwargs = {'key': self.conf.key})
+ response = self.client.get(url)
+
+ self.assertEquals(response.status_code, 200)
+ self.assertTemplateUsed(response, 'patchwork/optout.html')
+ self.assertTrue(self.email in response.content)
+
+ # check that we've got an optout in the list
+ self.assertEquals(EmailOptout.objects.count(), 1)
+ self.assertEquals(EmailOptout.objects.all()[0].email, self.email)
+
+ # check that the confirmation is now inactive
+ self.assertFalse(EmailConfirmation.objects.get(
+ pk = self.conf.pk).active)
+
+
+class OptoutPreexistingTest(OptoutTest):
+ """Test that a duplicated opt-out behaves the same as the initial one"""
+ def setUp(self):
+ super(OptoutPreexistingTest, self).setUp()
+ EmailOptout(email = self.email).save()
+
+class OptinRequestTest(TestCase):
+ view = 'patchwork.views.mail.optin'
+ url = reverse(view)
+
+ def setUp(self):
+ self.email = u'foo@example.com'
+ EmailOptout(email = self.email).save()
+
+ def testOptInRequestGET(self):
+ response = self.client.get(self.url)
+ self.assertRedirects(response, reverse('patchwork.views.mail.settings'))
+
+ def testOptInRequestValidPOST(self):
+ response = self.client.post(self.url, {'email': self.email})
+
+ # check for a confirmation object
+ self.assertEquals(EmailConfirmation.objects.count(), 1)
+ conf = EmailConfirmation.objects.get(email = self.email)
+
+ # check confirmation page
+ self.assertEquals(response.status_code, 200)
+ self.assertEquals(response.context['confirmation'], conf)
+ self.assertTrue(self.email in response.content)
+
+ # check email
+ url = reverse('patchwork.views.confirm', kwargs = {'key': conf.key})
+ self.assertEquals(len(mail.outbox), 1)
+ msg = mail.outbox[0]
+ self.assertEquals(msg.to, [self.email])
+ self.assertEquals(msg.subject, 'Patchwork opt-in confirmation')
+ self.assertTrue(url in msg.body)
+
+ def testOptoutRequestInvalidPOSTEmpty(self):
+ response = self.client.post(self.url, {'email': ''})
+ self.assertEquals(response.status_code, 200)
+ self.assertFormError(response, 'form', 'email',
+ 'This field is required.')
+ self.assertTrue(response.context['error'])
+ self.assertTrue('email_sent' not in response.context)
+ self.assertEquals(len(mail.outbox), 0)
+
+ def testOptoutRequestInvalidPOSTNonEmail(self):
+ response = self.client.post(self.url, {'email': 'foo'})
+ self.assertEquals(response.status_code, 200)
+ self.assertFormError(response, 'form', 'email',
+ 'Enter a valid e-mail address.')
+ self.assertTrue(response.context['error'])
+ self.assertTrue('email_sent' not in response.context)
+ self.assertEquals(len(mail.outbox), 0)
+
+class OptinTest(TestCase):
+
+ def setUp(self):
+ self.email = u'foo@example.com'
+ self.optout = EmailOptout(email = self.email)
+ self.optout.save()
+ self.conf = EmailConfirmation(type = 'optin', email = self.email)
+ self.conf.save()
+
+ def testOptinValidHash(self):
+ url = reverse('patchwork.views.confirm',
+ kwargs = {'key': self.conf.key})
+ response = self.client.get(url)
+
+ self.assertEquals(response.status_code, 200)
+ self.assertTemplateUsed(response, 'patchwork/optin.html')
+ self.assertTrue(self.email in response.content)
+
+ # check that there's no optout remaining
+ self.assertEquals(EmailOptout.objects.count(), 0)
+
+ # check that the confirmation is now inactive
+ self.assertFalse(EmailConfirmation.objects.get(
+ pk = self.conf.pk).active)
+
+class OptinWithoutOptoutTest(TestCase):
+ """Test an opt-in with no existing opt-out"""
+ view = 'patchwork.views.mail.optin'
+ url = reverse(view)
+
+ def testOptInWithoutOptout(self):
+ email = u'foo@example.com'
+ response = self.client.post(self.url, {'email': email})
+
+ # check for an error message
+ self.assertEquals(response.status_code, 200)
+ self.assertTrue(bool(response.context['error']))
+ self.assertTrue('not on the patchwork opt-out list' in response.content)
+
+class UserProfileOptoutFormTest(TestCase):
+ """Test that the correct optin/optout forms appear on the user profile
+ page, for logged-in users"""
+
+ view = 'patchwork.views.user.profile'
+ url = reverse(view)
+ optout_url = reverse('patchwork.views.mail.optout')
+ optin_url = reverse('patchwork.views.mail.optin')
+ form_re_template = ('<form\s+[^>]*action="%(url)s"[^>]*>'
+ '.*?<input\s+[^>]*value="%(email)s"[^>]*>.*?'
+ '</form>')
+ secondary_email = 'test2@example.com'
+
+ def setUp(self):
+ self.user = create_user()
+ self.client.login(username = self.user.username,
+ password = self.user.username)
+
+ def _form_re(self, url, email):
+ return re.compile(self.form_re_template % {'url': url, 'email': email},
+ re.DOTALL)
+
+ def testMainEmailOptoutForm(self):
+ form_re = self._form_re(self.optout_url, self.user.email)
+ response = self.client.get(self.url)
+ self.assertEquals(response.status_code, 200)
+ self.assertTrue(form_re.search(response.content) is not None)
+
+ def testMainEmailOptinForm(self):
+ EmailOptout(email = self.user.email).save()
+ form_re = self._form_re(self.optin_url, self.user.email)
+ response = self.client.get(self.url)
+ self.assertEquals(response.status_code, 200)
+ self.assertTrue(form_re.search(response.content) is not None)
+
+ def testSecondaryEmailOptoutForm(self):
+ p = Person(email = self.secondary_email, user = self.user)
+ p.save()
+
+ form_re = self._form_re(self.optout_url, p.email)
+ response = self.client.get(self.url)
+ self.assertEquals(response.status_code, 200)
+ self.assertTrue(form_re.search(response.content) is not None)
+
+ def testSecondaryEmailOptinForm(self):
+ p = Person(email = self.secondary_email, user = self.user)
+ p.save()
+ EmailOptout(email = p.email).save()
+
+ form_re = self._form_re(self.optin_url, self.user.email)
+ response = self.client.get(self.url)
+ self.assertEquals(response.status_code, 200)
+ self.assertTrue(form_re.search(response.content) is not None)
diff --git a/apps/patchwork/tests/notifications.py b/apps/patchwork/tests/notifications.py
new file mode 100644
index 0000000..f14b30b
--- /dev/null
+++ b/apps/patchwork/tests/notifications.py
@@ -0,0 +1,241 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2011 Jeremy Kerr <jk@ozlabs.org>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+import datetime
+from django.test import TestCase
+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, EmailOptout
+from patchwork.tests.utils import defaults, create_maintainer
+from patchwork.utils import send_notifications
+
+class PatchNotificationModelTest(TestCase):
+ """Tests for the creation & update of the PatchChangeNotification model"""
+
+ def setUp(self):
+ self.project = defaults.project
+ self.project.send_notifications = True
+ self.project.save()
+ self.submitter = defaults.patch_author_person
+ self.submitter.save()
+ self.patch = Patch(project = self.project, msgid = 'testpatch',
+ name = 'testpatch', content = '',
+ submitter = self.submitter)
+
+ def tearDown(self):
+ self.patch.delete()
+ self.submitter.delete()
+ self.project.delete()
+
+ def testPatchCreation(self):
+ """Ensure we don't get a notification on create"""
+ self.patch.save()
+ self.assertEqual(PatchChangeNotification.objects.count(), 0)
+
+ def testPatchUninterestingChange(self):
+ """Ensure we don't get a notification for "uninteresting" changes"""
+ self.patch.save()
+ self.patch.archived = True
+ self.patch.save()
+ self.assertEqual(PatchChangeNotification.objects.count(), 0)
+
+ def testPatchChange(self):
+ """Ensure we get a notification for interesting patch changes"""
+ self.patch.save()
+ oldstate = self.patch.state
+ state = State.objects.exclude(pk = oldstate.pk)[0]
+
+ self.patch.state = state
+ self.patch.save()
+ self.assertEqual(PatchChangeNotification.objects.count(), 1)
+ notification = PatchChangeNotification.objects.all()[0]
+ self.assertEqual(notification.patch, self.patch)
+ self.assertEqual(notification.orig_state, oldstate)
+
+ def testNotificationCancelled(self):
+ """Ensure we cancel notifications that are no longer valid"""
+ self.patch.save()
+ oldstate = self.patch.state
+ state = State.objects.exclude(pk = oldstate.pk)[0]
+
+ self.patch.state = state
+ self.patch.save()
+ self.assertEqual(PatchChangeNotification.objects.count(), 1)
+
+ self.patch.state = oldstate
+ self.patch.save()
+ self.assertEqual(PatchChangeNotification.objects.count(), 0)
+
+ def testNotificationUpdated(self):
+ """Ensure we update notifications when the patch has a second change,
+ but keep the original patch details"""
+ self.patch.save()
+ oldstate = self.patch.state
+ newstates = State.objects.exclude(pk = oldstate.pk)[:2]
+
+ self.patch.state = newstates[0]
+ self.patch.save()
+ self.assertEqual(PatchChangeNotification.objects.count(), 1)
+ notification = PatchChangeNotification.objects.all()[0]
+ self.assertEqual(notification.orig_state, oldstate)
+ orig_timestamp = notification.last_modified
+
+ self.patch.state = newstates[1]
+ self.patch.save()
+ self.assertEqual(PatchChangeNotification.objects.count(), 1)
+ notification = PatchChangeNotification.objects.all()[0]
+ self.assertEqual(notification.orig_state, oldstate)
+ self.assertTrue(notification.last_modified > orig_timestamp)
+
+ def testProjectNotificationsDisabled(self):
+ """Ensure we don't see notifications created when a project is
+ configured not to send them"""
+ self.project.send_notifications = False
+ self.project.save()
+
+ self.patch.save()
+ oldstate = self.patch.state
+ state = State.objects.exclude(pk = oldstate.pk)[0]
+
+ self.patch.state = state
+ self.patch.save()
+ self.assertEqual(PatchChangeNotification.objects.count(), 0)
+
+class PatchNotificationEmailTest(TestCase):
+
+ def setUp(self):
+ self.project = defaults.project
+ self.project.send_notifications = True
+ self.project.save()
+ self.submitter = defaults.patch_author_person
+ self.submitter.save()
+ self.patch = Patch(project = self.project, msgid = 'testpatch',
+ name = 'testpatch', content = '',
+ submitter = self.submitter)
+ self.patch.save()
+
+ def tearDown(self):
+ self.patch.delete()
+ self.submitter.delete()
+ self.project.delete()
+
+ def _expireNotifications(self, **kwargs):
+ timestamp = datetime.datetime.now() - \
+ datetime.timedelta(minutes =
+ settings.NOTIFICATION_DELAY_MINUTES + 1)
+
+ qs = PatchChangeNotification.objects.all()
+ if kwargs:
+ qs = qs.filter(**kwargs)
+
+ qs.update(last_modified = timestamp)
+
+ def testNoNotifications(self):
+ self.assertEquals(send_notifications(), [])
+
+ def testNoReadyNotifications(self):
+ """ We shouldn't see immediate notifications"""
+ PatchChangeNotification(patch = self.patch,
+ orig_state = self.patch.state).save()
+
+ errors = send_notifications()
+ self.assertEquals(errors, [])
+ self.assertEquals(len(mail.outbox), 0)
+
+ def testNotifications(self):
+ PatchChangeNotification(patch = self.patch,
+ orig_state = self.patch.state).save()
+ self._expireNotifications()
+
+ errors = send_notifications()
+ self.assertEquals(errors, [])
+ self.assertEquals(len(mail.outbox), 1)
+ msg = mail.outbox[0]
+ 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',
+ name = 'testpatch 2', content = '',
+ submitter = self.submitter)]
+
+ for patch in patches:
+ patch.save()
+ PatchChangeNotification(patch = patch,
+ orig_state = patch.state).save()
+
+ self.assertEquals(PatchChangeNotification.objects.count(), len(patches))
+ self._expireNotifications()
+ errors = send_notifications()
+ self.assertEquals(errors, [])
+ self.assertEquals(len(mail.outbox), 1)
+ msg = mail.outbox[0]
+ self.assertTrue(patches[0].get_absolute_url() in msg.body)
+ self.assertTrue(patches[1].get_absolute_url() in msg.body)
+
+ def testUnexpiredNotificationMerge(self):
+ """Test that when there are multiple pending notifications, with
+ at least one within the notification delay, that other notifications
+ are held"""
+ patches = [self.patch,
+ Patch(project = self.project, msgid = 'testpatch-2',
+ name = 'testpatch 2', content = '',
+ submitter = self.submitter)]
+
+ for patch in patches:
+ patch.save()
+ PatchChangeNotification(patch = patch,
+ orig_state = patch.state).save()
+
+ self.assertEquals(PatchChangeNotification.objects.count(), len(patches))
+ self._expireNotifications()
+
+ # update one notification, to bring it out of the notification delay
+ patches[0].state = State.objects.exclude(pk = patches[0].state.pk)[0]
+ patches[0].save()
+
+ # the updated notification should prevent the other from being sent
+ errors = send_notifications()
+ self.assertEquals(errors, [])
+ self.assertEquals(len(mail.outbox), 0)
+
+ # expire the updated notification
+ self._expireNotifications()
+
+ errors = send_notifications()
+ self.assertEquals(errors, [])
+ self.assertEquals(len(mail.outbox), 1)
+ msg = mail.outbox[0]
+ self.assertTrue(patches[0].get_absolute_url() in msg.body)
+ self.assertTrue(patches[1].get_absolute_url() in msg.body)
diff --git a/apps/patchwork/tests/registration.py b/apps/patchwork/tests/registration.py
new file mode 100644
index 0000000..18b781f
--- /dev/null
+++ b/apps/patchwork/tests/registration.py
@@ -0,0 +1,150 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2010 Jeremy Kerr <jk@ozlabs.org>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+import unittest
+from django.test import TestCase
+from django.test.client import Client
+from django.core import mail
+from django.core.urlresolvers import reverse
+from django.contrib.auth.models import User
+from patchwork.models import EmailConfirmation, Person
+from patchwork.tests.utils import create_user
+
+def _confirmation_url(conf):
+ return reverse('patchwork.views.confirm', kwargs = {'key': conf.key})
+
+class TestUser(object):
+ firstname = 'Test'
+ lastname = 'User'
+ username = 'testuser'
+ email = 'test@example.com'
+ password = 'foobar'
+
+class RegistrationTest(TestCase):
+ def setUp(self):
+ self.user = TestUser()
+ self.client = Client()
+ self.default_data = {'username': self.user.username,
+ 'first_name': self.user.firstname,
+ 'last_name': self.user.lastname,
+ 'email': self.user.email,
+ 'password': self.user.password}
+ self.required_error = 'This field is required.'
+ self.invalid_error = 'Enter a valid value.'
+
+ def testRegistrationForm(self):
+ response = self.client.get('/register/')
+ self.assertEquals(response.status_code, 200)
+ self.assertTemplateUsed(response, 'patchwork/registration_form.html')
+
+ def testBlankFields(self):
+ for field in ['username', 'email', 'password']:
+ data = self.default_data.copy()
+ del data[field]
+ response = self.client.post('/register/', data)
+ self.assertEquals(response.status_code, 200)
+ self.assertFormError(response, 'form', field, self.required_error)
+
+ def testInvalidUsername(self):
+ data = self.default_data.copy()
+ data['username'] = 'invalid user'
+ response = self.client.post('/register/', data)
+ self.assertEquals(response.status_code, 200)
+ self.assertFormError(response, 'form', 'username', self.invalid_error)
+
+ def testExistingUsername(self):
+ user = create_user()
+ data = self.default_data.copy()
+ data['username'] = user.username
+ response = self.client.post('/register/', data)
+ self.assertEquals(response.status_code, 200)
+ self.assertFormError(response, 'form', 'username',
+ 'This username is already taken. Please choose another.')
+
+ def testExistingEmail(self):
+ user = create_user()
+ data = self.default_data.copy()
+ data['email'] = user.email
+ response = self.client.post('/register/', data)
+ self.assertEquals(response.status_code, 200)
+ self.assertFormError(response, 'form', 'email',
+ 'This email address is already in use ' + \
+ 'for the account "%s".\n' % user.username)
+
+ def testValidRegistration(self):
+ response = self.client.post('/register/', self.default_data)
+ self.assertEquals(response.status_code, 200)
+ self.assertContains(response, 'confirmation email has been sent')
+
+ # check for presence of an inactive user object
+ users = User.objects.filter(username = self.user.username)
+ self.assertEquals(users.count(), 1)
+ user = users[0]
+ self.assertEquals(user.username, self.user.username)
+ self.assertEquals(user.email, self.user.email)
+ self.assertEquals(user.is_active, False)
+
+ # check for confirmation object
+ confs = EmailConfirmation.objects.filter(user = user,
+ type = 'registration')
+ self.assertEquals(len(confs), 1)
+ conf = confs[0]
+ self.assertEquals(conf.email, self.user.email)
+
+ # check for a sent mail
+ self.assertEquals(len(mail.outbox), 1)
+ msg = mail.outbox[0]
+ self.assertEquals(msg.subject, 'Patchwork account confirmation')
+ self.assertTrue(self.user.email in msg.to)
+ self.assertTrue(_confirmation_url(conf) in msg.body)
+
+ # ...and that the URL is valid
+ response = self.client.get(_confirmation_url(conf))
+ self.assertEquals(response.status_code, 200)
+
+class RegistrationConfirmationTest(TestCase):
+
+ def setUp(self):
+ self.user = TestUser()
+ self.default_data = {'username': self.user.username,
+ 'first_name': self.user.firstname,
+ 'last_name': self.user.lastname,
+ 'email': self.user.email,
+ 'password': self.user.password}
+
+ def testRegistrationConfirmation(self):
+ self.assertEqual(EmailConfirmation.objects.count(), 0)
+ response = self.client.post('/register/', self.default_data)
+ self.assertEquals(response.status_code, 200)
+ self.assertContains(response, 'confirmation email has been sent')
+
+ self.assertEqual(EmailConfirmation.objects.count(), 1)
+ conf = EmailConfirmation.objects.filter()[0]
+ self.assertFalse(conf.user.is_active)
+ self.assertTrue(conf.active)
+
+ response = self.client.get(_confirmation_url(conf))
+ self.assertEquals(response.status_code, 200)
+ self.assertTemplateUsed(response, 'patchwork/registration-confirm.html')
+
+ conf = EmailConfirmation.objects.get(pk = conf.pk)
+ self.assertTrue(conf.user.is_active)
+ self.assertFalse(conf.active)
+
+
diff --git a/apps/patchwork/tests/user.py b/apps/patchwork/tests/user.py
new file mode 100644
index 0000000..e96e6c5
--- /dev/null
+++ b/apps/patchwork/tests/user.py
@@ -0,0 +1,128 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2010 Jeremy Kerr <jk@ozlabs.org>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+import unittest
+from django.test import TestCase
+from django.test.client import Client
+from django.core import mail
+from django.core.urlresolvers import reverse
+from django.conf import settings
+from django.contrib.auth.models import User
+from patchwork.models import EmailConfirmation, Person
+
+def _confirmation_url(conf):
+ return reverse('patchwork.views.confirm', kwargs = {'key': conf.key})
+
+class TestUser(object):
+ username = 'testuser'
+ email = 'test@example.com'
+ secondary_email = 'test2@example.com'
+ password = None
+
+ def __init__(self):
+ self.password = User.objects.make_random_password()
+ self.user = User.objects.create_user(self.username,
+ self.email, self.password)
+
+class UserPersonRequestTest(TestCase):
+ def setUp(self):
+ self.user = TestUser()
+ self.client.login(username = self.user.username,
+ password = self.user.password)
+ EmailConfirmation.objects.all().delete()
+
+ def testUserPersonRequestForm(self):
+ response = self.client.get('/user/link/')
+ self.assertEquals(response.status_code, 200)
+ self.assertTrue(response.context['linkform'])
+
+ def testUserPersonRequestEmpty(self):
+ response = self.client.post('/user/link/', {'email': ''})
+ self.assertEquals(response.status_code, 200)
+ self.assertTrue(response.context['linkform'])
+ self.assertFormError(response, 'linkform', 'email',
+ 'This field is required.')
+
+ def testUserPersonRequestInvalid(self):
+ response = self.client.post('/user/link/', {'email': 'foo'})
+ self.assertEquals(response.status_code, 200)
+ self.assertTrue(response.context['linkform'])
+ self.assertFormError(response, 'linkform', 'email',
+ 'Enter a valid e-mail address.')
+
+ def testUserPersonRequestValid(self):
+ response = self.client.post('/user/link/',
+ {'email': self.user.secondary_email})
+ self.assertEquals(response.status_code, 200)
+ self.assertTrue(response.context['confirmation'])
+
+ # check that we have a confirmation saved
+ self.assertEquals(EmailConfirmation.objects.count(), 1)
+ conf = EmailConfirmation.objects.all()[0]
+ self.assertEquals(conf.user, self.user.user)
+ self.assertEquals(conf.email, self.user.secondary_email)
+ self.assertEquals(conf.type, 'userperson')
+
+ # check that an email has gone out...
+ self.assertEquals(len(mail.outbox), 1)
+ msg = mail.outbox[0]
+ self.assertEquals(msg.subject, 'Patchwork email address confirmation')
+ self.assertTrue(self.user.secondary_email in msg.to)
+ self.assertTrue(_confirmation_url(conf) in msg.body)
+
+ # ...and that the URL is valid
+ response = self.client.get(_confirmation_url(conf))
+ self.assertEquals(response.status_code, 200)
+ self.assertTemplateUsed(response, 'patchwork/user-link-confirm.html')
+
+class UserPersonConfirmTest(TestCase):
+ def setUp(self):
+ EmailConfirmation.objects.all().delete()
+ Person.objects.all().delete()
+ self.user = TestUser()
+ self.client.login(username = self.user.username,
+ password = self.user.password)
+ self.conf = EmailConfirmation(type = 'userperson',
+ email = self.user.secondary_email,
+ user = self.user.user)
+ self.conf.save()
+
+ def testUserPersonConfirm(self):
+ self.assertEquals(Person.objects.count(), 1)
+ response = self.client.get(_confirmation_url(self.conf))
+ self.assertEquals(response.status_code, 200)
+
+ # check that the Person object has been created and linked
+ self.assertEquals(Person.objects.count(), 2)
+ person = Person.objects.get(email = self.user.secondary_email)
+ self.assertEquals(person.email, self.user.secondary_email)
+ self.assertEquals(person.user, self.user.user)
+
+ # check that the confirmation has been marked as inactive. We
+ # need to reload the confirmation to check this.
+ conf = EmailConfirmation.objects.get(pk = self.conf.pk)
+ self.assertEquals(conf.active, False)
+
+class UserLoginRedirectTest(TestCase):
+
+ def testUserLoginRedirect(self):
+ url = '/user/'
+ response = self.client.get(url)
+ self.assertRedirects(response, settings.LOGIN_URL + '?next=' + url)
+
diff --git a/apps/patchwork/tests/utils.py b/apps/patchwork/tests/utils.py
index f1c95e8..1cb5dfb 100644
--- a/apps/patchwork/tests/utils.py
+++ b/apps/patchwork/tests/utils.py
@@ -59,7 +59,7 @@ class defaults(object):
_user_idx = 1
def create_user():
global _user_idx
- userid = 'test-%d' % _user_idx
+ userid = 'test%d' % _user_idx
email = '%s@example.com' % userid
_user_idx += 1
diff --git a/apps/patchwork/urls.py b/apps/patchwork/urls.py
index b49b4e1..10fc3b9 100644
--- a/apps/patchwork/urls.py
+++ b/apps/patchwork/urls.py
@@ -19,6 +19,7 @@
from django.conf.urls.defaults import *
from django.conf import settings
+from django.contrib.auth import views as auth_views
urlpatterns = patterns('',
# Example:
@@ -44,16 +45,39 @@ urlpatterns = patterns('',
'patchwork.views.bundle.mbox'),
(r'^user/link/$', 'patchwork.views.user.link'),
- (r'^user/link/(?P<key>[^/]+)/$', 'patchwork.views.user.link_confirm'),
(r'^user/unlink/(?P<person_id>[^/]+)/$', 'patchwork.views.user.unlink'),
+ # password change
+ url(r'^user/password-change/$', auth_views.password_change,
+ name='auth_password_change'),
+ url(r'^user/password-change/done/$', auth_views.password_change_done,
+ name='auth_password_change_done'),
+
+ # login/logout
+ url(r'^user/login/$', auth_views.login,
+ {'template_name': 'patchwork/login.html'},
+ name = 'auth_login'),
+ url(r'^user/logout/$', auth_views.logout,
+ {'template_name': 'patchwork/logout.html'},
+ name = 'auth_logout'),
+
+ # registration
+ (r'^register/', 'patchwork.views.user.register'),
+
# public view for bundles
(r'^bundle/(?P<username>[^/]*)/(?P<bundlename>[^/]*)/$',
'patchwork.views.bundle.public'),
+ (r'^confirm/(?P<key>[0-9a-f]+)/$', 'patchwork.views.confirm'),
+
# submitter autocomplete
(r'^submitter/$', 'patchwork.views.submitter_complete'),
+ # email setup
+ (r'^mail/$', 'patchwork.views.mail.settings'),
+ (r'^mail/optout/$', 'patchwork.views.mail.optout'),
+ (r'^mail/optin/$', 'patchwork.views.mail.optin'),
+
# help!
(r'^help/(?P<path>.*)$', 'patchwork.views.help'),
)
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
diff --git a/apps/patchwork/views/base.py b/apps/patchwork/views/base.py
index c0e68ed..82c0368 100644
--- a/apps/patchwork/views/base.py
+++ b/apps/patchwork/views/base.py
@@ -18,7 +18,7 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-from patchwork.models import Patch, Project, Person
+from patchwork.models import Patch, Project, Person, EmailConfirmation
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect, Http404
from patchwork.requestcontext import PatchworkRequestContext
@@ -58,6 +58,31 @@ def pwclient(request):
response.write(render_to_string('patchwork/pwclient', context))
return response
+def confirm(request, key):
+ import patchwork.views.user, patchwork.views.mail
+ views = {
+ 'userperson': patchwork.views.user.link_confirm,
+ 'registration': patchwork.views.user.register_confirm,
+ 'optout': patchwork.views.mail.optout_confirm,
+ 'optin': patchwork.views.mail.optin_confirm,
+ }
+
+ conf = get_object_or_404(EmailConfirmation, key = key)
+ if conf.type not in views:
+ raise Http404
+
+ if conf.active and conf.is_valid():
+ return views[conf.type](request, conf)
+
+ context = PatchworkRequestContext(request)
+ context['conf'] = conf
+ if not conf.active:
+ context['error'] = 'inactive'
+ elif not conf.is_valid():
+ context['error'] = 'expired'
+
+ return render_to_response('patchwork/confirm-error.html', context)
+
def submitter_complete(request):
search = request.GET.get('q', '')
response = HttpResponse(mimetype = "text/plain")
diff --git a/apps/patchwork/views/mail.py b/apps/patchwork/views/mail.py
new file mode 100644
index 0000000..aebba34
--- /dev/null
+++ b/apps/patchwork/views/mail.py
@@ -0,0 +1,119 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2010 Jeremy Kerr <jk@ozlabs.org>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+from patchwork.requestcontext import PatchworkRequestContext
+from patchwork.models import EmailOptout, EmailConfirmation
+from patchwork.forms import OptinoutRequestForm, EmailForm
+from django.shortcuts import render_to_response
+from django.template.loader import render_to_string
+from django.conf import settings as conf_settings
+from django.core.mail import send_mail
+from django.core.urlresolvers import reverse
+from django.http import HttpResponseRedirect
+
+def settings(request):
+ context = PatchworkRequestContext(request)
+ if request.method == 'POST':
+ form = EmailForm(data = request.POST)
+ if form.is_valid():
+ email = form.cleaned_data['email']
+ is_optout = EmailOptout.objects.filter(email = email).count() > 0
+ context.update({
+ 'email': email,
+ 'is_optout': is_optout,
+ })
+ return render_to_response('patchwork/mail-settings.html', context)
+
+ else:
+ form = EmailForm()
+ context['form'] = form
+ return render_to_response('patchwork/mail-form.html', context)
+
+def optout_confirm(request, conf):
+ context = PatchworkRequestContext(request)
+
+ email = conf.email.strip().lower()
+ # silently ignore duplicated optouts
+ if EmailOptout.objects.filter(email = email).count() == 0:
+ optout = EmailOptout(email = email)
+ optout.save()
+
+ conf.deactivate()
+ context['email'] = conf.email
+
+ return render_to_response('patchwork/optout.html', context)
+
+def optin_confirm(request, conf):
+ context = PatchworkRequestContext(request)
+
+ email = conf.email.strip().lower()
+ EmailOptout.objects.filter(email = email).delete()
+
+ conf.deactivate()
+ context['email'] = conf.email
+
+ return render_to_response('patchwork/optin.html', context)
+
+def optinout(request, action, description):
+ context = PatchworkRequestContext(request)
+
+ mail_template = 'patchwork/%s-request.mail' % action
+ html_template = 'patchwork/%s-request.html' % action
+
+ if request.method != 'POST':
+ return HttpResponseRedirect(reverse(settings))
+
+ form = OptinoutRequestForm(data = request.POST)
+ if not form.is_valid():
+ context['error'] = ('There was an error in the %s form. ' +
+ 'Please review the form and re-submit.') % \
+ description
+ context['form'] = form
+ return render_to_response(html_template, context)
+
+ email = form.cleaned_data['email']
+ if action == 'optin' and \
+ EmailOptout.objects.filter(email = email).count() == 0:
+ context['error'] = ('The email address %s is not on the ' +
+ 'patchwork opt-out list, so you don\'t ' +
+ 'need to opt back in') % email
+ context['form'] = form
+ return render_to_response(html_template, context)
+
+ conf = EmailConfirmation(type = action, email = email)
+ conf.save()
+ context['confirmation'] = conf
+ mail = render_to_string(mail_template, context)
+ try:
+ send_mail('Patchwork %s confirmation' % description, mail,
+ conf_settings.DEFAULT_FROM_EMAIL, [email])
+ context['email'] = mail
+ context['email_sent'] = True
+ except Exception, ex:
+ context['error'] = 'An error occurred during confirmation . ' + \
+ 'Please try again later.'
+ context['admins'] = conf_settings.ADMINS
+
+ return render_to_response(html_template, context)
+
+def optout(request):
+ return optinout(request, 'optout', 'opt-out')
+
+def optin(request):
+ return optinout(request, 'optin', 'opt-in')
diff --git a/apps/patchwork/views/user.py b/apps/patchwork/views/user.py
index 1ae3c2d..4a0e845 100644
--- a/apps/patchwork/views/user.py
+++ b/apps/patchwork/views/user.py
@@ -21,10 +21,13 @@
from django.contrib.auth.decorators import login_required
from patchwork.requestcontext import PatchworkRequestContext
from django.shortcuts import render_to_response, get_object_or_404
+from django.contrib import auth
+from django.contrib.sites.models import Site
from django.http import HttpResponseRedirect
-from patchwork.models import Project, Bundle, Person, UserPersonConfirmation, \
- State
-from patchwork.forms import UserProfileForm, UserPersonLinkForm
+from patchwork.models import Project, Bundle, Person, EmailConfirmation, \
+ State, EmailOptout
+from patchwork.forms import UserProfileForm, UserPersonLinkForm, \
+ RegistrationForm
from patchwork.filters import DelegateFilter
from patchwork.views import generic_list
from django.template.loader import render_to_string
@@ -32,6 +35,55 @@ from django.conf import settings
from django.core.mail import send_mail
import django.core.urlresolvers
+def register(request):
+ context = PatchworkRequestContext(request)
+ if request.method == 'POST':
+ form = RegistrationForm(request.POST)
+ if form.is_valid():
+ data = form.cleaned_data
+ # create inactive user
+ user = auth.models.User.objects.create_user(data['username'],
+ data['email'],
+ data['password'])
+ user.is_active = False;
+ user.first_name = data.get('first_name', '')
+ user.last_name = data.get('last_name', '')
+ user.save()
+
+ # create confirmation
+ conf = EmailConfirmation(type = 'registration', user = user,
+ email = user.email)
+ conf.save()
+
+ # send email
+ mail_ctx = {'site': Site.objects.get_current(),
+ 'confirmation': conf}
+
+ subject = render_to_string('patchwork/activation_email_subject.txt',
+ mail_ctx).replace('\n', ' ').strip()
+
+ message = render_to_string('patchwork/activation_email.txt',
+ mail_ctx)
+
+ send_mail(subject, message, settings.DEFAULT_FROM_EMAIL,
+ [conf.email])
+
+ # setting 'confirmation' in the template indicates success
+ context['confirmation'] = conf
+
+ else:
+ form = RegistrationForm()
+
+ return render_to_response('patchwork/registration_form.html',
+ { 'form': form },
+ context_instance=context)
+
+def register_confirm(request, conf):
+ conf.user.is_active = True
+ conf.user.save()
+ conf.deactivate()
+ return render_to_response('patchwork/registration-confirm.html')
+
@login_required
def profile(request):
context = PatchworkRequestContext(request)
@@ -48,7 +100,13 @@ def profile(request):
context['bundles'] = Bundle.objects.filter(owner = request.user)
context['profileform'] = form
- people = Person.objects.filter(user = request.user)
+ optout_query = '%s.%s IN (SELECT %s FROM %s)' % (
+ Person._meta.db_table,
+ Person._meta.get_field('email').column,
+ EmailOptout._meta.get_field('email').column,
+ EmailOptout._meta.db_table)
+ people = Person.objects.filter(user = request.user) \
+ .extra(select = {'is_optout': optout_query})
context['linked_emails'] = people
context['linkform'] = UserPersonLinkForm()
@@ -61,7 +119,8 @@ def link(request):
if request.method == 'POST':
form = UserPersonLinkForm(request.POST)
if form.is_valid():
- conf = UserPersonConfirmation(user = request.user,
+ conf = EmailConfirmation(type = 'userperson',
+ user = request.user,
email = form.cleaned_data['email'])
conf.save()
context['confirmation'] = conf
@@ -83,15 +142,19 @@ def link(request):
return render_to_response('patchwork/user-link.html', context)
@login_required
-def link_confirm(request, key):
+def link_confirm(request, conf):
context = PatchworkRequestContext(request)
- confirmation = get_object_or_404(UserPersonConfirmation, key = key)
- errors = confirmation.confirm()
- if errors:
- context['errors'] = errors
- else:
- context['person'] = Person.objects.get(email = confirmation.email)
+ try:
+ person = Person.objects.get(email__iexact = conf.email)
+ except Person.DoesNotExist:
+ person = Person(email = conf.email)
+
+ person.link_to_user(conf.user)
+ person.save()
+ conf.deactivate()
+
+ context['person'] = person
return render_to_response('patchwork/user-link-confirm.html', context)
diff --git a/apps/settings.py b/apps/settings.py
index 24d3762..7523099 100644
--- a/apps/settings.py
+++ b/apps/settings.py
@@ -64,7 +64,7 @@ MIDDLEWARE_CLASSES = (
ROOT_URLCONF = 'apps.urls'
-LOGIN_URL = '/accounts/login'
+LOGIN_URL = '/user/login/'
LOGIN_REDIRECT_URL = '/user/'
# If you change the ROOT_DIR setting in your local_settings.py, you'll need to
@@ -96,13 +96,15 @@ INSTALLED_APPS = (
'django.contrib.sites',
'django.contrib.admin',
'patchwork',
- 'registration',
)
DEFAULT_PATCHES_PER_PAGE = 100
DEFAULT_FROM_EMAIL = 'Patchwork <patchwork@patchwork.example.com>'
-ACCOUNT_ACTIVATION_DAYS = 7
+CONFIRMATION_VALIDITY_DAYS = 7
+
+NOTIFICATION_DELAY_MINUTES = 10
+NOTIFICATION_FROM_EMAIL = DEFAULT_FROM_EMAIL
# Set to True to enable the Patchwork XML-RPC interface
ENABLE_XMLRPC = False
diff --git a/apps/urls.py b/apps/urls.py
index 3894708..4ddef9e 100644
--- a/apps/urls.py
+++ b/apps/urls.py
@@ -23,9 +23,6 @@ from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib import admin
-from registration.views import register
-from patchwork.forms import RegistrationForm
-
admin.autodiscover()
htdocs = os.path.join(settings.ROOT_DIR, 'htdocs')
@@ -34,13 +31,6 @@ urlpatterns = patterns('',
# Example:
(r'^', include('patchwork.urls')),
- # override the default registration form
- url(r'^accounts/register/$',
- register, {'form_class': RegistrationForm},
- name='registration_register'),
-
- (r'^accounts/', include('registration.urls')),
-
# Uncomment this for admin:
(r'^admin/', include(admin.site.urls)),