# Path: user_management/utils/views.py
# class VerifyAccountViewMixin(object):
#     """
#     Verify a new user's email address.
# 
#     Verify a newly created account by checking the `token` in the URL kwargs.
#     """
#     ok_message = _('Your account has been verified.')
#     invalid_exception_class = InvalidExpiredToken
#     permission_denied_class = PermissionDenied
# 
#     # Default token never expires.
#     DEFAULT_VERIFY_ACCOUNT_EXPIRY = None
# 
#     def verify_token(self, request, *args, **kwargs):
#         """
#         Use `token` to allow one-time access to a view.
# 
#         Set the user as a class attribute or raise an `InvalidExpiredToken`.
# 
#         Token expiry can be set in `settings` with `VERIFY_ACCOUNT_EXPIRY` and is
#         set in seconds.
#         """
#         User = get_user_model()
# 
#         try:
#             max_age = settings.VERIFY_ACCOUNT_EXPIRY
#         except AttributeError:
#             max_age = self.DEFAULT_VERIFY_ACCOUNT_EXPIRY
# 
#         try:
#             email_data = signing.loads(kwargs['token'], max_age=max_age)
#         except signing.BadSignature:
#             raise self.invalid_exception_class
# 
#         email = email_data['email']
# 
#         try:
#             self.user = User.objects.get_by_natural_key(email)
#         except User.DoesNotExist:
#             raise self.invalid_exception_class
# 
#         if self.user.email_verified:
#             raise self.permission_denied_class
# 
#     def activate_user(self):
#         self.user.email_verified = True
#         self.user.is_active = True
#         self.user.save()
# 
# Path: user_management/ui/exceptions.py
# class AlreadyVerifiedException(Exception):
#     message = _('Email already verified.')
# 
# class InvalidExpiredToken(Http404):
#     """Exception to confirm an account."""
#     message = _('Invalid or expired token.')


from django.conf import settings
from django.contrib import auth, messages
from django.shortcuts import resolve_url
from django.utils.translation import ugettext_lazy as _
from django.views import generic
from user_management.utils.views import VerifyAccountViewMixin
from .exceptions import AlreadyVerifiedException, InvalidExpiredToken



class VerifyUserEmailView(VerifyAccountViewMixin, generic.RedirectView):
    """
    A view which verifies a user's email address.

    Accessed via a link in an email sent to the user, which contains both a
    uid generated from the user's pk, and a token also generated from the user
    object, for verification.  If everything lines up, it makes the user
    active.

    As a RedirectView, this will return a HTTP 302 to LOGIN_URL on success.
    """
    permanent = False
    already_verified = False
    success_message = _('Your email address was confirmed.')
    already_verified_message = _('Your email is already confirmed.')

# Path: user_management/utils/views.py
# class VerifyAccountViewMixin(object):
#     """
#     Verify a new user's email address.
# 
#     Verify a newly created account by checking the `token` in the URL kwargs.
#     """
#     ok_message = _('Your account has been verified.')
#     invalid_exception_class = InvalidExpiredToken
#     permission_denied_class = PermissionDenied
# 
#     # Default token never expires.
#     DEFAULT_VERIFY_ACCOUNT_EXPIRY = None
# 
#     def verify_token(self, request, *args, **kwargs):
#         """
#         Use `token` to allow one-time access to a view.
# 
#         Set the user as a class attribute or raise an `InvalidExpiredToken`.
# 
#         Token expiry can be set in `settings` with `VERIFY_ACCOUNT_EXPIRY` and is
#         set in seconds.
#         """
#         User = get_user_model()
# 
#         try:
#             max_age = settings.VERIFY_ACCOUNT_EXPIRY
#         except AttributeError:
#             max_age = self.DEFAULT_VERIFY_ACCOUNT_EXPIRY
# 
#         try:
#             email_data = signing.loads(kwargs['token'], max_age=max_age)
#         except signing.BadSignature:
#             raise self.invalid_exception_class
# 
#         email = email_data['email']
# 
#         try:
#             self.user = User.objects.get_by_natural_key(email)
#         except User.DoesNotExist:
#             raise self.invalid_exception_class
# 
#         if self.user.email_verified:
#             raise self.permission_denied_class
# 
#     def activate_user(self):
#         self.user.email_verified = True
#         self.user.is_active = True
#         self.user.save()
# 
# Path: user_management/ui/exceptions.py
# class AlreadyVerifiedException(Exception):
#     message = _('Email already verified.')
# 
# class InvalidExpiredToken(Http404):
#     """Exception to confirm an account."""
#     message = _('Invalid or expired token.')


# Path: user_management/ui/views.py
from django.conf import settings
from django.contrib import auth, messages
from django.shortcuts import resolve_url
from django.utils.translation import ugettext_lazy as _
from django.views import generic
from user_management.utils.views import VerifyAccountViewMixin
from .exceptions import AlreadyVerifiedException, InvalidExpiredToken




class VerifyUserEmailView(VerifyAccountViewMixin, generic.RedirectView):
    """
    A view which verifies a user's email address.

    Accessed via a link in an email sent to the user, which contains both a
    uid generated from the user's pk, and a token also generated from the user
    object, for verification.  If everything lines up, it makes the user
    active.

    As a RedirectView, this will return a HTTP 302 to LOGIN_URL on success.
    """
    permanent = False
    already_verified = False
    success_message = _('Your email address was confirmed.')
    already_verified_message = _('Your email is already confirmed.')

    invalid_exception_class = InvalidExpiredToken