Show More
@@ -1,125 +1,125 | |||||
1 | """ this is forms validation classes |
|
1 | """ this is forms validation classes | |
2 | http://formencode.org/module-formencode.validators.html |
|
2 | http://formencode.org/module-formencode.validators.html | |
3 | for list off all availible validators |
|
3 | for list off all availible validators | |
4 |
|
4 | |||
5 | we can create our own validators |
|
5 | we can create our own validators | |
6 |
|
6 | |||
7 | The table below outlines the options which can be used in a schema in addition to the validators themselves |
|
7 | The table below outlines the options which can be used in a schema in addition to the validators themselves | |
8 | pre_validators [] These validators will be applied before the schema |
|
8 | pre_validators [] These validators will be applied before the schema | |
9 | chained_validators [] These validators will be applied after the schema |
|
9 | chained_validators [] These validators will be applied after the schema | |
10 | allow_extra_fields False If True, then it is not an error when keys that aren't associated with a validator are present |
|
10 | allow_extra_fields False If True, then it is not an error when keys that aren't associated with a validator are present | |
11 | filter_extra_fields False If True, then keys that aren't associated with a validator are removed |
|
11 | filter_extra_fields False If True, then keys that aren't associated with a validator are removed | |
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. |
|
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. | |
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 |
|
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 | |
14 |
|
14 | |||
15 |
|
15 | |||
16 | <name> = formencode.validators.<name of validator> |
|
16 | <name> = formencode.validators.<name of validator> | |
17 | <name> must equal form name |
|
17 | <name> must equal form name | |
18 | list=[1,2,3,4,5] |
|
18 | list=[1,2,3,4,5] | |
19 | for SELECT use formencode.All(OneOf(list), Int()) |
|
19 | for SELECT use formencode.All(OneOf(list), Int()) | |
20 |
|
20 | |||
21 | """ |
|
21 | """ | |
22 | from formencode.validators import UnicodeString, OneOf, Int, Number, Regex |
|
22 | from formencode.validators import UnicodeString, OneOf, Int, Number, Regex | |
23 | from pylons import session |
|
23 | from pylons import session | |
24 | from pylons.i18n.translation import _ |
|
24 | from pylons.i18n.translation import _ | |
25 | from pylons_app.lib.auth import get_crypt_password |
|
25 | from pylons_app.lib.auth import get_crypt_password | |
26 | from pylons_app.model import meta |
|
26 | from pylons_app.model import meta | |
27 | from pylons_app.model.db import Users |
|
27 | from pylons_app.model.db import Users | |
28 | from sqlalchemy.exc import OperationalError |
|
28 | from sqlalchemy.exc import OperationalError | |
29 | from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound |
|
29 | from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound | |
30 | from webhelpers.pylonslib.secure_form import authentication_token |
|
30 | from webhelpers.pylonslib.secure_form import authentication_token | |
31 | import formencode |
|
31 | import formencode | |
32 | import logging |
|
32 | import logging | |
33 | log = logging.getLogger(__name__) |
|
33 | log = logging.getLogger(__name__) | |
34 |
|
34 | |||
35 |
|
35 | |||
36 | #this is needed to translate the messages using _() in validators |
|
36 | #this is needed to translate the messages using _() in validators | |
37 | class State_obj(object): |
|
37 | class State_obj(object): | |
38 | _ = staticmethod(_) |
|
38 | _ = staticmethod(_) | |
39 |
|
39 | |||
40 | #=============================================================================== |
|
40 | #=============================================================================== | |
41 | # VALIDATORS |
|
41 | # VALIDATORS | |
42 | #=============================================================================== |
|
42 | #=============================================================================== | |
43 | class ValidAuthToken(formencode.validators.FancyValidator): |
|
43 | class ValidAuthToken(formencode.validators.FancyValidator): | |
44 | messages = {'invalid_token':_('Token mismatch')} |
|
44 | messages = {'invalid_token':_('Token mismatch')} | |
45 |
|
45 | |||
46 | def validate_python(self, value, state): |
|
46 | def validate_python(self, value, state): | |
47 |
|
47 | |||
48 | if value != authentication_token(): |
|
48 | if value != authentication_token(): | |
49 | raise formencode.Invalid(self.message('invalid_token', state, |
|
49 | raise formencode.Invalid(self.message('invalid_token', state, | |
50 | search_number=value), value, state) |
|
50 | search_number=value), value, state) | |
51 |
|
51 | |||
52 | class ValidAuth(formencode.validators.FancyValidator): |
|
52 | class ValidAuth(formencode.validators.FancyValidator): | |
53 | messages = { |
|
53 | messages = { | |
54 | 'invalid_password':_('invalid password'), |
|
54 | 'invalid_password':_('invalid password'), | |
55 | 'invalid_login':_('invalid user name'), |
|
55 | 'invalid_login':_('invalid user name'), | |
56 | 'disabled_account':_('Your acccount is disabled') |
|
56 | 'disabled_account':_('Your acccount is disabled') | |
57 |
|
57 | |||
58 | } |
|
58 | } | |
59 | #error mapping |
|
59 | #error mapping | |
60 | e_dict = {'username':messages['invalid_login'], |
|
60 | e_dict = {'username':messages['invalid_login'], | |
61 | 'password':messages['invalid_password']} |
|
61 | 'password':messages['invalid_password']} | |
62 |
|
62 | |||
63 | def validate_python(self, value, state): |
|
63 | def validate_python(self, value, state): | |
64 | sa = meta.Session |
|
64 | sa = meta.Session | |
65 | crypted_passwd = get_crypt_password(value['password']) |
|
65 | crypted_passwd = get_crypt_password(value['password']) | |
66 | username = value['username'] |
|
66 | username = value['username'] | |
67 | try: |
|
67 | try: | |
68 | user = sa.query(Users).filter(Users.username == username).one() |
|
68 | user = sa.query(Users).filter(Users.username == username).one() | |
69 | except (NoResultFound, MultipleResultsFound, OperationalError) as e: |
|
69 | except (NoResultFound, MultipleResultsFound, OperationalError) as e: | |
70 | log.error(e) |
|
70 | log.error(e) | |
71 | user = None |
|
71 | user = None | |
72 | if user: |
|
72 | if user: | |
73 | if user.active: |
|
73 | if user.active: | |
74 | if user.username == username and user.password == crypted_passwd: |
|
74 | if user.username == username and user.password == crypted_passwd: | |
75 | log.info('user %s authenticated correctly', username) |
|
|||
76 | from pylons_app.lib.auth import AuthUser |
|
75 | from pylons_app.lib.auth import AuthUser | |
77 | auth_user = AuthUser() |
|
76 | auth_user = AuthUser() | |
78 | auth_user.username = username |
|
77 | auth_user.username = username | |
79 | auth_user.is_authenticated = True |
|
78 | auth_user.is_authenticated = True | |
80 | auth_user.is_admin = user.admin |
|
79 | auth_user.is_admin = user.admin | |
81 | session['hg_app_user'] = auth_user |
|
80 | session['hg_app_user'] = auth_user | |
82 | session.save() |
|
81 | session.save() | |
|
82 | log.info('user %s is now authenticated', username) | |||
83 | return value |
|
83 | return value | |
84 | else: |
|
84 | else: | |
85 | log.warning('user %s not authenticated', username) |
|
85 | log.warning('user %s not authenticated', username) | |
86 | raise formencode.Invalid(self.message('invalid_password', |
|
86 | raise formencode.Invalid(self.message('invalid_password', | |
87 | state=State_obj), value, state, |
|
87 | state=State_obj), value, state, | |
88 | error_dict=self.e_dict) |
|
88 | error_dict=self.e_dict) | |
89 | else: |
|
89 | else: | |
90 | log.warning('user %s is disabled', username) |
|
90 | log.warning('user %s is disabled', username) | |
91 | raise formencode.Invalid(self.message('disabled_account', |
|
91 | raise formencode.Invalid(self.message('disabled_account', | |
92 | state=State_obj), |
|
92 | state=State_obj), | |
93 | value, state, error_dict=self.e_dict) |
|
93 | value, state, error_dict=self.e_dict) | |
94 |
|
94 | |||
95 |
|
95 | |||
96 |
|
96 | |||
97 | #=============================================================================== |
|
97 | #=============================================================================== | |
98 | # FORMS |
|
98 | # FORMS | |
99 | #=============================================================================== |
|
99 | #=============================================================================== | |
100 | class LoginForm(formencode.Schema): |
|
100 | class LoginForm(formencode.Schema): | |
101 | allow_extra_fields = True |
|
101 | allow_extra_fields = True | |
102 | filter_extra_fields = True |
|
102 | filter_extra_fields = True | |
103 | username = UnicodeString( |
|
103 | username = UnicodeString( | |
104 | strip=True, |
|
104 | strip=True, | |
105 | min=3, |
|
105 | min=3, | |
106 | not_empty=True, |
|
106 | not_empty=True, | |
107 | messages={ |
|
107 | messages={ | |
108 | 'empty':_('Please enter a login'), |
|
108 | 'empty':_('Please enter a login'), | |
109 | 'tooShort':_('Enter a value %(min)i characters long or more')} |
|
109 | 'tooShort':_('Enter a value %(min)i characters long or more')} | |
110 | ) |
|
110 | ) | |
111 |
|
111 | |||
112 | password = UnicodeString( |
|
112 | password = UnicodeString( | |
113 | strip=True, |
|
113 | strip=True, | |
114 | min=3, |
|
114 | min=3, | |
115 | not_empty=True, |
|
115 | not_empty=True, | |
116 | messages={ |
|
116 | messages={ | |
117 | 'empty':_('Please enter a password'), |
|
117 | 'empty':_('Please enter a password'), | |
118 | 'tooShort':_('Enter a value %(min)i characters long or more')} |
|
118 | 'tooShort':_('Enter a value %(min)i characters long or more')} | |
119 | ) |
|
119 | ) | |
120 |
|
120 | |||
121 |
|
121 | |||
122 | #chained validators have access to all data |
|
122 | #chained validators have access to all data | |
123 | chained_validators = [ValidAuth] |
|
123 | chained_validators = [ValidAuth] | |
124 |
|
124 | |||
125 |
|
125 |
General Comments 0
You need to be logged in to leave comments.
Login now