reg/forms.py
changeset 3 182f216da4a8
child 4 ededea9ad08b
equal deleted inserted replaced
2:c11afa8623f7 3:182f216da4a8
       
     1 from django.contrib.auth.models import User
       
     2 from django import forms
       
     3 
       
     4 from django.contrib.auth import authenticate
       
     5 
       
     6 class LoginForm(forms.ModelForm):
       
     7     """ a form to handle login.
       
     8     """
       
     9 
       
    10     class Meta:
       
    11         model = User
       
    12         fields = ['email', 'password']
       
    13 
       
    14     def clean_email(self):
       
    15         """ see if a user exists for this email.
       
    16         """
       
    17 
       
    18         email = self.cleaned_data['email']
       
    19         try:
       
    20             self.user = User.objects.get(email__iexact=email)
       
    21             return email
       
    22         except User.DoesNotExist:
       
    23             raise forms.ValidationError("Incorrect e-mail or password")
       
    24 
       
    25     def clean_password(self):
       
    26         """ now we know that the user exists.
       
    27         we see if he typed the password correctly.
       
    28         """
       
    29 
       
    30         password = self.cleaned_data['password']
       
    31         user = authenticate(self.user.username, password)
       
    32         if not user:
       
    33             raise forms.ValidationError("Incorrect e-mail or password")
       
    34         return password
       
    35