2 Calculate the current state of the books
7 from bluechips.lib.base import *
8 from bluechips.lib.permissions import BlueChipResident
11 from sqlalchemy import orm
13 from authkit.authorize.pylons_adaptors import authorize
15 from pylons import request
16 from pylons.decorators import validate
17 from pylons.decorators.secure import authenticate_form
19 from formencode import validators, Schema, FancyValidator, Invalid
21 log = logging.getLogger(__name__)
24 class EmailSchema(Schema):
25 "Validate email updates."
26 allow_extra_fields = False
27 new_email = validators.Email()
30 class UniqueUsername(FancyValidator):
31 def _to_python(self, value, state):
32 u = meta.Session.query(model.User).\
33 filter(model.User.username == value).\
37 'That username already exists',
42 class NewUserSchema(Schema):
44 allow_extra_fields = False
45 username = UniqueUsername(not_empty=True)
46 password = validators.String(if_missing=None)
47 confirm_password = validators.String(if_missing=None)
48 name = validators.String(not_empty=False)
49 resident = validators.StringBoolean(not_empty=True)
50 chained_validators = [
51 validators.FieldsMatch('password', 'confirm_password'),
55 class UserController(BaseController):
57 c.title = 'User Settings'
58 return render('/user/index.mako')
61 c.title = 'User Settings'
62 return render('/user/email.mako')
65 @validate(schema=EmailSchema(), form='index')
67 new_email = self.form_result['new_email']
68 request.environ['user'].email = new_email
71 h.flash("Removed email address.")
73 h.flash("Updated email address to '%s'." % new_email)
74 return h.redirect_to('/')
76 @authorize(BlueChipResident())
78 c.title = 'Register a New User'
79 return render('/user/new.mako')
82 @authorize(BlueChipResident())
83 @validate(schema=NewUserSchema(), form='new')
85 u = model.User(username=self.form_result['username'],
86 resident=self.form_result['resident'])
88 if self.form_result['name']:
89 u.name = self.form_result['name']
91 u.name = self.form_result['username']
93 if self.form_result['password'] is not None:
94 u.password = self.form_result['password']
99 h.flash('Successfully created new user %s' % u.username)
100 return h.redirect_to('/')