class RegistrationForm(forms.Form):
username = UsernameField()
email = forms.EmailField(label=_('Email'))
password = PasswordField(label=_('Password'))
password_dup = PasswordField(label=_('Password (confirmation)'))
def __init__(self, *args, **kwargs):
super(RegistrationForm, self).__init__(*args, **kwargs)
def clean_email(self):
try:
User.objects.get(email__exact=self.cleaned_data['email'])
except User.DoesNotExist:
return self.cleaned_data['email']
except KeyError:
pass
else:
raise forms.ValidationError(_(u'This email already registered'))
def clean(self):
pwd1 = self.cleaned_data.get('password')
pwd2 = self.cleaned_data.get('password_dup')
if pwd1 and pwd2:
if pwd1 != pwd2:
self._errors['password_dup'] = [_('Passwords do not match')]
return self.cleaned_data
def save(self):
username = self.cleaned_data['username']
email = self.cleaned_data['email']
password = self.cleaned_data['password']
user = User.objects.create_user(username, email, password=password)
return user