# Path: userprofiles/contrib/emailverification/forms.py
# 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,
#             old_email=user.email, new_email=self.cleaned_data['new_email'])
# 
#         context = {
#             'user': user,
#             'verification': verification,
#             'site': Site.objects.get_current(),
#         }
# 
#         subject = ''.join(render_to_string(
#             'userprofiles/mails/emailverification_subject.html', context).splitlines())
#         body = render_to_string('userprofiles/mails/emailverification.html', context)
# 
#         send_mail(subject, body, settings.DEFAULT_FROM_EMAIL,
#             [self.cleaned_data['new_email']])
# 
#         return verification
# 
# 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/mixins.py
# class LoginRequiredMixin(object):
#     """Ensures that the user is authenticated in order to access the view.
#     http://djangosnippets.org/snippets/2442/"""
# 
#     @method_decorator(login_required)
#     def dispatch(self, *args, **kwargs):
#         return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)
# 
# Path: userprofiles/settings.py
# class Settings(object):
#     def __init__(self, **kwargs):
#     def __getattr__(self, key):
# def validate_settings():


from django.contrib import messages
from django.core.urlresolvers import reverse
from django.shortcuts import redirect
from django.utils.translation import ugettext_lazy as _
from django.views.generic import FormView, TemplateView, RedirectView
from userprofiles.contrib.emailverification.forms import ChangeEmailForm
from userprofiles.contrib.emailverification.models import EmailVerification
from userprofiles.mixins import LoginRequiredMixin
from userprofiles.settings import up_settings
        return redirect('userprofiles_email_change_requested')

email_change = EmailChangeView.as_view()


class EmailChangeRequestedView(LoginRequiredMixin, TemplateView):
    template_name = 'userprofiles/email_change_requested.html'

    def get_context_data(self, **kwargs):
        return {
            'expiration_days': up_settings.EMAIL_VERIFICATION_DAYS
        }

email_change_requested = EmailChangeRequestedView.as_view()


class EmailChangeApproveView(LoginRequiredMixin, RedirectView):
    permanent = False

    def get_redirect_url(self, token, code):
        try:
            verification = EmailVerification.objects.get(token=token, code=code,
                user=self.request.user, is_expired=False, is_approved=False)

            verification.is_approved = True
            verification.save()

            messages.success(self.request, _(u'E-mail address changed to %(email)s' % {
                'email': verification.new_email}))


# Path: userprofiles/contrib/emailverification/forms.py
# 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,
#             old_email=user.email, new_email=self.cleaned_data['new_email'])
# 
#         context = {
#             'user': user,
#             'verification': verification,
#             'site': Site.objects.get_current(),
#         }
# 
#         subject = ''.join(render_to_string(
#             'userprofiles/mails/emailverification_subject.html', context).splitlines())
#         body = render_to_string('userprofiles/mails/emailverification.html', context)
# 
#         send_mail(subject, body, settings.DEFAULT_FROM_EMAIL,
#             [self.cleaned_data['new_email']])
# 
#         return verification
# 
# 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/mixins.py
# class LoginRequiredMixin(object):
#     """Ensures that the user is authenticated in order to access the view.
#     http://djangosnippets.org/snippets/2442/"""
# 
#     @method_decorator(login_required)
#     def dispatch(self, *args, **kwargs):
#         return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)
# 
# Path: userprofiles/settings.py
# class Settings(object):
#     def __init__(self, **kwargs):
#     def __getattr__(self, key):
# def validate_settings():


# Path: userprofiles/contrib/emailverification/views.py
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.shortcuts import redirect
from django.utils.translation import ugettext_lazy as _
from django.views.generic import FormView, TemplateView, RedirectView
from userprofiles.contrib.emailverification.forms import ChangeEmailForm
from userprofiles.contrib.emailverification.models import EmailVerification
from userprofiles.mixins import LoginRequiredMixin
from userprofiles.settings import up_settings

        return redirect('userprofiles_email_change_requested')

email_change = EmailChangeView.as_view()


class EmailChangeRequestedView(LoginRequiredMixin, TemplateView):
    template_name = 'userprofiles/email_change_requested.html'

    def get_context_data(self, **kwargs):
        return {
            'expiration_days': up_settings.EMAIL_VERIFICATION_DAYS
        }

email_change_requested = EmailChangeRequestedView.as_view()


class EmailChangeApproveView(LoginRequiredMixin, RedirectView):
    permanent = False

    def get_redirect_url(self, token, code):
        try:
            verification = EmailVerification.objects.get(token=token, code=code,
                user=self.request.user, is_expired=False, is_approved=False)

            verification.is_approved = True
            verification.save()

            messages.success(self.request, _(u'E-mail address changed to %(email)s' % {
                'email': verification.new_email}))


        except EmailVerification.DoesNotExist: