-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
36 lines (26 loc) · 1.18 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from django import forms
from django.contrib.auth import authenticate, get_user_model
# login class
class UserLoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
def clean(self, *args, **kwargs):
username = self.cleaned_data.get('username')
password = self.cleaned_data.get('password')
if username and password:
validation = authenticate(username=username, password=password)
if not validation:
raise forms.ValidationError('This user does not exist')
if not validation.check_password(password):
raise forms.ValidationError('Incorrect password')
if not validation.is_active:
raise forms.ValidationError('this user is not active')
return super(UserLoginForm, self).clean(*args, **kwargs)
User = get_user_model() # get the custom model
# register class
class UserRegisterForm(forms.ModelForm):
email = forms.EmailField(label='Email Address')
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = ['username', 'email', 'password']