made the login view. have to write templates and check it.
authornishanth
Fri, 09 Apr 2010 11:46:35 +0530
changeset 3 182f216da4a8
parent 2 c11afa8623f7
child 4 ededea9ad08b
made the login view. have to write templates and check it.
reg/forms.py
reg/views.py
--- /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
+
--- a/reg/views.py	Thu Apr 08 22:28:15 2010 +0530
+++ b/reg/views.py	Fri Apr 09 11:46:35 2010 +0530
@@ -1,7 +1,30 @@
-from django.shortcuts import render_to_response
+from django.shortcuts import render_to_response, redirect
+
+from django.contrib.auth import authenticate, login, logout
+from django.contrib.auth.decorators import login_required
 
-def homepage(request):
-    """ List all the workshops and their status.
+from workshop.reg import forms as reg_forms
+
+def login(request):
+    """ get the user object from e-mail and then check for password.
     """
 
-    events = Event.objects.all()
+    user = request.user
+    if user.is_authenticated():
+        return redirect('/reg')
+
+    if request.method == "POST":
+        form = reg_forms.LoginForm(request.POST)
+        if form.is_valid():
+            email = form.cleaned_data['email']
+            password = form.cleaned_data['password']
+            username = User.objects.get(email__iexact=email)
+
+            user = authenticate(username, password)
+            login(request, user)
+            return redirect('/reg')
+        else:
+            return render_to_response('login.html', {'form':form})
+    else:
+        form = LoginForm()
+        return render_to_response('login.html', {'form':form})