Mercurial > kallithea
comparison pylons_app/model/forms.py @ 238:a55c17874486
Rewrite of user managment, improved forms, added some user info
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sun, 30 May 2010 17:55:56 +0200 |
parents | a0116e944da1 |
children | 5da4ef115006 |
comparison
equal
deleted
inserted
replaced
237:f188b156603d | 238:a55c17874486 |
---|---|
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 Email, Bool, StringBoolean | |
24 from formencode import All | |
23 from pylons import session | 25 from pylons import session |
24 from pylons.i18n.translation import _ | 26 from pylons.i18n.translation import _ |
25 from pylons_app.lib.auth import get_crypt_password | 27 from pylons_app.lib.auth import get_crypt_password |
26 from pylons_app.model import meta | 28 from pylons_app.model import meta |
27 from pylons_app.model.db import User | 29 from pylons_app.model.db import User |
46 def validate_python(self, value, state): | 48 def validate_python(self, value, state): |
47 | 49 |
48 if value != authentication_token(): | 50 if value != authentication_token(): |
49 raise formencode.Invalid(self.message('invalid_token', state, | 51 raise formencode.Invalid(self.message('invalid_token', state, |
50 search_number=value), value, state) | 52 search_number=value), value, state) |
53 class ValidUsername(formencode.validators.FancyValidator): | |
51 | 54 |
55 def validate_python(self, value, state): | |
56 pass | |
57 | |
58 class ValidPassword(formencode.validators.FancyValidator): | |
59 | |
60 def to_python(self, value, state): | |
61 return get_crypt_password(value) | |
62 | |
52 class ValidAuth(formencode.validators.FancyValidator): | 63 class ValidAuth(formencode.validators.FancyValidator): |
53 messages = { | 64 messages = { |
54 'invalid_password':_('invalid password'), | 65 'invalid_password':_('invalid password'), |
55 'invalid_login':_('invalid user name'), | 66 'invalid_login':_('invalid user name'), |
56 'disabled_account':_('Your acccount is disabled') | 67 'disabled_account':_('Your acccount is disabled') |
68 try: | 79 try: |
69 user = sa.query(User).filter(User.username == username).one() | 80 user = sa.query(User).filter(User.username == username).one() |
70 except (NoResultFound, MultipleResultsFound, OperationalError) as e: | 81 except (NoResultFound, MultipleResultsFound, OperationalError) as e: |
71 log.error(e) | 82 log.error(e) |
72 user = None | 83 user = None |
84 raise formencode.Invalid(self.message('invalid_password', | |
85 state=State_obj), value, state, | |
86 error_dict=self.e_dict) | |
73 if user: | 87 if user: |
74 if user.active: | 88 if user.active: |
75 if user.username == username and user.password == crypted_passwd: | 89 if user.username == username and user.password == crypted_passwd: |
76 from pylons_app.lib.auth import AuthUser | 90 from pylons_app.lib.auth import AuthUser |
77 auth_user = AuthUser() | 91 auth_user = AuthUser() |
122 | 136 |
123 | 137 |
124 #chained validators have access to all data | 138 #chained validators have access to all data |
125 chained_validators = [ValidAuth] | 139 chained_validators = [ValidAuth] |
126 | 140 |
127 | 141 def UserForm(edit=False): |
142 class _UserForm(formencode.Schema): | |
143 allow_extra_fields = True | |
144 filter_extra_fields = True | |
145 username = All(UnicodeString(strip=True, min=3, not_empty=True), ValidUsername) | |
146 if edit: | |
147 new_password = All(UnicodeString(strip=True, min=3, not_empty=False), ValidPassword) | |
148 else: | |
149 password = All(UnicodeString(strip=True, min=3, not_empty=False), ValidPassword) | |
150 active = StringBoolean(if_missing=False) | |
151 name = UnicodeString(strip=True, min=3, not_empty=True) | |
152 lastname = UnicodeString(strip=True, min=3, not_empty=True) | |
153 email = Email(not_empty=True) | |
154 | |
155 return _UserForm |