~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.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
20
from ivle.webapp.base.rest import JSONRESTView
21
from ivle.webapp.base.xhtml import XHTMLView
1099.1.72 by Nick Chadwick
Dispatch now generates an index for each plugin type, allowing plugins to
22
from ivle.webapp.base.plugins import ViewPlugin
1099.1.1 by Matt Giuca
Began implementing new dispatch framework (with Will Grant and Nick Chadwick).
23
import ivle.database
24
import ivle.util
25
26
# List of fields returned as part of the user JSON dictionary
27
# (as returned by the get_user action)
28
user_fields_list = (
29
    "login", "state", "unixid", "email", "nick", "fullname",
30
    "rolenm", "studentid", "acct_exp", "pass_exp", "last_login",
31
    "svn_pass"
32
)
33
34
class UserRESTView(JSONRESTView):
35
    """
36
    A REST interface to the user object.
37
    """
38
    def __init__(self, req, login):
39
        super(UserRESTView, self).__init__(self, req, login)
40
        self.context = ivle.database.User.get_by_login(req.store, login)
41
42
    def GET(self, req):
43
        # XXX Check Caps
44
        user = ivle.util.object_to_dict(user_fields_list, self.context)
45
        # Convert time stamps to nice strings
46
        for k in 'pass_exp', 'acct_exp', 'last_login':
47
            if user[k] is not None:
48
                user[k] = unicode(user[k])
49
50
        user['local_password'] = self.context.passhash is not None
51
        return user
52
53
    def PATCH(self, req, data):
54
        # XXX Check Caps
55
        # XXX Admins can set extra fields
56
        # Note: Cannot change password here (use change_password named op)
57
58
        for f in user_fields_list:
59
            try:
60
                field = data[f]
61
                if isinstance(field, str):
62
                    field = unicode(field)
63
                setattr(self.context, f, field)
64
            except KeyError:
65
                continue
66
1099.1.33 by William Grant
Port www/apps/settings to new framework. It now appears under
67
class UserSettingsView(XHTMLView):
1099.1.35 by William Grant
ivle.webapp.base.xhtml#XHTMLView: Rename app_template to template (the things
68
    template = 'user-settings.html'
1099.1.33 by William Grant
Port www/apps/settings to new framework. It now appears under
69
    appname = 'settings'
1099.1.35 by William Grant
ivle.webapp.base.xhtml#XHTMLView: Rename app_template to template (the things
70
1099.1.33 by William Grant
Port www/apps/settings to new framework. It now appears under
71
    def __init__(self, req, login):
72
        self.context = ivle.database.User.get_by_login(req.store, login)
73
74
    def populate(self, req, ctx):
75
        if not self.context:
76
            raise NotFound()
77
1099.1.61 by William Grant
Port ivle.webapp.admin.user's media to the new framework.
78
        self.plugin_scripts[Plugin] = ['settings.js']
79
        req.scripts_init = ['revert_settings']
80
1099.1.39 by William Grant
ivle.webapp.admin.user#UserSettingsView: s/settings_login/login/, as we can no
81
        ctx['login'] = self.context.login
1099.1.33 by William Grant
Port www/apps/settings to new framework. It now appears under
82
1099.1.72 by Nick Chadwick
Dispatch now generates an index for each plugin type, allowing plugins to
83
class Plugin(ViewPlugin):
1099.1.1 by Matt Giuca
Began implementing new dispatch framework (with Will Grant and Nick Chadwick).
84
    """
85
    The Plugin class for the user plugin.
86
    """
87
    # Magic attribute: urls
88
    # Sequence of pairs/triples of
89
    # (regex str, handler class, kwargs dict)
90
    # The kwargs dict is passed to the __init__ of the view object
91
    urls = [
1099.1.51 by William Grant
Move /users/:login/+settings to /~:login/+settings.
92
        ('~:login/+settings', UserSettingsView),
93
        ('api/~:login', UserRESTView),
1099.1.1 by Matt Giuca
Began implementing new dispatch framework (with Will Grant and Nick Chadwick).
94
    ]
1099.1.61 by William Grant
Port ivle.webapp.admin.user's media to the new framework.
95
96
    media = 'user-media'