- Programacao dinamica
[cascardo/eventmanager.git] / decorators.py
1 """
2 Copyright (C) 2007 Lincoln de Sousa <lincoln@archlinux-br.org>
3 Copyright (C) 2007 Douglas Andrade <douglas@archlinux-br.org>
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with this program; if not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19
20     Changes:
21
22     * Lincoln de Sousa (22 Jul 2007) adding is_login_form here.
23
24 """
25 from django.contrib.auth.forms import AuthenticationForm
26 from django.contrib.auth import login
27 from django.http import HttpResponseRedirect
28
29 def is_login_form(d):
30     return 'submitting_login_form' in d
31
32 def enable_login_form(func):
33     def _login(request, *args, **kwargs):
34         errors = {}
35         manipulator = AuthenticationForm(request)
36         # hammer test to make sure that the *login* form is being sent
37         if request.POST and is_login_form(request.POST):
38             errors = manipulator.get_validation_errors(request.POST)
39             got_user = manipulator.get_user()
40             if got_user:
41                 login(request, got_user)
42                 try:
43                     request.session.delete_test_cookie()
44                 except KeyError:
45                     pass
46                 return HttpResponseRedirect('/')
47             else:
48                 return HttpResponseRedirect('/?login_failed')
49
50         request.session.set_test_cookie()
51         return func(request, *args, **kwargs)
52     return _login