annotate pylons_app/controllers/login.py @ 360:db187729c40c

new style error display for login
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 24 Jul 2010 00:53:03 +0200
parents 3782a6d698af
children 98abf8953b87
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
252
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
1 #!/usr/bin/env python
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
2 # encoding: utf-8
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
3 # login controller for pylons
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
360
db187729c40c new style error display for login
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
5 #
252
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
6 # This program is free software; you can redistribute it and/or
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
7 # modify it under the terms of the GNU General Public License
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
8 # as published by the Free Software Foundation; version 2
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
9 # of the License or (at your opinion) any later version of the license.
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
10 #
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
11 # This program is distributed in the hope that it will be useful,
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
14 # GNU General Public License for more details.
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
15 #
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
16 # You should have received a copy of the GNU General Public License
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
17 # along with this program; if not, write to the Free Software
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
19 # MA 02110-1301, USA.
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
20 """
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
21 Created on April 22, 2010
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
22 login controller for pylons
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
23 @author: marcink
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 202
diff changeset
24 """
186
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
25 import logging
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
26 from formencode import htmlfill
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
27 from pylons import request, response, session, tmpl_context as c, url
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
28 from pylons.controllers.util import abort, redirect
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
29 from pylons_app.lib.base import BaseController, render
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 import formencode
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31 from pylons_app.model.forms import LoginForm
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
32 from pylons_app.lib.auth import AuthUser
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
33
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34 log = logging.getLogger(__name__)
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
35
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
36 class LoginController(BaseController):
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37
202
3fd2af1ba5ea updated logging in logout. Added before, on login page.
Marcin Kuzminski <marcin@python-works.com>
parents: 195
diff changeset
38 def __before__(self):
3fd2af1ba5ea updated logging in logout. Added before, on login page.
Marcin Kuzminski <marcin@python-works.com>
parents: 195
diff changeset
39 super(LoginController, self).__before__()
3fd2af1ba5ea updated logging in logout. Added before, on login page.
Marcin Kuzminski <marcin@python-works.com>
parents: 195
diff changeset
40
186
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41 def index(self):
195
7109d15c6813 cleared prints leftoovers, and changed current user fetching in login controller
Marcin Kuzminski <marcin@python-works.com>
parents: 186
diff changeset
42 #redirect if already logged in
7109d15c6813 cleared prints leftoovers, and changed current user fetching in login controller
Marcin Kuzminski <marcin@python-works.com>
parents: 186
diff changeset
43 if c.hg_app_user.is_authenticated:
186
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
44 return redirect(url('hg_home'))
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
45
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
46 if request.POST:
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
47 #import Login Form validator class
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48 login_form = LoginForm()
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49 try:
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50 c.form_result = login_form.to_python(dict(request.POST))
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 return redirect(url('hg_home'))
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
52
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53 except formencode.Invalid as errors:
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54 return htmlfill.render(
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55 render('/login.html'),
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56 defaults=errors.value,
360
db187729c40c new style error display for login
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
57 errors=errors.error_dict or {},
db187729c40c new style error display for login
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
58 prefix_error=False,
186
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59 encoding="UTF-8")
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
60
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61 return render('/login.html')
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63 def logout(self):
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64 session['hg_app_user'] = AuthUser()
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65 session.save()
202
3fd2af1ba5ea updated logging in logout. Added before, on login page.
Marcin Kuzminski <marcin@python-works.com>
parents: 195
diff changeset
66 log.info('Logging out and setting user as Empty')
186
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67 redirect(url('hg_home'))