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

« back to all changes in this revision

Viewing changes to ivle/webapp/admin/user.py

ivle.webapp.testing: Add, with fake request and user.
ivle.webapp.base.test: Add! Test the JSONRESTView, using the new mocks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
# Author: Matt Giuca, Will Grant
19
19
 
20
 
from ivle.webapp.base.rest import JSONRESTView, require_permission
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
 
20
from ivle.webapp.base.views import JSONRESTView
 
21
from ivle.webapp.base.plugins import BasePlugin
24
22
import ivle.database
25
23
import ivle.util
26
24
 
28
26
# (as returned by the get_user action)
29
27
user_fields_list = (
30
28
    "login", "state", "unixid", "email", "nick", "fullname",
31
 
    "admin", "studentid", "acct_exp", "pass_exp", "last_login",
 
29
    "rolenm", "studentid", "acct_exp", "pass_exp", "last_login",
32
30
    "svn_pass"
33
31
)
34
32
 
39
37
    def __init__(self, req, login):
40
38
        super(UserRESTView, self).__init__(self, req, login)
41
39
        self.context = ivle.database.User.get_by_login(req.store, login)
42
 
        if self.context is None:
43
 
            raise NotFound()
44
40
 
45
 
    @require_permission('view')
46
41
    def GET(self, req):
47
42
        # XXX Check Caps
48
43
        user = ivle.util.object_to_dict(user_fields_list, self.context)
54
49
        user['local_password'] = self.context.passhash is not None
55
50
        return user
56
51
 
57
 
class UserSettingsView(XHTMLView):
58
 
    template = 'user-settings.html'
59
 
    tab = 'settings'
60
 
    permission = 'edit'
61
 
 
62
 
    def __init__(self, req, login):
63
 
        self.context = ivle.database.User.get_by_login(req.store, login)
64
 
        if self.context is None:
65
 
            raise NotFound()
66
 
 
67
 
    def populate(self, req, ctx):
68
 
        self.plugin_scripts[Plugin] = ['settings.js']
69
 
        req.scripts_init = ['revert_settings']
70
 
 
71
 
        ctx['login'] = self.context.login
72
 
 
73
 
class Plugin(ViewPlugin, MediaPlugin):
 
52
    def PATCH(self, req, data):
 
53
        # XXX Check Caps
 
54
        # XXX Admins can set extra fields
 
55
        # Note: Cannot change password here (use change_password named op)
 
56
 
 
57
        for f in user_fields_list:
 
58
            try:
 
59
                field = data[f]
 
60
                if isinstance(field, str):
 
61
                    field = unicode(field)
 
62
                setattr(self.context, f, field)
 
63
            except KeyError:
 
64
                continue
 
65
 
 
66
class Plugin(BasePlugin):
74
67
    """
75
68
    The Plugin class for the user plugin.
76
69
    """
79
72
    # (regex str, handler class, kwargs dict)
80
73
    # The kwargs dict is passed to the __init__ of the view object
81
74
    urls = [
82
 
        ('~:login/+settings', UserSettingsView),
83
 
        ('api/~:login', UserRESTView),
 
75
        ('api/users/:login', UserRESTView)
84
76
    ]
85
 
 
86
 
    media = 'user-media'