# Path: authemail/forms.py
# class EmailUserCreationForm(forms.ModelForm):
#     """
#     A form that creates a user, with no privileges, from the given email and
#     password.
#     """
#     password1 = forms.CharField(label=_('Password'),
#                                 widget=forms.PasswordInput)
#     password2 = forms.CharField(label=_('Password confirmation'),
#                                 widget=forms.PasswordInput,
#                                 help_text=_('Enter the same password as '
#                                 'above, for verification.'))
# 
#     class Meta:
#         model = get_user_model()
#         fields = ('email',)
# 
#     def clean_email(self):
#         email = self.cleaned_data.get('email')
#         try:
#             get_user_model().objects.get(email=email)
#         except get_user_model().DoesNotExist:
#             return email
#         raise forms.ValidationError(_('A user with that email already exists.'))
# 
#     def clean_password2(self):
#         password1 = self.cleaned_data.get('password1')
#         password2 = self.cleaned_data.get('password2')
#         if password1 and password2 and password1 != password2:
#             raise forms.ValidationError(
#                 _('The two password fields did not match.'))
#         return password2
# 
#     def save(self, commit=True):
#         user = super(EmailUserCreationForm, self).save(commit=False)
#         user.set_password(self.cleaned_data['password1'])
#         if commit:
#             user.save()
#         return user
# 
# class EmailUserChangeForm(UserChangeForm):
#     class Meta:
#         model = get_user_model()
#         fields = '__all__'
# 
#     def __init__(self, *args, **kwargs):
#         super(EmailUserChangeForm, self).__init__(*args, **kwargs)
#         if 'username' in self.fields:
#             del self.fields['username']
# 
# 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 PasswordResetCode(AbstractBaseCode):
#     objects = PasswordResetCodeManager()
# 
#     def send_password_reset_email(self):
#         prefix = 'password_reset_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)


from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import ugettext_lazy as _
from authemail.forms import EmailUserCreationForm, EmailUserChangeForm
from authemail.models import SignupCode, PasswordResetCode, EmailChangeCode
    readonly_fields = ('code', 'email', 'created_at')

    def has_add_permission(self, request, obj=None):
        return False


class EmailUserAdmin(UserAdmin):
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        (_('Personal Info'), {'fields': ('first_name', 'last_name')}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2'),
        }),
    )
    form = EmailUserChangeForm
    add_form = EmailUserCreationForm
    inlines = [SignupCodeInline, EmailChangeCodeInline, PasswordResetCodeInline]
    list_display = ('email', 'is_verified', 'first_name', 'last_name',
                    'is_staff')
    search_fields = ('first_name', 'last_name', 'email')
    ordering = ('email',)


admin.site.register(get_user_model(), EmailUserAdmin)

# Path: authemail/forms.py
# class EmailUserCreationForm(forms.ModelForm):
#     """
#     A form that creates a user, with no privileges, from the given email and
#     password.
#     """
#     password1 = forms.CharField(label=_('Password'),
#                                 widget=forms.PasswordInput)
#     password2 = forms.CharField(label=_('Password confirmation'),
#                                 widget=forms.PasswordInput,
#                                 help_text=_('Enter the same password as '
#                                 'above, for verification.'))
# 
#     class Meta:
#         model = get_user_model()
#         fields = ('email',)
# 
#     def clean_email(self):
#         email = self.cleaned_data.get('email')
#         try:
#             get_user_model().objects.get(email=email)
#         except get_user_model().DoesNotExist:
#             return email
#         raise forms.ValidationError(_('A user with that email already exists.'))
# 
#     def clean_password2(self):
#         password1 = self.cleaned_data.get('password1')
#         password2 = self.cleaned_data.get('password2')
#         if password1 and password2 and password1 != password2:
#             raise forms.ValidationError(
#                 _('The two password fields did not match.'))
#         return password2
# 
#     def save(self, commit=True):
#         user = super(EmailUserCreationForm, self).save(commit=False)
#         user.set_password(self.cleaned_data['password1'])
#         if commit:
#             user.save()
#         return user
# 
# class EmailUserChangeForm(UserChangeForm):
#     class Meta:
#         model = get_user_model()
#         fields = '__all__'
# 
#     def __init__(self, *args, **kwargs):
#         super(EmailUserChangeForm, self).__init__(*args, **kwargs)
#         if 'username' in self.fields:
#             del self.fields['username']
# 
# 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 PasswordResetCode(AbstractBaseCode):
#     objects = PasswordResetCodeManager()
# 
#     def send_password_reset_email(self):
#         prefix = 'password_reset_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)


# Path: authemail/admin.py
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import ugettext_lazy as _
from authemail.forms import EmailUserCreationForm, EmailUserChangeForm
from authemail.models import SignupCode, PasswordResetCode, EmailChangeCode

    readonly_fields = ('code', 'email', 'created_at')

    def has_add_permission(self, request, obj=None):
        return False


class EmailUserAdmin(UserAdmin):
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        (_('Personal Info'), {'fields': ('first_name', 'last_name')}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2'),
        }),
    )
    form = EmailUserChangeForm
    add_form = EmailUserCreationForm
    inlines = [SignupCodeInline, EmailChangeCodeInline, PasswordResetCodeInline]
    list_display = ('email', 'is_verified', 'first_name', 'last_name',
                    'is_staff')
    search_fields = ('first_name', 'last_name', 'email')
    ordering = ('email',)


admin.site.register(get_user_model(), EmailUserAdmin)

admin.site.register(SignupCode, SignupCodeAdmin)