# Path: authemail/models.py
# class SignupCode(AbstractBaseCode):
#     ipaddr = models.GenericIPAddressField(_('ip address'))
# 
#     objects = SignupCodeManager()
# 
#     def send_signup_email(self):
#         prefix = 'signup_email'
#         self.send_email(prefix)
# 
# class EmailChangeCode(AbstractBaseCode):
#     email = models.EmailField(_('email address'), max_length=255)
# 
#     objects = EmailChangeCodeManager()
# 
#     def send_email_change_emails(self):
#         prefix = 'email_change_notify_previous_email'
#         self.send_email(prefix)
# 
#         prefix = 'email_change_confirm_new_email'
#         ctxt = {
#             'email': self.email,
#             'code': self.code
#         }
# 
#         send_multi_format_email(prefix, ctxt, target_email=self.email)
# 
# class PasswordResetCode(AbstractBaseCode):
#     objects = PasswordResetCodeManager()
# 
#     def send_password_reset_email(self):
#         prefix = 'password_reset_email'
#         self.send_email(prefix)
# 
# Path: authemail/models.py
# def send_multi_format_email(template_prefix, template_ctxt, target_email):
#     subject_file = 'authemail/%s_subject.txt' % template_prefix
#     txt_file = 'authemail/%s.txt' % template_prefix
#     html_file = 'authemail/%s.html' % template_prefix
# 
#     subject = render_to_string(subject_file).strip()
#     from_email = settings.EMAIL_FROM
#     to = target_email
#     bcc_email = settings.EMAIL_BCC
#     text_content = render_to_string(txt_file, template_ctxt)
#     html_content = render_to_string(html_file, template_ctxt)
#     msg = EmailMultiAlternatives(subject, text_content, from_email, [to],
#                                  bcc=[bcc_email])
#     msg.attach_alternative(html_content, 'text/html')
#     msg.send()
# 
# Path: authemail/serializers.py
# class SignupSerializer(serializers.Serializer):
#     """
#     Don't require email to be unique so visitor can signup multiple times,
#     if misplace verification email.  Handle in view.
#     """
#     email = serializers.EmailField(max_length=255)
#     password = serializers.CharField(max_length=128)
#     first_name = serializers.CharField(max_length=30, default='',
#                                        required=False)
#     last_name = serializers.CharField(max_length=30, default='',
#                                       required=False)
# 
# class LoginSerializer(serializers.Serializer):
#     email = serializers.EmailField(max_length=255)
#     password = serializers.CharField(max_length=128)
# 
# Path: authemail/serializers.py
# class PasswordResetSerializer(serializers.Serializer):
#     email = serializers.EmailField(max_length=255)
# 
# Path: authemail/serializers.py
# class PasswordResetVerifiedSerializer(serializers.Serializer):
#     code = serializers.CharField(max_length=40)
#     password = serializers.CharField(max_length=128)
# 
# Path: authemail/serializers.py
# class EmailChangeSerializer(serializers.Serializer):
#     email = serializers.EmailField(max_length=255)
# 
# Path: authemail/serializers.py
# class PasswordChangeSerializer(serializers.Serializer):
#     password = serializers.CharField(max_length=128)
# 
# Path: authemail/serializers.py
# class UserSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = get_user_model()
#         fields = ('email', 'first_name', 'last_name')


from datetime import date
from django.conf import settings
from django.contrib.auth import authenticate, get_user_model
from django.utils.translation import gettext as _
from rest_framework import status
from rest_framework.authtoken.models import Token
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from authemail.models import SignupCode, EmailChangeCode, PasswordResetCode
from authemail.models import send_multi_format_email
from authemail.serializers import SignupSerializer, LoginSerializer
from authemail.serializers import PasswordResetSerializer
from authemail.serializers import PasswordResetVerifiedSerializer
from authemail.serializers import EmailChangeSerializer
from authemail.serializers import PasswordChangeSerializer
from authemail.serializers import UserSerializer
            try:
                user = get_user_model().objects.get(email=email)
                if user.is_verified:
                    content = {'detail': _('Email address already taken.')}
                    return Response(content, status=status.HTTP_400_BAD_REQUEST)

                try:
                    # Delete old signup codes
                    signup_code = SignupCode.objects.get(user=user)
                    signup_code.delete()
                except SignupCode.DoesNotExist:
                    pass

            except get_user_model().DoesNotExist:
                user = get_user_model().objects.create_user(email=email)

            # Set user fields provided
            user.set_password(password)
            user.first_name = first_name
            user.last_name = last_name
            if not must_validate_email:
                user.is_verified = True
                send_multi_format_email('welcome_email',
                                        {'email': user.email, },
                                        target_email=user.email)
            user.save()

            if must_validate_email:
                # Create and associate signup code
                ipaddr = self.request.META.get('REMOTE_ADDR', '0.0.0.0')

# Path: authemail/models.py
# class SignupCode(AbstractBaseCode):
#     ipaddr = models.GenericIPAddressField(_('ip address'))
# 
#     objects = SignupCodeManager()
# 
#     def send_signup_email(self):
#         prefix = 'signup_email'
#         self.send_email(prefix)
# 
# class EmailChangeCode(AbstractBaseCode):
#     email = models.EmailField(_('email address'), max_length=255)
# 
#     objects = EmailChangeCodeManager()
# 
#     def send_email_change_emails(self):
#         prefix = 'email_change_notify_previous_email'
#         self.send_email(prefix)
# 
#         prefix = 'email_change_confirm_new_email'
#         ctxt = {
#             'email': self.email,
#             'code': self.code
#         }
# 
#         send_multi_format_email(prefix, ctxt, target_email=self.email)
# 
# class PasswordResetCode(AbstractBaseCode):
#     objects = PasswordResetCodeManager()
# 
#     def send_password_reset_email(self):
#         prefix = 'password_reset_email'
#         self.send_email(prefix)
# 
# Path: authemail/models.py
# def send_multi_format_email(template_prefix, template_ctxt, target_email):
#     subject_file = 'authemail/%s_subject.txt' % template_prefix
#     txt_file = 'authemail/%s.txt' % template_prefix
#     html_file = 'authemail/%s.html' % template_prefix
# 
#     subject = render_to_string(subject_file).strip()
#     from_email = settings.EMAIL_FROM
#     to = target_email
#     bcc_email = settings.EMAIL_BCC
#     text_content = render_to_string(txt_file, template_ctxt)
#     html_content = render_to_string(html_file, template_ctxt)
#     msg = EmailMultiAlternatives(subject, text_content, from_email, [to],
#                                  bcc=[bcc_email])
#     msg.attach_alternative(html_content, 'text/html')
#     msg.send()
# 
# Path: authemail/serializers.py
# class SignupSerializer(serializers.Serializer):
#     """
#     Don't require email to be unique so visitor can signup multiple times,
#     if misplace verification email.  Handle in view.
#     """
#     email = serializers.EmailField(max_length=255)
#     password = serializers.CharField(max_length=128)
#     first_name = serializers.CharField(max_length=30, default='',
#                                        required=False)
#     last_name = serializers.CharField(max_length=30, default='',
#                                       required=False)
# 
# class LoginSerializer(serializers.Serializer):
#     email = serializers.EmailField(max_length=255)
#     password = serializers.CharField(max_length=128)
# 
# Path: authemail/serializers.py
# class PasswordResetSerializer(serializers.Serializer):
#     email = serializers.EmailField(max_length=255)
# 
# Path: authemail/serializers.py
# class PasswordResetVerifiedSerializer(serializers.Serializer):
#     code = serializers.CharField(max_length=40)
#     password = serializers.CharField(max_length=128)
# 
# Path: authemail/serializers.py
# class EmailChangeSerializer(serializers.Serializer):
#     email = serializers.EmailField(max_length=255)
# 
# Path: authemail/serializers.py
# class PasswordChangeSerializer(serializers.Serializer):
#     password = serializers.CharField(max_length=128)
# 
# Path: authemail/serializers.py
# class UserSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = get_user_model()
#         fields = ('email', 'first_name', 'last_name')


# Path: authemail/views.py
from datetime import date
from django.conf import settings
from django.contrib.auth import authenticate, get_user_model
from django.utils.translation import gettext as _
from rest_framework import status
from rest_framework.authtoken.models import Token
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from authemail.models import SignupCode, EmailChangeCode, PasswordResetCode
from authemail.models import send_multi_format_email
from authemail.serializers import SignupSerializer, LoginSerializer
from authemail.serializers import PasswordResetSerializer
from authemail.serializers import PasswordResetVerifiedSerializer
from authemail.serializers import EmailChangeSerializer
from authemail.serializers import PasswordChangeSerializer
from authemail.serializers import UserSerializer

            try:
                user = get_user_model().objects.get(email=email)
                if user.is_verified:
                    content = {'detail': _('Email address already taken.')}
                    return Response(content, status=status.HTTP_400_BAD_REQUEST)

                try:
                    # Delete old signup codes
                    signup_code = SignupCode.objects.get(user=user)
                    signup_code.delete()
                except SignupCode.DoesNotExist:
                    pass

            except get_user_model().DoesNotExist:
                user = get_user_model().objects.create_user(email=email)

            # Set user fields provided
            user.set_password(password)
            user.first_name = first_name
            user.last_name = last_name
            if not must_validate_email:
                user.is_verified = True
                send_multi_format_email('welcome_email',
                                        {'email': user.email, },
                                        target_email=user.email)
            user.save()

            if must_validate_email:
                # Create and associate signup code
                ipaddr = self.request.META.get('REMOTE_ADDR', '0.0.0.0')

                signup_code = SignupCode.objects.create_signup_code(user, ipaddr)