reg/forms.py
changeset 4 ededea9ad08b
parent 3 182f216da4a8
child 6 057498d12450
equal deleted inserted replaced
3:182f216da4a8 4:ededea9ad08b
     1 from django.contrib.auth.models import User
     1 from django.contrib.auth.models import User
     2 from django import forms
     2 from django import forms
     3 
     3 
     4 from django.contrib.auth import authenticate
     4 from django.contrib.auth import authenticate
     5 
     5 
     6 class LoginForm(forms.ModelForm):
     6 class LoginForm(forms.Form):
     7     """ a form to handle login.
     7     """ a form to handle login.
     8     """
     8     """
     9 
     9 
    10     class Meta:
    10     email = forms.EmailField()
    11         model = User
    11     password = forms.CharField(widget=forms.PasswordInput)
    12         fields = ['email', 'password']
       
    13 
    12 
    14     def clean_email(self):
    13     def clean_email(self):
    15         """ see if a user exists for this email.
    14         """ see if a user exists for this email.
    16         """
    15         """
    17 
    16 
    18         email = self.cleaned_data['email']
    17         email = self.cleaned_data['email']
       
    18         password = self.data['password']
    19         try:
    19         try:
    20             self.user = User.objects.get(email__iexact=email)
    20             username = User.objects.get(email__iexact=email).username
    21             return email
       
    22         except User.DoesNotExist:
    21         except User.DoesNotExist:
    23             raise forms.ValidationError("Incorrect e-mail or password")
    22             raise forms.ValidationError("Incorrect e-mail or password")
    24 
    23 
    25     def clean_password(self):
    24         user = authenticate(username=username, password=password)
    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:
    25         if not user:
    33             raise forms.ValidationError("Incorrect e-mail or password")
    26             raise forms.ValidationError("Incorrect e-mail or password")
    34         return password
    27         return email
    35 
    28