2017-04-28 22:01:46 +02:00
|
|
|
from django import forms
|
|
|
|
from django.forms import ModelForm
|
|
|
|
|
2017-04-30 01:44:17 +02:00
|
|
|
from django.contrib.auth import get_user_model
|
2017-05-02 19:03:56 +02:00
|
|
|
from django.contrib.auth.password_validation import validate_password
|
2017-04-28 22:01:46 +02:00
|
|
|
|
|
|
|
class UserCreateForm(ModelForm):
|
2017-04-30 01:27:26 +02:00
|
|
|
password1 = forms.CharField(label='Passwort')
|
|
|
|
password2 = forms.CharField(label='Passwort bestätigen')
|
2017-04-28 22:01:46 +02:00
|
|
|
|
|
|
|
class Meta:
|
2017-04-30 01:44:17 +02:00
|
|
|
model = get_user_model()
|
2017-04-28 22:01:46 +02:00
|
|
|
fields = ['username', 'email', 'twitter', 'github']
|
|
|
|
|
2017-05-02 19:03:56 +02:00
|
|
|
def clean_password1(self):
|
|
|
|
password1 = self.cleaned_data.get("password1")
|
|
|
|
|
|
|
|
validate_password(password1)
|
|
|
|
|
|
|
|
return password1
|
|
|
|
|
2017-04-28 22:01:46 +02:00
|
|
|
|
|
|
|
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("Passwords don't match")
|
|
|
|
|
|
|
|
return password1
|
|
|
|
|
|
|
|
|
|
|
|
def clean_email(self):
|
|
|
|
#Make sure Email is unique
|
|
|
|
email = self.cleaned_data.get("email")
|
2017-05-02 00:11:34 +02:00
|
|
|
if get_user_model().objects.filter(email = email):
|
2017-04-28 22:01:46 +02:00
|
|
|
raise forms.ValidationError("Email already in use.")
|
|
|
|
|
|
|
|
return email
|
|
|
|
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
user = super(UserCreateForm, self).save(commit=False)
|
|
|
|
user.set_password(self.cleaned_data["password1"])
|
|
|
|
user.is_active = False
|
|
|
|
if commit:
|
|
|
|
user.save()
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UserUpdateForm(ModelForm):
|
|
|
|
new_password1 = forms.CharField(label='New Password',
|
|
|
|
widget=forms.PasswordInput,
|
|
|
|
required=False)
|
|
|
|
new_password2 = forms.CharField(label='New Password confirmation',
|
|
|
|
widget=forms.PasswordInput,
|
|
|
|
required=False)
|
|
|
|
current_password = forms.CharField(label='Current Password', widget=forms.PasswordInput)
|
|
|
|
|
|
|
|
class Meta:
|
2017-04-30 01:44:17 +02:00
|
|
|
model = get_user_model()
|
2017-04-28 22:01:46 +02:00
|
|
|
fields = ['email', 'twitter', 'github']
|
|
|
|
|
|
|
|
|
|
|
|
def clean_current_password(self):
|
|
|
|
current_password = self.cleaned_data.get("current_password")
|
|
|
|
|
|
|
|
if not self.instance.check_password(current_password):
|
|
|
|
raise forms.ValidationError("Password incorrect")
|
|
|
|
|
|
|
|
return current_password
|
|
|
|
|
|
|
|
|
2017-05-02 21:33:23 +02:00
|
|
|
def clean_new_password1(self):
|
|
|
|
password1 = self.cleaned_data.get("new_password1")
|
|
|
|
|
|
|
|
if password1 != '':
|
|
|
|
validate_password(password1)
|
|
|
|
|
|
|
|
return password1
|
|
|
|
|
|
|
|
|
2017-04-28 22:01:46 +02:00
|
|
|
def clean_new_password2(self):
|
|
|
|
password1 = self.cleaned_data.get("new_password1")
|
|
|
|
password2 = self.cleaned_data.get("new_password2")
|
|
|
|
|
|
|
|
if password1 and password2 and password1 != password2:
|
|
|
|
raise forms.ValidationError("Passwords don't match")
|
|
|
|
|
|
|
|
return password1
|
|
|
|
|
|
|
|
|
|
|
|
def clean_email(self):
|
|
|
|
#Make sure Email is still unique
|
|
|
|
email = self.cleaned_data.get("email")
|
2017-05-02 00:11:34 +02:00
|
|
|
if get_user_model().objects.filter(email = email).exclude(id=self.instance.id):
|
2017-04-28 22:01:46 +02:00
|
|
|
raise forms.ValidationError("Email already in use.")
|
|
|
|
|
|
|
|
return email
|
|
|
|
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
user = super(UserUpdateForm, self).save(commit=False)
|
|
|
|
|
|
|
|
password = self.cleaned_data.get("new_password1")
|
|
|
|
if password:
|
|
|
|
user.set_password(password)
|
|
|
|
|
|
|
|
if commit:
|
|
|
|
user.save()
|
|
|
|
return user
|