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