# HG changeset patch # User nishanth # Date 1270793795 -19800 # Node ID 182f216da4a8e0c368caf88b52c1e596a91d8ede # Parent c11afa8623f780c29d147a59cca73d1e64c97adc made the login view. have to write templates and check it. diff -r c11afa8623f7 -r 182f216da4a8 reg/forms.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 + diff -r c11afa8623f7 -r 182f216da4a8 reg/views.py --- 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})