annotate pylons_app/model/forms.py @ 195:7109d15c6813

cleared prints leftoovers, and changed current user fetching in login controller
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 22 May 2010 16:07:28 +0200
parents 556473ba0399
children 5af2cd31c99b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
1 """ this is forms validation classes
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
2 http://formencode.org/module-formencode.validators.html
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
3 for list off all availible validators
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
4
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
5 we can create our own validators
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
6
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
7 The table below outlines the options which can be used in a schema in addition to the validators themselves
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
8 pre_validators [] These validators will be applied before the schema
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
9 chained_validators [] These validators will be applied after the schema
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
10 allow_extra_fields False If True, then it is not an error when keys that aren't associated with a validator are present
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
11 filter_extra_fields False If True, then keys that aren't associated with a validator are removed
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
12 if_key_missing NoDefault If this is given, then any keys that aren't available but are expected will be replaced with this value (and then validated). This does not override a present .if_missing attribute on validators. NoDefault is a special FormEncode class to mean that no default values has been specified and therefore missing keys shouldn't take a default value.
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
13 ignore_key_missing False If True, then missing keys will be missing in the result, if the validator doesn't have .if_missing on it already
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
14
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
15
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
16 <name> = formencode.validators.<name of validator>
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
17 <name> must equal form name
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
18 list=[1,2,3,4,5]
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: 45
diff changeset
19 for SELECT use formencode.All(OneOf(list), Int())
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
20
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
21 """
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: 45
diff changeset
22 from formencode.validators import UnicodeString, OneOf, Int, Number, Regex
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
23 from pylons import session
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
24 from pylons.i18n.translation import _
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
25 from pylons_app.lib.auth import get_crypt_password
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
26 from pylons_app.model import meta
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
27 from pylons_app.model.db import Users
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
28 from sqlalchemy.exc import OperationalError
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
29 from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
30 from webhelpers.pylonslib.secure_form import authentication_token
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
31 import formencode
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: 45
diff changeset
32 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: 45
diff changeset
33 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: 45
diff changeset
34
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
35
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: 45
diff changeset
36 #this is needed to translate the messages using _() in validators
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
37 class State_obj(object):
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
38 _ = staticmethod(_)
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
39
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
40 #===============================================================================
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
41 # VALIDATORS
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
42 #===============================================================================
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
43 class ValidAuthToken(formencode.validators.FancyValidator):
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
44 messages = {'invalid_token':_('Token mismatch')}
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
45
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
46 def validate_python(self, value, state):
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
47
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
48 if value != authentication_token():
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: 45
diff changeset
49 raise formencode.Invalid(self.message('invalid_token', state,
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
50 search_number=value), value, state)
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
51
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: 45
diff changeset
52 class ValidAuth(formencode.validators.FancyValidator):
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
53 messages = {
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
54 'invalid_password':_('invalid password'),
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
55 'invalid_login':_('invalid user 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: 45
diff changeset
56 'disabled_account':_('Your acccount is disabled')
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
57
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
58 }
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
59 #error mapping
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
60 e_dict = {'username':messages['invalid_login'],
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
61 'password':messages['invalid_password']}
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
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: 45
diff changeset
63 def validate_python(self, value, state):
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
64 sa = meta.Session
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
65 crypted_passwd = get_crypt_password(value['password'])
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
66 username = value['username']
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
67 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: 45
diff changeset
68 user = sa.query(Users).filter(Users.username == username).one()
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
69 except (NoResultFound, MultipleResultsFound, OperationalError) as e:
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
70 log.error(e)
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
71 user = None
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
72 if user:
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
73 if user.active:
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
74 if user.username == username and user.password == crypted_passwd:
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
75 log.info('user %s authenticated correctly', username)
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
76 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: 45
diff changeset
77 auth_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: 45
diff changeset
78 auth_user.username = username
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
79 auth_user.is_authenticated = True
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
80 auth_user.is_admin = user.admin
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
81 session['hg_app_user'] = auth_user
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
82 session.save()
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
83 return value
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
84 else:
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
85 log.warning('user %s not authenticated', username)
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
86 raise formencode.Invalid(self.message('invalid_password',
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
87 state=State_obj), value, state,
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
88 error_dict=self.e_dict)
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
89 else:
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
90 log.warning('user %s is disabled', username)
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
91 raise formencode.Invalid(self.message('disabled_account',
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
92 state=State_obj),
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
93 value, state, error_dict=self.e_dict)
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
94
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
95
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
96
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
97 #===============================================================================
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
98 # FORMS
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
99 #===============================================================================
45
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
100 class LoginForm(formencode.Schema):
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
101 allow_extra_fields = True
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
102 filter_extra_fields = True
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
103 username = UnicodeString(
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
104 strip=True,
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
105 min=3,
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
106 not_empty=True,
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
107 messages={
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
108 'empty':_('Please enter a login'),
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
109 'tooShort':_('Enter a value %(min)i characters long or more')}
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
110 )
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
111
45
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
112 password = UnicodeString(
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
113 strip=True,
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
114 min=3,
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
115 not_empty=True,
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
116 messages={
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
117 'empty':_('Please enter a password'),
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
118 'tooShort':_('Enter a value %(min)i characters long or more')}
a886f5eba757 implemented admin page login
marcink
parents: 0
diff changeset
119 )
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
120
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
121
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: 45
diff changeset
122 #chained validators have access to all data
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
123 chained_validators = [ValidAuth]
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
124
556473ba0399 fixed menu in home page, and added login html with forms that validates username and password.
Marcin Kuzminski <marcin@python-works.com>
parents: 45
diff changeset
125