mirror of
https://gitlab.science.ru.nl/technicie/MarietjeDjango.git
synced 2025-12-16 02:32:22 +01:00
Initial commit.
This commit is contained in:
74
marietje/marietje/forms.py
Normal file
74
marietje/marietje/forms.py
Normal file
@ -0,0 +1,74 @@
|
||||
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 is not None:
|
||||
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["password1"])
|
||||
if commit:
|
||||
user.save()
|
||||
return user
|
||||
Reference in New Issue
Block a user