fixed a bug in registration .
import string
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth import authenticate
from workshop.reg.models import Profile
class LoginForm(forms.Form):
""" a form to handle login.
"""
email = forms.EmailField()
password = forms.CharField(widget=forms.PasswordInput)
def clean_email(self):
""" see if a user exists for this email.
"""
email = self.cleaned_data['email']
password = self.data['password']
try:
username = User.objects.get(email__iexact=email).username
except User.DoesNotExist:
raise forms.ValidationError("Incorrect e-mail or password")
user = authenticate(username=username, password=password)
if not user:
raise forms.ValidationError("Incorrect e-mail or password")
return email
class RegisterForm(forms.ModelForm):
""" add the fields email and password
and then generate form using profile model.
"""
email = forms.EmailField()
password = forms.CharField(widget=forms.PasswordInput, help_text="Choose a good password which 8 to 30 chars long")
confirm_password = forms.CharField(widget=forms.PasswordInput)
first_name = forms.CharField(required=True)
last_name = forms.CharField()
class Meta:
model = Profile
fields = ['email', 'password', 'confirm_password', 'first_name', 'last_name', 'gender', 'profession', 'affiliated_to', 'interests']
def clean_email(self):
""" check if a user exists with same email.
"""
email = self.cleaned_data['email']
try:
User.objects.get(email__iexact=email)
raise forms.ValidationError("An account already exists with this email.\
Click on forgot password if you have forgotten your password")
except User.DoesNotExist:
return email
def clean_password(self):
""" check if the password contains only alphabets or digits or punctuation.
then check if the size of the password is optimal.
then check if both the given passwords match.
"""
password = self.cleaned_data['password']
if password.strip(string.ascii_letters+string.punctuation+string.digits):
raise forms.ValidationError("Only alphabets, digits and punctuation symbols are allowed in password")
if not 8 <= len(password) <= 30:
raise forms.ValidationError("Password must be atleast 8 characters and at most 30 characters")
if not password == self.data['confirm_password']:
raise forms.ValidationError("Passwords do not match")