# Path: userprofiles/contrib/emailverification/models.py
# class EmailVerification(models.Model):
#     user = models.ForeignKey(User, verbose_name=_('User'), blank=False)
#     old_email = models.EmailField(_('Old e-mail address'))
#     new_email = models.EmailField(_('New e-mail address'))
# 
#     token = models.CharField(_('Token'), max_length=40, default=generate_token)
#     code = models.CharField(_('Code'), max_length=40, default=generate_token)
# 
#     is_approved = models.BooleanField(_('Approved'), default=False)
#     is_expired = models.BooleanField(_('Expired'), default=False)
# 
#     expiration_date = models.DateTimeField(_('Expiration date'),
#         default=generate_confirm_expire_date)
# 
#     def __unicode__(self):
#         return '%s - %s/%s' % (self.user, self.old_email, self.new_email)
# 
#     def save(self, *args, **kwargs):
#         if self.is_approved:
#             EmailVerification.objects.filter(
#                 user=self.user, is_approved=False).update(is_expired=True)
# 
#             self.is_expired = True
# 
#             if self.user.email == self.old_email:
#                 self.user.email = self.new_email
#                 self.user.save()
#         return super(EmailVerification, self).save(*args, **kwargs)
# 
#     class Meta:
#         app_label = 'userprofiles'
#         verbose_name = _('E-mail verification')
#         verbose_name_plural = _('E-mail verifications')


from django import forms
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _
from userprofiles.contrib.emailverification.models import EmailVerification
# -*- coding: utf-8 -*-



class ChangeEmailForm(forms.Form):
    new_email = forms.EmailField(label=_('New e-mail address'), required=True)

    def clean_new_email(self):
        new_email = self.cleaned_data['new_email']

        user_emails = User.objects.filter(email__iexact=new_email).count()
        verification_emails = EmailVerification.objects.filter(
            new_email__iexact=new_email, is_expired=False).count()
        if user_emails + verification_emails > 0:
            raise forms.ValidationError(_(
                'This email address is already in use. Please supply a different email address.'))

        return new_email

    def save(self, user):

# Path: userprofiles/contrib/emailverification/models.py
# class EmailVerification(models.Model):
#     user = models.ForeignKey(User, verbose_name=_('User'), blank=False)
#     old_email = models.EmailField(_('Old e-mail address'))
#     new_email = models.EmailField(_('New e-mail address'))
# 
#     token = models.CharField(_('Token'), max_length=40, default=generate_token)
#     code = models.CharField(_('Code'), max_length=40, default=generate_token)
# 
#     is_approved = models.BooleanField(_('Approved'), default=False)
#     is_expired = models.BooleanField(_('Expired'), default=False)
# 
#     expiration_date = models.DateTimeField(_('Expiration date'),
#         default=generate_confirm_expire_date)
# 
#     def __unicode__(self):
#         return '%s - %s/%s' % (self.user, self.old_email, self.new_email)
# 
#     def save(self, *args, **kwargs):
#         if self.is_approved:
#             EmailVerification.objects.filter(
#                 user=self.user, is_approved=False).update(is_expired=True)
# 
#             self.is_expired = True
# 
#             if self.user.email == self.old_email:
#                 self.user.email = self.new_email
#                 self.user.save()
#         return super(EmailVerification, self).save(*args, **kwargs)
# 
#     class Meta:
#         app_label = 'userprofiles'
#         verbose_name = _('E-mail verification')
#         verbose_name_plural = _('E-mail verifications')


# Path: userprofiles/contrib/emailverification/forms.py
from django import forms
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _
from userprofiles.contrib.emailverification.models import EmailVerification

# -*- coding: utf-8 -*-



class ChangeEmailForm(forms.Form):
    new_email = forms.EmailField(label=_('New e-mail address'), required=True)

    def clean_new_email(self):
        new_email = self.cleaned_data['new_email']

        user_emails = User.objects.filter(email__iexact=new_email).count()
        verification_emails = EmailVerification.objects.filter(
            new_email__iexact=new_email, is_expired=False).count()
        if user_emails + verification_emails > 0:
            raise forms.ValidationError(_(
                'This email address is already in use. Please supply a different email address.'))

        return new_email

    def save(self, user):

        verification = EmailVerification.objects.create(user=user,