from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.core.exceptions import ValidationError
from Home.models import ProfilePic
class UserLoginForm(forms.Form):
"""Form to be used to log user in"""
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
class ProfilePicForm(forms.Form):
"""Form that is used to update profile picture
of the user
"""
class Meta:
model=ProfilePic
fields = ['avatar']
class UserRegistrationForm(UserCreationForm):
"""Form to be used to register a user"""
password1 = forms.CharField(
label="Password",
widget=forms.PasswordInput,
)
password2 = forms.CharField(
label="Password Confirmation",
widget=forms.PasswordInput,
)
def __init__(self, *args, **kwargs):
super(UserRegistrationForm, self).__init__(*args, **kwargs)
self.fields['email'].required = True
class Meta:
"""Specify fields you want to expose here"""
model = User
fields = ['email', 'username', 'password1', 'password2']
def clean_email(self):
email = self.cleaned_data.get('email')
username = self.cleaned_data.get('username')
if User.objects.filter(email=email).exclude(username=username):
raise forms.ValidationError(u'This email address is already taken')
return email
def clean_password2(self):
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if not password1 or not password2:
raise ValidationError("Please confirm your passwrod")
if password1 != password2:
raise ValidationError("Password are not the same")
|