~azzar1/unity/add-show-desktop-key

1099.1.1 by Matt Giuca
Began implementing new dispatch framework (with Will Grant and Nick Chadwick).
1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2009 The University of Melbourne
3
#
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.
8
#
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.
13
#
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
17
18
# Author: Matt Giuca, Will Grant
19
1099.1.112 by William Grant
Implement authorization in JSON REST views. Add security declarations to
20
from ivle.webapp.base.rest import JSONRESTView, require_permission
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
21
from ivle.webapp.base.xhtml import XHTMLView
1099.1.99 by William Grant
Require that plugins providing media subclass MediaPlugin.
22
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
1099.1.96 by William Grant
Add an XHTMLUnauthorizedView which redirects unauthenticated users to the
23
from ivle.webapp.errors import NotFound, Unauthorized
1099.1.1 by Matt Giuca
Began implementing new dispatch framework (with Will Grant and Nick Chadwick).
24
import ivle.database
25
import ivle.util
26
27
# List of fields returned as part of the user JSON dictionary
28
# (as returned by the get_user action)
29
user_fields_list = (
30
    "login", "state", "unixid", "email", "nick", "fullname",
31
    "rolenm", "studentid", "acct_exp", "pass_exp", "last_login",
32
    "svn_pass"
33
)
34
35
class UserRESTView(JSONRESTView):
36
    """
37
    A REST interface to the user object.
38
    """
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)
1099.1.87 by William Grant
Fix views in ivle.webapp.admin and ivle.webapp.tutorial to return 404 wherever
42
        if self.context is None:
43
            raise NotFound()
1099.1.1 by Matt Giuca
Began implementing new dispatch framework (with Will Grant and Nick Chadwick).
44
1099.1.112 by William Grant
Implement authorization in JSON REST views. Add security declarations to
45
    @require_permission('view')
1099.1.1 by Matt Giuca
Began implementing new dispatch framework (with Will Grant and Nick Chadwick).
46
    def GET(self, req):
47
        # XXX Check Caps
48
        user = ivle.util.object_to_dict(user_fields_list, self.context)
49
        # Convert time stamps to nice strings
50
        for k in 'pass_exp', 'acct_exp', 'last_login':
51
            if user[k] is not None:
52
                user[k] = unicode(user[k])
53
54
        user['local_password'] = self.context.passhash is not None
55
        return user
56
1099.1.112 by William Grant
Implement authorization in JSON REST views. Add security declarations to
57
    @require_permission('edit')
1099.1.1 by Matt Giuca
Began implementing new dispatch framework (with Will Grant and Nick Chadwick).
58
    def PATCH(self, req, data):
59
        # XXX Admins can set extra fields
60
        # Note: Cannot change password here (use change_password named op)
61
62
        for f in user_fields_list:
63
            try:
64
                field = data[f]
65
                if isinstance(field, str):
66
                    field = unicode(field)
67
                setattr(self.context, f, field)
68
            except KeyError:
69
                continue
70
1099.1.33 by William Grant
Port www/apps/settings to new framework. It now appears under
71
class UserSettingsView(XHTMLView):
1099.1.35 by William Grant
ivle.webapp.base.xhtml#XHTMLView: Rename app_template to template (the things
72
    template = 'user-settings.html'
1099.1.33 by William Grant
Port www/apps/settings to new framework. It now appears under
73
    appname = 'settings'
1099.1.110 by William Grant
Implement an authorization system in the new framework. This breaks the REST
74
    permission = 'edit'
1099.1.35 by William Grant
ivle.webapp.base.xhtml#XHTMLView: Rename app_template to template (the things
75
1099.1.33 by William Grant
Port www/apps/settings to new framework. It now appears under
76
    def __init__(self, req, login):
77
        self.context = ivle.database.User.get_by_login(req.store, login)
1099.1.87 by William Grant
Fix views in ivle.webapp.admin and ivle.webapp.tutorial to return 404 wherever
78
        if self.context is None:
79
            raise NotFound()
1099.1.33 by William Grant
Port www/apps/settings to new framework. It now appears under
80
81
    def populate(self, req, ctx):
1099.1.61 by William Grant
Port ivle.webapp.admin.user's media to the new framework.
82
        self.plugin_scripts[Plugin] = ['settings.js']
83
        req.scripts_init = ['revert_settings']
84
1099.1.39 by William Grant
ivle.webapp.admin.user#UserSettingsView: s/settings_login/login/, as we can no
85
        ctx['login'] = self.context.login
1099.1.33 by William Grant
Port www/apps/settings to new framework. It now appears under
86
1099.1.99 by William Grant
Require that plugins providing media subclass MediaPlugin.
87
class Plugin(ViewPlugin, MediaPlugin):
1099.1.1 by Matt Giuca
Began implementing new dispatch framework (with Will Grant and Nick Chadwick).
88
    """
89
    The Plugin class for the user plugin.
90
    """
91
    # Magic attribute: urls
92
    # Sequence of pairs/triples of
93
    # (regex str, handler class, kwargs dict)
94
    # The kwargs dict is passed to the __init__ of the view object
95
    urls = [
1099.1.51 by William Grant
Move /users/:login/+settings to /~:login/+settings.
96
        ('~:login/+settings', UserSettingsView),
97
        ('api/~:login', UserRESTView),
1099.1.1 by Matt Giuca
Began implementing new dispatch framework (with Will Grant and Nick Chadwick).
98
    ]
1099.1.61 by William Grant
Port ivle.webapp.admin.user's media to the new framework.
99
100
    media = 'user-media'