reg/forms.py
author nishanth
Fri, 09 Apr 2010 11:46:35 +0530
changeset 3 182f216da4a8
child 4 ededea9ad08b
permissions -rw-r--r--
made the login view. have to write templates and check it.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
     1
from django.contrib.auth.models import User
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
     2
from django import forms
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
     3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
     4
from django.contrib.auth import authenticate
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
     5
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
     6
class LoginForm(forms.ModelForm):
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
     7
    """ a form to handle login.
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
     8
    """
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
     9
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    10
    class Meta:
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    11
        model = User
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    12
        fields = ['email', 'password']
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    13
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    14
    def clean_email(self):
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    15
        """ see if a user exists for this email.
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    16
        """
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    17
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    18
        email = self.cleaned_data['email']
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    19
        try:
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    20
            self.user = User.objects.get(email__iexact=email)
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    21
            return email
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    22
        except User.DoesNotExist:
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    23
            raise forms.ValidationError("Incorrect e-mail or password")
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    24
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    25
    def clean_password(self):
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    26
        """ now we know that the user exists.
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    27
        we see if he typed the password correctly.
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    28
        """
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    29
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    30
        password = self.cleaned_data['password']
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    31
        user = authenticate(self.user.username, password)
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    32
        if not user:
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    33
            raise forms.ValidationError("Incorrect e-mail or password")
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    34
        return password
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents:
diff changeset
    35