2 Calculate the current state of the books
7 from bluechips.lib.base import *
10 from sqlalchemy import orm
12 from pylons import request
13 from pylons.decorators import validate
14 from pylons.decorators.secure import authenticate_form
16 from formencode import validators, Schema, FancyValidator, Invalid
18 log = logging.getLogger(__name__)
21 class EmailSchema(Schema):
22 "Validate email updates."
23 allow_extra_fields = False
24 new_email = validators.Email()
27 class UniqueUsername(FancyValidator):
28 def _to_python(self, value, state):
29 u = meta.Session.query(model.User).\
30 filter(model.User.username == value).\
34 'That username already exists',
39 class NewUserSchema(Schema):
41 allow_extra_fields = False
42 username = UniqueUsername(not_empty=True)
43 password = validators.String(if_missing=None)
44 confirm_password = validators.String(if_missing=None)
45 name = validators.String(not_empty=False)
46 resident = validators.StringBoolean(not_empty=True)
47 chained_validators = [
48 validators.FieldsMatch('password', 'confirm_password'),
52 class UserController(BaseController):
54 c.title = 'User Settings'
55 return render('/user/index.mako')
58 c.title = 'User Settings'
59 return render('/user/email.mako')
62 @validate(schema=EmailSchema(), form='index')
64 new_email = self.form_result['new_email']
65 request.environ['user'].email = new_email
68 h.flash("Removed email address.")
70 h.flash("Updated email address to '%s'." % new_email)
71 return h.redirect_to('/')
74 c.title = 'Register a New User'
75 return render('/user/new.mako')
78 @validate(schema=NewUserSchema(), form='new')
80 u = model.User(username=self.form_result['username'],
81 resident=self.form_result['resident'])
83 if self.form_result['name']:
84 u.name = self.form_result['name']
86 u.name = self.form_result['username']
88 if self.form_result['password'] is not None:
89 u.password = self.form_result['password']
94 h.flash('Successfully created new user %s' % u.username)
95 return h.redirect_to('/')