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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# IVLE - Informatics Virtual Learning Environment
# Copyright (C) 2007-2009 The University of Melbourne
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# Author: Matt Giuca, Will Grant

from ivle.webapp.base.rest import JSONRESTView
from ivle.webapp.base.xhtml import XHTMLView
from ivle.webapp.base.plugins import BasePlugin
import ivle.database
import ivle.util

# List of fields returned as part of the user JSON dictionary
# (as returned by the get_user action)
user_fields_list = (
    "login", "state", "unixid", "email", "nick", "fullname",
    "rolenm", "studentid", "acct_exp", "pass_exp", "last_login",
    "svn_pass"
)

class UserRESTView(JSONRESTView):
    """
    A REST interface to the user object.
    """
    def __init__(self, req, login):
        super(UserRESTView, self).__init__(self, req, login)
        self.context = ivle.database.User.get_by_login(req.store, login)

    def GET(self, req):
        # XXX Check Caps
        user = ivle.util.object_to_dict(user_fields_list, self.context)
        # Convert time stamps to nice strings
        for k in 'pass_exp', 'acct_exp', 'last_login':
            if user[k] is not None:
                user[k] = unicode(user[k])

        user['local_password'] = self.context.passhash is not None
        return user

    def PATCH(self, req, data):
        # XXX Check Caps
        # XXX Admins can set extra fields
        # Note: Cannot change password here (use change_password named op)

        for f in user_fields_list:
            try:
                field = data[f]
                if isinstance(field, str):
                    field = unicode(field)
                setattr(self.context, f, field)
            except KeyError:
                continue

class UserSettingsView(XHTMLView):
    template = 'user-settings.html'
    appname = 'settings'

    def __init__(self, req, login):
        self.context = ivle.database.User.get_by_login(req.store, login)

    def populate(self, req, ctx):
        if not self.context:
            raise NotFound()

        self.plugin_scripts[Plugin] = ['settings.js']
        req.scripts_init = ['revert_settings']

        ctx['login'] = self.context.login

class Plugin(BasePlugin):
    """
    The Plugin class for the user plugin.
    """
    # Magic attribute: urls
    # Sequence of pairs/triples of
    # (regex str, handler class, kwargs dict)
    # The kwargs dict is passed to the __init__ of the view object
    urls = [
        ('~:login/+settings', UserSettingsView),
        ('api/~:login', UserRESTView),
    ]

    media = 'user-media'