# HG changeset patch # User anoop # Date 1264781233 -19800 # Node ID 94136f9a48bc31c843c1c28da7cc1e1b217b56c7 # Parent aea7e764c033284eb258ff29882922f318970ca1 added login, logout, and register. diff -r aea7e764c033 -r 94136f9a48bc pytask/taskapp/forms/user.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) diff -r aea7e764c033 -r 94136f9a48bc pytask/taskapp/views/users.py --- 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!!!') diff -r aea7e764c033 -r 94136f9a48bc pytask/templates/user/login.html --- /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 %} +
+{% endblock %} diff -r aea7e764c033 -r 94136f9a48bc pytask/templates/user/register.html --- /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 %} + +{% endblock %} diff -r aea7e764c033 -r 94136f9a48bc pytask/urls.py --- 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) )