# 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.url = reverse('user')
        self.optout_url = reverse('mail_optout')
        self.optin_url = reverse('mail_optin')
        self.form_re_template = (r'<form\s+[^>]*action="%(url)s"[^>]*>'
                                 r'.*?<input\s+[^>]*value="%(email)s"[^>]*>.*?'
                                 r'</form>')
        self.secondary_email = 'test2@example.com'

        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.assertEqual(response.status_code, 200)
        self.assertTrue(form_re.search(response.content.decode()) 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.assertEqual(response.status_code, 200)
        self.assertTrue(form_re.search(response.content.decode()) is not None)

    def testSecondaryEmailOptoutForm(self):

# 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.url = reverse('user')
        self.optout_url = reverse('mail_optout')
        self.optin_url = reverse('mail_optin')
        self.form_re_template = (r'<form\s+[^>]*action="%(url)s"[^>]*>'
                                 r'.*?<input\s+[^>]*value="%(email)s"[^>]*>.*?'
                                 r'</form>')
        self.secondary_email = 'test2@example.com'

        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.assertEqual(response.status_code, 200)
        self.assertTrue(form_re.search(response.content.decode()) 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.assertEqual(response.status_code, 200)
        self.assertTrue(form_re.search(response.content.decode()) is not None)

    def testSecondaryEmailOptoutForm(self):

        p = Person(email=self.secondary_email, user=self.user)