comparison pylons_app/model/forms.py @ 383:ebdd1a89cdd9

Added extra validation in creating users. new style errors for users
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 24 Jul 2010 00:46:29 +0200
parents b0715a788432
children 98abf8953b87
comparison
equal deleted inserted replaced
382:b0715a788432 383:ebdd1a89cdd9
50 def validate_python(self, value, state): 50 def validate_python(self, value, state):
51 51
52 if value != authentication_token(): 52 if value != authentication_token():
53 raise formencode.Invalid(self.message('invalid_token', state, 53 raise formencode.Invalid(self.message('invalid_token', state,
54 search_number=value), value, state) 54 search_number=value), value, state)
55 class ValidUsername(formencode.validators.FancyValidator): 55
56 56 def ValidUsername(edit, old_data):
57 def validate_python(self, value, state): 57 class _ValidUsername(formencode.validators.FancyValidator):
58 if value in ['default', 'new_user']: 58
59 raise formencode.Invalid(_('Invalid username'), value, state) 59 def validate_python(self, value, state):
60 if value in ['default', 'new_user']:
61 raise formencode.Invalid(_('Invalid username'), value, state)
62 #check if user is uniq
63 sa = meta.Session
64 old_un = None
65 if edit:
66 old_un = sa.query(User).get(old_data.get('user_id')).username
67
68 if old_un != value or not edit:
69 if sa.query(User).filter(User.username == value).scalar():
70 raise formencode.Invalid(_('This username already exists') ,
71 value, state)
72 meta.Session.remove()
73
74 return _ValidUsername
60 75
61 class ValidPassword(formencode.validators.FancyValidator): 76 class ValidPassword(formencode.validators.FancyValidator):
62 77
63 def to_python(self, value, state): 78 def to_python(self, value, state):
64 if value: 79 if value:
231 246
232 247
233 #chained validators have access to all data 248 #chained validators have access to all data
234 chained_validators = [ValidAuth] 249 chained_validators = [ValidAuth]
235 250
236 def UserForm(edit=False): 251 def UserForm(edit=False, old_data={}):
237 class _UserForm(formencode.Schema): 252 class _UserForm(formencode.Schema):
238 allow_extra_fields = True 253 allow_extra_fields = True
239 filter_extra_fields = True 254 filter_extra_fields = True
240 username = All(UnicodeString(strip=True, min=3, not_empty=True), ValidUsername) 255 username = All(UnicodeString(strip=True, min=3, not_empty=True), ValidUsername(edit, old_data))
241 if edit: 256 if edit:
242 new_password = All(UnicodeString(strip=True, min=3, not_empty=False), ValidPassword) 257 new_password = All(UnicodeString(strip=True, min=3, not_empty=False), ValidPassword)
243 admin = StringBoolean(if_missing=False) 258 admin = StringBoolean(if_missing=False)
244 else: 259 else:
245 password = All(UnicodeString(strip=True, min=3, not_empty=False), ValidPassword) 260 password = All(UnicodeString(strip=True, min=8, not_empty=True), ValidPassword)
246 active = StringBoolean(if_missing=False) 261 active = StringBoolean(if_missing=False)
247 name = UnicodeString(strip=True, min=3, not_empty=True) 262 name = UnicodeString(strip=True, min=3, not_empty=True)
248 lastname = UnicodeString(strip=True, min=3, not_empty=True) 263 lastname = UnicodeString(strip=True, min=3, not_empty=True)
249 email = Email(not_empty=True) 264 email = Email(not_empty=True)
250 265