reg/forms.py
author nishanth
Fri, 09 Apr 2010 15:35:40 +0530
changeset 6 057498d12450
parent 4 ededea9ad08b
child 7 af9ab5ad2786
permissions -rw-r--r--
users can now register but still there is no concept of activation e-mail .

import string

from django.contrib.auth.models import User
from django import forms

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):
        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):
        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")