@@ -45,6 +45,12 @@ class Person(models.Model):
name = models.CharField(max_length=255, null = True, blank = True)
user = models.ForeignKey(User, null = True, blank = True)
+ def save(self):
+ # Convert to lower case to avoid identical emails with case variations
+ # from being inserted in the DB.
+ self.email = self.email.lower()
+ super(Person, self).save()
+
def __unicode__(self):
if self.name:
return u'%s <%s>' % (self.name, self.email)
@@ -1,7 +1,8 @@
-from django.test import TestCase
+from django.db.utils import IntegrityError
+from django.test import TestCase, TransactionTestCase
-from patchwork.models import State
+from patchwork.models import Person, State
from patchwork.tests.factory import factory
@@ -40,3 +41,11 @@ class UserProfileTestCase(TestCase):
# profile.
patches = profile.submitted_patches_waiting_feedback(patch1.project)
self.assertEquals([patch1, patch2], list(patches))
+
+
+class PersonTestCase(TestCase):
+
+ def test_email_uniqueness_is_case_insensitive(self):
+ Person(email='foo.bar@example.com').save()
+ p2 = Person(email='Foo.Bar@example.com')
+ self.assertRaises(IntegrityError, p2.save)