22
22
from genshi.filters import HTMLFormFiller
24
24
from ivle.webapp import ApplicationRoot
25
from ivle.webapp.base.forms import BaseFormView
25
from ivle.webapp.base.rest import JSONRESTView, require_permission
26
26
from ivle.webapp.base.xhtml import XHTMLView
27
27
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
28
28
from ivle.webapp.admin.publishing import root_to_user, user_url
29
29
from ivle.database import User
33
35
class UsersView(XHTMLView):
44
46
ctx['users'] = req.store.find(User).order_by(User.login)
49
# List of fields returned as part of the user JSON dictionary
50
# (as returned by the get_user action)
52
"login", "state", "unixid", "email", "nick", "fullname",
53
"admin", "studentid", "acct_exp", "pass_exp", "last_login",
57
class UserRESTView(JSONRESTView):
59
A REST interface to the user object.
62
@require_permission('view')
65
user = ivle.util.object_to_dict(user_fields_list, self.context)
66
# Convert time stamps to nice strings
67
for k in 'pass_exp', 'acct_exp', 'last_login':
68
if user[k] is not None:
69
user[k] = unicode(user[k])
71
user['local_password'] = self.context.passhash is not None
47
74
class UserEditSchema(formencode.Schema):
48
75
nick = formencode.validators.UnicodeString(not_empty=True)
49
76
email = formencode.validators.Email(not_empty=False,
52
class UserEditView(BaseFormView):
79
class UserEditView(XHTMLView):
53
80
"""A form to change a user's details."""
54
81
template = 'templates/user-edit.html'
56
83
permission = 'edit'
60
return UserEditSchema()
62
def get_default_data(self, req):
63
return {'nick': self.context.nick,
64
'email': self.context.email
67
def save_object(self, req, data):
68
self.context.nick = data['nick']
69
self.context.email = unicode(data['email']) if data['email'] \
85
def filter(self, stream, ctx):
86
return stream | HTMLFormFiller(data=ctx['data'])
73
88
def populate(self, req, ctx):
74
super(UserEditView, self).populate(req, ctx)
89
if req.method == 'POST':
90
data = dict(req.get_fieldstorage())
92
validator = UserEditSchema()
93
data = validator.to_python(data, state=req)
94
self.context.nick = data['nick']
95
self.context.email = unicode(data['email']) if data['email'] \
98
req.throw_redirect(req.uri)
99
except formencode.Invalid, e:
100
errors = e.unpack_errors()
102
data = {'nick': self.context.nick,
103
'email': self.context.email
75
107
ctx['format_datetime'] = ivle.date.make_date_nice
76
108
ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
111
ctx['user'] = self.context
113
ctx['errors'] = errors
79
115
class UserAdminSchema(formencode.Schema):
80
116
admin = formencode.validators.StringBoolean(if_missing=False)
93
129
"""Only allow access if the requesting user is an admin."""
94
130
return req.user and req.user.admin
98
return UserAdminSchema()
100
def get_default_data(self, req):
101
return {'admin': self.context.admin,
102
'disabled': self.context.state == u'disabled',
103
'fullname': self.context.fullname,
104
'studentid': self.context.studentid,
107
def save_object(self, req, data):
108
if self.context is req.user:
109
# Admin checkbox is disabled -- assume unchanged
110
data['admin'] = self.context.admin
111
data['disabled'] = self.context.state == u'disabled'
132
def filter(self, stream, ctx):
133
return stream | HTMLFormFiller(data=ctx['data'])
135
def populate(self, req, ctx):
136
if req.method == 'POST':
137
data = dict(req.get_fieldstorage())
139
validator = UserAdminSchema()
140
data = validator.to_python(data, state=req)
142
if self.context is req.user:
143
# Admin checkbox is disabled -- assume unchanged
144
data['admin'] = self.context.admin
145
data['disabled'] = self.context.state == u'disabled'
147
self.context.admin = data['admin']
148
if self.context.state in (u'enabled', u'disabled'):
149
self.context.state = (u'disabled' if data['disabled']
151
self.context.fullname = data['fullname'] \
152
if data['fullname'] else None
153
self.context.studentid = data['studentid'] \
154
if data['studentid'] else None
156
req.throw_redirect(req.uri)
157
except formencode.Invalid, e:
158
errors = e.unpack_errors()
113
self.context.admin = data['admin']
114
if self.context.state in (u'enabled', u'disabled'):
115
self.context.state = (u'disabled' if data['disabled']
117
self.context.fullname = data['fullname'] \
118
if data['fullname'] else None
119
self.context.studentid = data['studentid'] \
120
if data['studentid'] else None
123
def populate(self, req, ctx):
124
super(UserAdminView, self).populate(req, ctx)
126
# Disable the admin checkbox if editing oneself
160
data = {'admin': self.context.admin,
161
'disabled': self.context.state == u'disabled',
162
'fullname': self.context.fullname,
163
'studentid': self.context.studentid,
168
ctx['user'] = self.context
169
# Disable the Admin checkbox if editing oneself
127
170
ctx['disable_admin'] = self.context is req.user
172
ctx['errors'] = errors
129
174
class PasswordChangeView(XHTMLView):
130
175
"""A form to change a user's password, with knowledge of the old one."""
193
238
forward_routes = (root_to_user,)
194
239
reverse_routes = (user_url,)
195
240
views = [(ApplicationRoot, 'users', UsersView),
196
(User, '+index', UserEditView),
197
(User, '+admin', UserAdminView),
198
(User, '+changepassword', PasswordChangeView),
199
(User, '+resetpassword', PasswordResetView),
241
(ivle.database.User, '+index', UserEditView),
242
(ivle.database.User, '+admin', UserAdminView),
243
(ivle.database.User, '+changepassword', PasswordChangeView),
244
(ivle.database.User, '+resetpassword', PasswordResetView),
245
(ivle.database.User, '+index', UserRESTView, 'api'),