]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/controllers/user.py
Add a new /user action with pointers to the others.
[bluechips.git] / bluechips / controllers / user.py
1 """
2 Calculate the current state of the books
3 """
4
5 import logging
6
7 from bluechips.lib.base import *
8
9 import sqlalchemy
10 from sqlalchemy import orm
11
12 from pylons import request
13 from pylons.decorators import validate
14 from pylons.decorators.secure import authenticate_form
15
16 from formencode import validators, Schema, FancyValidator, Invalid
17
18 log = logging.getLogger(__name__)
19
20
21 class EmailSchema(Schema):
22     "Validate email updates."
23     allow_extra_fields = False
24     new_email = validators.Email()
25
26
27 class UniqueUsername(FancyValidator):
28     def _to_python(self, value, state):
29         u = meta.Session.query(model.User).\
30             filter(model.User.username == value).\
31             first()
32         if u:
33             raise Invalid(
34                 'That username already exists',
35                 value, state)
36         return value
37
38
39 class NewUserSchema(Schema):
40     "Validate new users."
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'),
49         ]
50
51
52 class UserController(BaseController):
53     def index(self):
54         c.title = 'User Settings'
55         return render('/user/index.mako')
56
57     def email(self):
58         c.title = 'User Settings'
59         return render('/user/email.mako')
60
61     @authenticate_form
62     @validate(schema=EmailSchema(), form='index')
63     def update(self):
64         new_email = self.form_result['new_email']
65         request.environ['user'].email = new_email
66         meta.Session.commit()
67         if new_email is None:
68             h.flash("Removed email address.")
69         else:
70             h.flash("Updated email address to '%s'." % new_email)
71         return h.redirect_to('/')
72
73     def new(self):
74         c.title = 'Register a New User'
75         return render('/user/new.mako')
76
77     @authenticate_form
78     @validate(schema=NewUserSchema(), form='new')
79     def create(self):
80         u = model.User(username=self.form_result['username'],
81                        resident=self.form_result['resident'])
82
83         if self.form_result['name']:
84             u.name = self.form_result['name']
85         else:
86             u.name = self.form_result['username']
87
88         if self.form_result['password'] is not None:
89             u.password = self.form_result['password']
90
91         meta.Session.save(u)
92         meta.Session.commit()
93
94         h.flash('Successfully created new user %s' % u.username)
95         return h.redirect_to('/')