# 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
        self.assertContains(response, '<strong>may</strong>')
        optout_url = reverse('mail_optout')
        self.assertContains(response, ('action="%s"' % optout_url))

    def testMailSettingsPOSTOptedOut(self):
        email = u'foo@example.com'
        EmailOptout(email=email).save()
        response = self.client.post(self.url, {'email': email})
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'patchwork/mail-settings.html')
        self.assertEqual(response.context['is_optout'], True)
        self.assertContains(response, '<strong>may not</strong>')
        optin_url = reverse('mail_optin')
        self.assertContains(response, ('action="%s"' % optin_url))


class OptoutRequestTest(TestCase):

    def setUp(self):
        self.url = reverse('mail_optout')

    def testOptOutRequestGET(self):
        response = self.client.get(self.url)
        self.assertRedirects(response, reverse('mail_settings'))

    def testOptoutRequestValidPOST(self):
        email = u'foo@example.com'
        response = self.client.post(self.url, {'email': email})

        # check for a confirmation object

# 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

        self.assertContains(response, '<strong>may</strong>')
        optout_url = reverse('mail_optout')
        self.assertContains(response, ('action="%s"' % optout_url))

    def testMailSettingsPOSTOptedOut(self):
        email = u'foo@example.com'
        EmailOptout(email=email).save()
        response = self.client.post(self.url, {'email': email})
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'patchwork/mail-settings.html')
        self.assertEqual(response.context['is_optout'], True)
        self.assertContains(response, '<strong>may not</strong>')
        optin_url = reverse('mail_optin')
        self.assertContains(response, ('action="%s"' % optin_url))


class OptoutRequestTest(TestCase):

    def setUp(self):
        self.url = reverse('mail_optout')

    def testOptOutRequestGET(self):
        response = self.client.get(self.url)
        self.assertRedirects(response, reverse('mail_settings'))

    def testOptoutRequestValidPOST(self):
        email = u'foo@example.com'
        response = self.client.post(self.url, {'email': email})

        # check for a confirmation object

        self.assertEqual(EmailConfirmation.objects.count(), 1)