Skip to content

Instantly share code, notes, and snippets.

@matiasherranz
Created September 28, 2016 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matiasherranz/addb6d1e9f2d9406fa1e47a5c4baed1f to your computer and use it in GitHub Desktop.
Save matiasherranz/addb6d1e9f2d9406fa1e47a5c4baed1f to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib.auth import password_validation
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from accounts.models import User
class CustomUserCreationForm(UserCreationForm):
"""
A form that creates a user, with no privileges, from the given email and
password.
"""
def __init__(self, *args, **kargs):
super(CustomUserCreationForm, self).__init__(*args, **kargs)
if 'username' in self.fields:
print "here"
del self.fields['username']
class Meta:
model = User
fields = ("email",)
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(
self.error_messages['password_mismatch'],
code='password_mismatch',
)
self.instance.email = self.cleaned_data.get('email')
password_validation.validate_password(
self.cleaned_data.get('password2'), self.instance
)
return password2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment