added login, logout, and register.
authoranoop
Fri, 29 Jan 2010 21:37:13 +0530
changeset 6 94136f9a48bc
parent 5 aea7e764c033
child 7 232d40a28118
added login, logout, and register.
pytask/taskapp/forms/user.py
pytask/taskapp/views/users.py
pytask/templates/user/login.html
pytask/templates/user/register.html
pytask/urls.py
--- a/pytask/taskapp/forms/user.py	Fri Jan 29 19:27:26 2010 +0530
+++ b/pytask/taskapp/forms/user.py	Fri Jan 29 21:37:13 2010 +0530
@@ -0,0 +1,16 @@
+#!/usr/bin/python2.5
+
+from django import forms
+from pytask.taskapp.models import GENDER_CHOICES
+
+class RegistrationForm(forms.Form):
+    username = forms.CharField(max_length=30, required=True)
+    password = forms.CharField(max_length=60, required=True, widget=forms.PasswordInput)
+    repeat_password = forms.CharField(max_length=60, required=True, widget=forms.PasswordInput)
+    email = forms.EmailField(max_length=30, required=True)
+    gender = forms.ChoiceField(choices=GENDER_CHOICES, required = True)
+    dob = forms.DateField(required=True, help_text = "(YYYY-MM-DD)")
+
+class LoginForm(forms.Form):
+    username = forms.CharField(max_length=30, required=True)
+    password = forms.CharField(max_length=60, required=True, widget=forms.PasswordInput)
--- a/pytask/taskapp/views/users.py	Fri Jan 29 19:27:26 2010 +0530
+++ b/pytask/taskapp/views/users.py	Fri Jan 29 21:37:13 2010 +0530
@@ -1,6 +1,10 @@
 from django.http import HttpResponse
 from django.shortcuts import redirect, render_to_response
 from pytask.taskapp.models import Task
+from pytask.taskapp.forms.user import RegistrationForm, LoginForm
+from pytask.taskapp.events.user import createUser
+from django.contrib.auth import login, logout, authenticate
+from django.contrib.auth.models import User
 
 def redirect_to_homepage(request):
     """ simply redirect to homepage """
@@ -37,5 +41,52 @@
                }
                
     return render_to_response('index.html', context)
-               
+
 
+def register(request):
+    """ user registration: gets the user details and create the user and userprofile if data entered is valid"""
+    if request.method == 'POST':
+        form = RegistrationForm(request.POST)
+        if form.is_valid():
+            data = form.cleaned_data
+            if data['password'] == data['repeat_password']:
+                try:
+                    if User.objects.get(username__exact = data['username']):
+                        errors=['Choose some other username']
+                        return render_to_response('user/register.html',{'form':form,'errors':errors})
+                except:
+                     u = createUser(username=data['username'], email=data['email'], password=data['password'],dob = data['dob'],gender = data['gender'])
+                return redirect('/accounts/login/')
+            else:
+                errors=['Password do not match']
+                form = RegistrationForm(request.POST)
+                return render_to_response('user/register.html',{'form':form,'errors':errors})#HttpResponse('Password did not match')
+        else:
+            form = RegistrationForm()
+    else:
+        form = RegistrationForm()
+    return render_to_response('user/register.html', {'form': form})
+
+def user_login(request):
+    if request.method == 'POST':
+        username = request.POST['username']
+        password = request.POST['password']
+        user = authenticate(username=username, password=password)
+        if user is not None:
+            if user.is_active:
+                login(request, user)
+                return redirect('/')# Redirect to a success page.
+            else:
+                return HttpResponse('username is not active, please contact the administrator')# Return a 'disabled account' error message
+        else:
+            errors = ['Please check your username and password']
+            form = LoginForm()
+            return render_to_response('user/login.html',{'form':form,'errors':errors})# Return an 'invalid login' error message.
+        return redirect('/')
+    else:
+        form = LoginForm()
+        return render_to_response('user/login.html',{'form': form})
+
+def user_logout(request):
+    logout(request)
+    return HttpResponse('You have logged off successfully!!!')
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pytask/templates/user/login.html	Fri Jan 29 21:37:13 2010 +0530
@@ -0,0 +1,17 @@
+{% extends 'base.html' %}
+{% block content %}
+<form action="/accounts/login/" method="post">
+    {{ form.as_p }}
+
+{% if errors %}
+    <p style="color: red;">
+        Please correct the error{{ errors|pluralize }} below.
+        {% for i in errors %}
+            <br>{{ i }}
+        {% endfor %}
+    </p>
+{% endif %}
+
+<input type="submit" value="Submit" />
+</form>
+{% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pytask/templates/user/register.html	Fri Jan 29 21:37:13 2010 +0530
@@ -0,0 +1,17 @@
+{% extends 'base.html' %}
+{% block content %}
+<form action="/accounts/register/" method="post">
+    {{ form.as_p }}
+
+{% if errors %}
+    <p style="color: red;">
+        Please correct the error{{ errors|pluralize }} below.
+        {% for i in errors %}
+            <br>{{ i }}
+        {% endfor %}
+    </p>
+{% endif %}
+
+<input type="submit" value="Submit" />
+</form>
+{% endblock %}
--- a/pytask/urls.py	Fri Jan 29 19:27:26 2010 +0530
+++ b/pytask/urls.py	Fri Jan 29 21:37:13 2010 +0530
@@ -4,7 +4,7 @@
 from django.contrib import admin
 admin.autodiscover()
 
-from pytask.taskapp.views.users import redirect_to_homepage, homepage
+from pytask.taskapp.views.users import redirect_to_homepage, homepage, register, user_login, user_logout
 from pytask.taskapp.views.tasks import browse_tasks, view_task
 
 urlpatterns = patterns('',
@@ -20,5 +20,8 @@
     (r'^task/view/tid=(\d+)', view_task),
     
     (r'^admin/', include(admin.site.urls)),
-
+    
+    (r'^accounts/register/$',register),
+    (r'^accounts/login/$',user_login),
+    (r'^accounts/logout/$',user_logout)
 )