1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2009 The University of Melbourne
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
# Author: Matt Giuca, Will Grant
20
from ivle.webapp.base.rest import JSONRESTView
21
from ivle.webapp.base.xhtml import XHTMLView
22
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
23
from ivle.webapp.errors import NotFound, Unauthorized
27
# List of fields returned as part of the user JSON dictionary
28
# (as returned by the get_user action)
30
"login", "state", "unixid", "email", "nick", "fullname",
31
"rolenm", "studentid", "acct_exp", "pass_exp", "last_login",
35
class UserRESTView(JSONRESTView):
37
A REST interface to the user object.
39
def __init__(self, req, login):
40
super(UserRESTView, self).__init__(self, req, login)
41
self.context = ivle.database.User.get_by_login(req.store, login)
42
if self.context is None:
47
user = ivle.util.object_to_dict(user_fields_list, self.context)
48
# Convert time stamps to nice strings
49
for k in 'pass_exp', 'acct_exp', 'last_login':
50
if user[k] is not None:
51
user[k] = unicode(user[k])
53
user['local_password'] = self.context.passhash is not None
56
def PATCH(self, req, data):
58
# XXX Admins can set extra fields
59
# Note: Cannot change password here (use change_password named op)
61
for f in user_fields_list:
64
if isinstance(field, str):
65
field = unicode(field)
66
setattr(self.context, f, field)
70
class UserSettingsView(XHTMLView):
71
template = 'user-settings.html'
74
def __init__(self, req, login):
75
self.context = ivle.database.User.get_by_login(req.store, login)
76
if self.context is None:
79
if req.user is None or (req.user is not self.context and
80
req.user.rolenm != 'admin'):
83
def populate(self, req, ctx):
84
self.plugin_scripts[Plugin] = ['settings.js']
85
req.scripts_init = ['revert_settings']
87
ctx['login'] = self.context.login
89
class Plugin(ViewPlugin, MediaPlugin):
91
The Plugin class for the user plugin.
93
# Magic attribute: urls
94
# Sequence of pairs/triples of
95
# (regex str, handler class, kwargs dict)
96
# The kwargs dict is passed to the __init__ of the view object
98
('~:login/+settings', UserSettingsView),
99
('api/~:login', UserRESTView),