# Path: patchwork/models.py
# class EmailOptout(models.Model):
#     email = models.CharField(max_length=200, primary_key=True)
# 
#     @classmethod
#     def is_optout(cls, email):
#         email = email.lower().strip()
#         return cls.objects.filter(email=email).count() > 0
# 
#     def __str__(self):
#         return self.email
# 
# 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, on_delete=models.CASCADE)
#     key = HashField()
#     date = models.DateTimeField(default=datetime.datetime.now)
#     active = models.BooleanField(default=True)
# 
#     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(EmailConfirmation, self).save()
# 
# class Person(models.Model):
#     email = models.CharField(max_length=255, unique=True)
#     name = models.CharField(max_length=255, null=True, blank=True)
#     user = models.ForeignKey(User, null=True, blank=True,
#                              on_delete=models.SET_NULL)
# 
#     def display_name(self):
#         if self.name:
#             return self.name
#         else:
#             return self.email
# 
#     def email_name(self):
#         if (self.name):
#             return "\"%s\" <%s>" % (self.name, self.email)
#         else:
#             return self.email
# 
#     def link_to_user(self, user):
#         self.name = user.profile.name()
#         self.user = user
# 
#     def __str__(self):
#         return self.display_name()
# 
#     class Meta:
#         verbose_name_plural = 'People'


import re
from django.core import mail
from django.core.urlresolvers import reverse
from django.test import TestCase
from patchwork.models import EmailOptout, EmailConfirmation, Person
from patchwork.tests.utils import create_user, error_strings
    def testOptoutRequestInvalidPOSTNonEmail(self):
        response = self.client.post(self.url, {'email': 'foo'})
        self.assertEqual(response.status_code, 200)
        self.assertFormError(response, 'form', 'email', error_strings['email'])
        self.assertTrue(response.context['error'])
        self.assertNotIn('email_sent', response.context)
        self.assertEqual(len(mail.outbox), 0)


class OptoutTest(TestCase):

    def setUp(self):
        self.url = reverse('mail_optout')
        self.email = u'foo@example.com'
        self.conf = EmailConfirmation(type='optout', email=self.email)
        self.conf.save()

    def testOptoutValidHash(self):
        url = reverse('confirm', kwargs={'key': self.conf.key})
        response = self.client.get(url)

        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'patchwork/optout.html')
        self.assertContains(response, self.email)

        # check that we've got an optout in the list
        self.assertEqual(EmailOptout.objects.count(), 1)
        self.assertEqual(EmailOptout.objects.all()[0].email, self.email)

        # check that the confirmation is now inactive

# Path: patchwork/models.py
# class EmailOptout(models.Model):
#     email = models.CharField(max_length=200, primary_key=True)
# 
#     @classmethod
#     def is_optout(cls, email):
#         email = email.lower().strip()
#         return cls.objects.filter(email=email).count() > 0
# 
#     def __str__(self):
#         return self.email
# 
# 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, on_delete=models.CASCADE)
#     key = HashField()
#     date = models.DateTimeField(default=datetime.datetime.now)
#     active = models.BooleanField(default=True)
# 
#     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(EmailConfirmation, self).save()
# 
# class Person(models.Model):
#     email = models.CharField(max_length=255, unique=True)
#     name = models.CharField(max_length=255, null=True, blank=True)
#     user = models.ForeignKey(User, null=True, blank=True,
#                              on_delete=models.SET_NULL)
# 
#     def display_name(self):
#         if self.name:
#             return self.name
#         else:
#             return self.email
# 
#     def email_name(self):
#         if (self.name):
#             return "\"%s\" <%s>" % (self.name, self.email)
#         else:
#             return self.email
# 
#     def link_to_user(self, user):
#         self.name = user.profile.name()
#         self.user = user
# 
#     def __str__(self):
#         return self.display_name()
# 
#     class Meta:
#         verbose_name_plural = 'People'


# Path: patchwork/tests/test_mail_settings.py
import re
from django.core import mail
from django.core.urlresolvers import reverse
from django.test import TestCase
from patchwork.models import EmailOptout, EmailConfirmation, Person
from patchwork.tests.utils import create_user, error_strings

    def testOptoutRequestInvalidPOSTNonEmail(self):
        response = self.client.post(self.url, {'email': 'foo'})
        self.assertEqual(response.status_code, 200)
        self.assertFormError(response, 'form', 'email', error_strings['email'])
        self.assertTrue(response.context['error'])
        self.assertNotIn('email_sent', response.context)
        self.assertEqual(len(mail.outbox), 0)


class OptoutTest(TestCase):

    def setUp(self):
        self.url = reverse('mail_optout')
        self.email = u'foo@example.com'
        self.conf = EmailConfirmation(type='optout', email=self.email)
        self.conf.save()

    def testOptoutValidHash(self):
        url = reverse('confirm', kwargs={'key': self.conf.key})
        response = self.client.get(url)

        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'patchwork/optout.html')
        self.assertContains(response, self.email)

        # check that we've got an optout in the list
        self.assertEqual(EmailOptout.objects.count(), 1)
        self.assertEqual(EmailOptout.objects.all()[0].email, self.email)

        # check that the confirmation is now inactive

        self.assertFalse(EmailConfirmation.objects.get(