reg/forms.py
author nishanth
Fri, 09 Apr 2010 15:48:47 +0530
changeset 7 af9ab5ad2786
parent 6 057498d12450
child 8 e2699e042129
permissions -rw-r--r--
fixed a bug in registration .
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
     1
import string
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
     2
7
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
     3
from django import forms
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
     4
from django.contrib.auth.models import User
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
     5
from django.contrib.auth import authenticate
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
     6
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
     7
from workshop.reg.models import Profile
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
     8
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
     9
class LoginForm(forms.Form):
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    10
    """ a form to handle login.
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    11
    """
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    12
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    13
    email = forms.EmailField()
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    14
    password = forms.CharField(widget=forms.PasswordInput)
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    15
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    16
    def clean_email(self):
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    17
        """ see if a user exists for this email.
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    18
        """
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    19
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    20
        email = self.cleaned_data['email']
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    21
        password = self.data['password']
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    22
        try:
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    23
            username = User.objects.get(email__iexact=email).username
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    24
        except User.DoesNotExist:
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    25
            raise forms.ValidationError("Incorrect e-mail or password")
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    26
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    27
        user = authenticate(username=username, password=password)
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    28
        if not user:
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    29
            raise forms.ValidationError("Incorrect e-mail or password")
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    30
        return email
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    31
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    32
class RegisterForm(forms.ModelForm):
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    33
    """ add the fields email and password
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    34
    and then generate form using profile model.
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    35
    """
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    36
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    37
    email = forms.EmailField()
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    38
    password = forms.CharField(widget=forms.PasswordInput, help_text="Choose a good password which 8 to 30 chars long")
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    39
    confirm_password = forms.CharField(widget=forms.PasswordInput) 
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    40
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    41
    first_name = forms.CharField(required=True)
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    42
    last_name = forms.CharField()
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    43
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    44
    class Meta:
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    45
        model = Profile
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    46
        fields = ['email', 'password', 'confirm_password', 'first_name', 'last_name', 'gender', 'profession', 'affiliated_to', 'interests']
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    47
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    48
    def clean_email(self):
7
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    49
        """ check if a user exists with same email.
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    50
        """
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    51
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    52
        email = self.cleaned_data['email']
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    53
        try:
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    54
            User.objects.get(email__iexact=email)
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    55
            raise forms.ValidationError("An account already exists with this email.\
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    56
                                        Click on forgot password if you have forgotten your password")
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    57
        except User.DoesNotExist:
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    58
            return email
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    59
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    60
    def clean_password(self):
7
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    61
        """ check if the password contains only alphabets or digits or punctuation.
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    62
        then check if the size of the password is optimal.
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    63
        then check if both the given passwords match.
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    64
        """
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    65
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    66
        password = self.cleaned_data['password']
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    67
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    68
        if password.strip(string.ascii_letters+string.punctuation+string.digits):
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    69
            raise forms.ValidationError("Only alphabets, digits and punctuation symbols are allowed in password")
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    70
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    71
        if not 8 <= len(password) <= 30:
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    72
            raise forms.ValidationError("Password must be atleast 8 characters and at most 30 characters")
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    73
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    74
        if not password == self.data['confirm_password']:
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 4
diff changeset
    75
            raise forms.ValidationError("Passwords do not match")