From: Lincoln de Sousa Date: Tue, 1 Jul 2008 01:45:09 +0000 (-0300) Subject: adding login/logout stuff X-Git-Tag: 200807081126~11 X-Git-Url: http://git.cascardo.info/?p=cascardo%2Fema.git;a=commitdiff_plain;h=63776084d2bbb2ca8d68a45d6638893c1425029c adding login/logout stuff --- diff --git a/eventos/urls.py b/eventos/urls.py new file mode 100644 index 0000000..ba07a72 --- /dev/null +++ b/eventos/urls.py @@ -0,0 +1,6 @@ +from django.conf.urls.defaults import * + +urlpatterns = patterns('', + (r'^login/', 'eventos.views.login'), + (r'^logout/', 'eventos.views.logout'), +) diff --git a/eventos/views.py b/eventos/views.py index 60f00ef..ce68872 100644 --- a/eventos/views.py +++ b/eventos/views.py @@ -1 +1,53 @@ -# Create your views here. +# -*- coding: utf-8 -*- +# Copyright (C) 2008 Lincoln de Sousa +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 2 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public +# License along with this program; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. +from django.http import HttpResponseRedirect +from django.contrib import auth +from django.contrib.auth.forms import AuthenticationForm + +def login(request): + """This is a function that will be used as a front-end to the + django's login system. It receives username and password fields + from a POST request and tries to login the user. + + If login is successful, user will be redirected to the referer + address, otherwise will be redirected to /?login_failed. + """ + errors = {} + manipulator = AuthenticationForm(request) + if request.POST: + errors = manipulator.get_validation_errors(request.POST) + got_user = manipulator.get_user() + if got_user: + auth.login(request, got_user) + try: + request.session.delete_test_cookie() + except KeyError: + pass + return HttpResponseRedirect('/') + else: + return HttpResponseRedirect('/?login_failed') + + request.session.set_test_cookie() + return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/')) + +def logout(request): + """Simple front-end to django's logout stuff. This function should + be mapped to an url and simply called without any parameter. + """ + auth.logout(request) + return HttpResponseRedirect('/login') diff --git a/urls.py b/urls.py index 572767b..7c3de47 100644 --- a/urls.py +++ b/urls.py @@ -2,5 +2,6 @@ from django.conf.urls.defaults import * urlpatterns = patterns('', (r'^admin/', include('django.contrib.admin.urls')), + (r'^', include('ema.eventos.urls')), (r'^', include('diario.urls.entries')), )