ce68872cc594381792816b56ce318d4b40ef781b
[cascardo/ema.git] / eventos / views.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2008 Lincoln de Sousa <lincoln@minaslivre.org>
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 2 of the
7 # License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public
15 # License along with this program; if not, write to the
16 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 # Boston, MA 02111-1307, USA.
18 from django.http import HttpResponseRedirect
19 from django.contrib import auth
20 from django.contrib.auth.forms import AuthenticationForm
21
22 def login(request):
23     """This is a function that will be used as a front-end to the
24     django's login system. It receives username and password fields
25     from a POST request and tries to login the user.
26
27     If login is successful, user will be redirected to the referer
28     address, otherwise will be redirected to /?login_failed.
29     """
30     errors = {}
31     manipulator = AuthenticationForm(request)
32     if request.POST:
33         errors = manipulator.get_validation_errors(request.POST)
34         got_user = manipulator.get_user()
35         if got_user:
36             auth.login(request, got_user)
37             try:
38                 request.session.delete_test_cookie()
39             except KeyError:
40                 pass
41             return HttpResponseRedirect('/')
42         else:
43             return HttpResponseRedirect('/?login_failed')
44
45     request.session.set_test_cookie()
46     return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
47
48 def logout(request):
49     """Simple front-end to django's logout stuff. This function should
50     be mapped to an url and simply called without any parameter.
51     """
52     auth.logout(request)
53     return HttpResponseRedirect('/login')