X-Git-Url: http://git.cascardo.info/?p=cascardo%2Fema.git;a=blobdiff_plain;f=eventos%2Fviews.py;h=ce68872cc594381792816b56ce318d4b40ef781b;hp=60f00ef0ef347811e7b0c0921b7fda097acd9fcc;hb=63776084d2bbb2ca8d68a45d6638893c1425029c;hpb=bcfecc088b96da943a969e5aa6e447425102773d 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')