reg/forms.py
changeset 3 182f216da4a8
child 4 ededea9ad08b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/reg/forms.py	Fri Apr 09 11:46:35 2010 +0530
@@ -0,0 +1,35 @@
+from django.contrib.auth.models import User
+from django import forms
+
+from django.contrib.auth import authenticate
+
+class LoginForm(forms.ModelForm):
+    """ a form to handle login.
+    """
+
+    class Meta:
+        model = User
+        fields = ['email', 'password']
+
+    def clean_email(self):
+        """ see if a user exists for this email.
+        """
+
+        email = self.cleaned_data['email']
+        try:
+            self.user = User.objects.get(email__iexact=email)
+            return email
+        except User.DoesNotExist:
+            raise forms.ValidationError("Incorrect e-mail or password")
+
+    def clean_password(self):
+        """ now we know that the user exists.
+        we see if he typed the password correctly.
+        """
+
+        password = self.cleaned_data['password']
+        user = authenticate(self.user.username, password)
+        if not user:
+            raise forms.ValidationError("Incorrect e-mail or password")
+        return password
+