login and logout works .
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/login.html Fri Apr 09 12:28:58 2010 +0530
@@ -0,0 +1,12 @@
+<html>
+<head>
+<title>
+</title>
+</head>
+<body>
+<form action="" method="post">
+{{ form.as_p }}
+<input type="submit" value="Login">
+</form>
+</body>
+</html>
--- a/reg/forms.py Fri Apr 09 11:46:35 2010 +0530
+++ b/reg/forms.py Fri Apr 09 12:28:58 2010 +0530
@@ -3,33 +3,26 @@
from django.contrib.auth import authenticate
-class LoginForm(forms.ModelForm):
+class LoginForm(forms.Form):
""" a form to handle login.
"""
- class Meta:
- model = User
- fields = ['email', 'password']
+ email = forms.EmailField()
+ password = forms.CharField(widget=forms.PasswordInput)
def clean_email(self):
""" see if a user exists for this email.
"""
email = self.cleaned_data['email']
+ password = self.data['password']
try:
- self.user = User.objects.get(email__iexact=email)
- return email
+ username = User.objects.get(email__iexact=email).username
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)
+ user = authenticate(username=username, password=password)
if not user:
raise forms.ValidationError("Incorrect e-mail or password")
- return password
+ return email
--- a/reg/site/urls.py Fri Apr 09 11:46:35 2010 +0530
+++ b/reg/site/urls.py Fri Apr 09 12:28:58 2010 +0530
@@ -3,5 +3,7 @@
from workshop.reg import views as reg_views
urlpatterns = patterns('',
- ('^$', reg_views.something),
+ ('^$', reg_views.homepage),
+ ('^login/$', reg_views.user_login),
+ ('^logout/$', reg_views.user_logout),
)
--- a/reg/views.py Fri Apr 09 11:46:35 2010 +0530
+++ b/reg/views.py Fri Apr 09 12:28:58 2010 +0530
@@ -1,11 +1,29 @@
-from django.shortcuts import render_to_response, redirect
-
+from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
+from django.shortcuts import render_to_response, redirect
+
from workshop.reg import forms as reg_forms
-def login(request):
+from django.http import HttpResponse
+
+def homepage(request):
+ """ see if the user is active.
+ If not, only show the re send activation email link.
+ else show all the options in homepage.
+ """
+
+ user = request.user
+
+ if not user.is_authenticated():
+ return HttpResponse('not logged in<br><a href="/reg/login">login</a>')
+ elif not user.is_active:
+ return HttpResponse('not active<br /><a href="/reg/logout">logout</a>')
+ else:
+ return HttpResponse('you are ogey<br /><a href="/reg/logout">logout</a>')
+
+def user_login(request):
""" get the user object from e-mail and then check for password.
"""
@@ -18,13 +36,23 @@
if form.is_valid():
email = form.cleaned_data['email']
password = form.cleaned_data['password']
- username = User.objects.get(email__iexact=email)
+ username = User.objects.get(email__iexact=email).username
- user = authenticate(username, password)
+ user = authenticate(username=username, password=password)
login(request, user)
return redirect('/reg')
else:
return render_to_response('login.html', {'form':form})
else:
- form = LoginForm()
+ form = reg_forms.LoginForm()
return render_to_response('login.html', {'form':form})
+
+def user_logout(request):
+ """ simply logout the user and redirect to homepage.
+ """
+
+ logout(request)
+ print "logged out"
+ return redirect('/reg')
+
+
--- a/urls.py Fri Apr 09 11:46:35 2010 +0530
+++ b/urls.py Fri Apr 09 12:28:58 2010 +0530
@@ -16,6 +16,6 @@
(r'^admin/', include(admin.site.urls)),
(r'^reg/', include('workshop.reg.site.urls')),
- (r'^quiz/', include('workshop.quiz.site.urls')),
- (r'^feedback/', include('workshop.feedback.site.urls')),
+ #(r'^quiz/', include('workshop.quiz.site.urls')),
+ #(r'^feedback/', include('workshop.feedback.site.urls')),
)