Files
MarietjeDjango/marietje/marietje/forms.py
2017-01-30 03:39:33 +01:00

107 lines
3.6 KiB
Python

from django import forms
from django.contrib.auth.forms import AuthenticationForm as BaseAuthenticationForm, UsernameField
from django.contrib.auth import get_user_model, password_validation
from django.utils.translation import ugettext_lazy as _
class AuthenticationForm(BaseAuthenticationForm):
def __init__(self, request=None, *args, **kwargs):
super(AuthenticationForm, self).__init__(request, *args, **kwargs)
def confirm_login_allowed(self, user):
if user.activation_token:
raise forms.ValidationError(
self.error_messages['inactive'],
code='inactive',
)
super(AuthenticationForm, self).confirm_login_allowed(user)
class RegistrationForm(forms.ModelForm):
error_messages = {
'password_mismatch': _("The two password fields didn't match."),
}
password1 = forms.CharField(
label=_("Password"),
strip=False,
widget=forms.PasswordInput,
)
password2 = forms.CharField(
label=_("Password confirmation"),
widget=forms.PasswordInput,
strip=False,
help_text=_("Enter the same password as before, for verification."),
)
class Meta:
model = get_user_model()
fields = ('email',)
field_classes = {'email': UsernameField}
def __init__(self, *args, **kwargs):
super(RegistrationForm, self).__init__(*args, **kwargs)
if self._meta.model.USERNAME_FIELD in self.fields:
self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update({'autofocus': ''})
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if len(password1) < 6:
raise forms.ValidationError(
_("A password must be at least 6 characters."),
code='password_length',
)
if password1 and password2 and password1 != password2:
raise forms.ValidationError(
self.error_messages['password_mismatch'],
code='password_mismatch',
)
self.instance.username = self.cleaned_data.get('username')
password_validation.validate_password(self.cleaned_data.get('password2'), self.instance)
return password2
def clean_email(self):
email = self.cleaned_data.get("email") + '@science.ru.nl'
return email
def save(self, commit=True):
user = super(RegistrationForm, self).save(commit=False)
user.set_password(self.cleaned_data.get("password1"))
if commit:
user.save()
return user
class ResetPasswordForm(forms.Form):
error_messages = {
'password_mismatch': _("The two password fields didn't match."),
}
password1 = forms.CharField(
label=_("Password"),
strip=False,
widget=forms.PasswordInput,
)
password2 = forms.CharField(
label=_("Password confirmation"),
widget=forms.PasswordInput,
strip=False,
help_text=_("Enter the same password as before, for verification."),
)
def __init__(self, *args, **kwargs):
super(ResetPasswordForm, self).__init__(*args, **kwargs)
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',
)
password_validation.validate_password(self.cleaned_data.get('password2'))
return password2