105
105
ctx['data'] = data
106
106
ctx['errors'] = errors
108
class UserAdminSchema(formencode.Schema):
109
admin = formencode.validators.StringBoolean(if_missing=False)
110
fullname = formencode.validators.UnicodeString(not_empty=True)
111
studentid = formencode.validators.UnicodeString(not_empty=False,
115
class UserAdminView(XHTMLView):
116
"""A form for admins to change more of a user's details."""
117
template = 'templates/user-admin.html'
120
def __init__(self, req, login):
121
self.context = ivle.database.User.get_by_login(req.store, login)
122
if self.context is None:
125
def authorize(self, req):
126
"""Only allow access if the requesting user is an admin."""
127
return req.user.admin
129
def filter(self, stream, ctx):
130
return stream | HTMLFormFiller(data=ctx['data'])
132
def populate(self, req, ctx):
133
if req.method == 'POST':
134
data = dict(req.get_fieldstorage())
136
validator = UserAdminSchema()
137
data = validator.to_python(data, state=req)
139
self.context.admin = data['admin']
140
self.context.fullname = data['fullname'] \
141
if data['fullname'] else None
142
self.context.studentid = data['studentid'] \
143
if data['studentid'] else None
145
req.throw_redirect(req.uri)
146
except formencode.Invalid, e:
147
errors = e.unpack_errors()
149
data = {'admin': self.context.admin,
150
'fullname': self.context.fullname,
151
'studentid': self.context.studentid,
156
ctx['user'] = self.context
158
ctx['errors'] = errors
108
160
class PasswordChangeView(XHTMLView):
109
161
"""A form to change a user's password, with knowledge of the old one."""
110
162
template = 'templates/user-password-change.html'
184
236
# The kwargs dict is passed to the __init__ of the view object
186
238
('~:login/+edit', UserEditView),
239
('~:login/+admin', UserAdminView),
187
240
('~:login/+changepassword', PasswordChangeView),
188
241
('~:login/+resetpassword', PasswordResetView),
189
242
('api/~:login', UserRESTView),