added views, templates for register, login, logout user.
authoranoop
Thu, 04 Feb 2010 22:14:34 +0530
changeset 13 0fb64b24a1c9
parent 12 a93eebabfeb1
child 14 54694601c5e3
added views, templates for register, login, logout user.
taskapp/forms/user.py
taskapp/views/users.py
templates/user/login.html
templates/user/register.html
urls.py
--- a/taskapp/forms/user.py	Thu Feb 04 22:12:48 2010 +0530
+++ b/taskapp/forms/user.py	Thu Feb 04 22:14:34 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/taskapp/views/users.py	Thu Feb 04 22:12:48 2010 +0530
+++ b/taskapp/views/users.py	Thu Feb 04 22:14:34 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/templates/user/login.html	Thu Feb 04 22:14:34 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/templates/user/register.html	Thu Feb 04 22:14:34 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/urls.py	Thu Feb 04 22:12:48 2010 +0530
+++ b/urls.py	Thu Feb 04 22:14:34 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)
 )