Last active
September 27, 2015 03:08
-
-
Save d9k/cfec6bd7d405f4543ec0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# schema | |
class LoginSchema(Schema): | |
login = SchemaNode( | |
String(), | |
title='Login', | |
widget=TextInputWidget(), | |
) | |
password = SchemaNode( | |
String(), | |
title='Password', | |
widget=PasswordWidget(), | |
) | |
# sub function | |
def exception_for_form_field(form, field_name, text): | |
exc = colander.Invalid(form) | |
""":type : SchemaNode""" | |
field = form.get(field_name) | |
exc.add(colander.Invalid(field, text), field._order) | |
return exc | |
# validator | |
def validate_auth(form, values): | |
nonlocal authed_user | |
login = values.get('login') | |
password = values.get('password') | |
user = User.by_any(login) | |
if not user: | |
raise exception_for_form_field(form, 'login', 'User not found') | |
if not user.check_password(password): | |
raise exception_for_form_field(form, 'password', 'Wrong password') | |
authed_user = user | |
# form creation | |
login_form = Form( | |
LoginSchema(validator=validate_auth), | |
buttons=[Button(name='login_form_submit', title='Enter')] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment