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

1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
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
1165.3.4 by Nick Chadwick
Fixed an omission in XHTMLRESTView in which a template which had not
18
# Author: Matt Giuca, Will Grant, Nick Chadwick
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
19
1796.1.2 by William Grant
Replace @named_operation with @read_operation and @write_operation. Allow execution of read operations with a GET rather than a POST.
20
import cgi
21
import functools
22
import inspect
1165.3.4 by Nick Chadwick
Fixed an omission in XHTMLRESTView in which a template which had not
23
import os
1099.4.1 by Nick Chadwick
Working on putting worksheets into the database.
24
import urlparse
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
25
1801.1.1 by William Grant
Replace cjson with json, or simplejson if json is not available (Python <2.6)
26
try:
27
    import json
28
except ImportError:
29
    import simplejson as json
30
1165.2.1 by Nick Chadwick
Added an XHTMLRESTView, which returns normal json, with the addition
31
import genshi.template
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
32
33
from ivle.webapp.base.views import BaseView
1720 by William Grant
Share one TemplateLoader between every instance of every view, so we cache EVERYTHING.
34
from ivle.webapp.base.xhtml import GenshiLoaderMixin
1099.1.112 by William Grant
Implement authorization in JSON REST views. Add security declarations to
35
from ivle.webapp.errors import BadRequest, MethodNotAllowed, Unauthorized
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
36
37
class RESTView(BaseView):
38
    """
39
    A view which provides a RESTful interface. The content type is
40
    unspecified (see JSONRESTView for a specific content type).
41
    """
42
    content_type = "application/octet-stream"
43
44
    def render(self, req):
1099.1.52 by William Grant
ivle.webapp.base.rest#RESTView: Remove broken old render() - it should be
45
        raise NotImplementedError()
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
46
47
class JSONRESTView(RESTView):
48
    """
49
    A special case of RESTView which deals entirely in JSON.
50
    """
51
    content_type = "application/json"
52
53
    _allowed_methods = property(
54
        lambda self: [m for m in ('GET', 'PUT', 'PATCH')
55
                      if hasattr(self, m)] + ['POST'])
56
1099.1.112 by William Grant
Implement authorization in JSON REST views. Add security declarations to
57
    def authorize(self, req):
58
        return True # Real authz performed in render().
59
60
    def authorize_method(self, req, op):
61
        if not hasattr(op, '_rest_api_permission'):
62
            raise Unauthorized()
63
1544 by Matt Giuca
Added an argument 'config' to every single get_permissions method throughout the program. All calls to get_permissions pass a config. This is to allow per-site policy configurations on permissions.
64
        if (op._rest_api_permission not in
65
            self.get_permissions(req.user, req.config)):
1099.1.112 by William Grant
Implement authorization in JSON REST views. Add security declarations to
66
            raise Unauthorized()
1099.4.3 by Nick Chadwick
Updated the tutorial service, to now allow users to edit worksheets
67
    
68
    def convert_bool(self, value):
1099.1.188 by Nick Chadwick
Fixed a slight issue in convert_bool, which now uses a tuple, and
69
        if value in ('True', 'true', True):
1099.4.3 by Nick Chadwick
Updated the tutorial service, to now allow users to edit worksheets
70
            return True
1099.1.188 by Nick Chadwick
Fixed a slight issue in convert_bool, which now uses a tuple, and
71
        elif value in ('False', 'false', False):
1099.4.3 by Nick Chadwick
Updated the tutorial service, to now allow users to edit worksheets
72
            return False
73
        else:
74
            raise BadRequest()
1099.1.112 by William Grant
Implement authorization in JSON REST views. Add security declarations to
75
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
76
    def render(self, req):
77
        if req.method not in self._allowed_methods:
78
            raise MethodNotAllowed(allowed=self._allowed_methods)
79
80
        if req.method == 'GET':
1796.1.2 by William Grant
Replace @named_operation with @read_operation and @write_operation. Allow execution of read operations with a GET rather than a POST.
81
            qargs = dict(cgi.parse_qsl(
1796.1.3 by William Grant
Use req.unparsed_uri instead of req.uri -- req.uri doesn't contain the query string.
82
                urlparse.urlparse(req.unparsed_uri).query,
83
                keep_blank_values=1))
1796.1.2 by William Grant
Replace @named_operation with @read_operation and @write_operation. Allow execution of read operations with a GET rather than a POST.
84
            if 'ivle.op' in qargs:
85
                outjson = self._named_operation(req, qargs, readonly=True)
86
            else:
87
                self.authorize_method(req, self.GET)
88
                outjson = self.GET(req)
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
89
        # Since PATCH isn't yet an official HTTP method, we allow users to
90
        # turn a PUT into a PATCH by supplying a special header.
91
        elif req.method == 'PATCH' or (req.method == 'PUT' and
92
              'X-IVLE-Patch-Semantics' in req.headers_in and
93
              req.headers_in['X-IVLE-Patch-Semantics'].lower() == 'yes'):
1099.1.112 by William Grant
Implement authorization in JSON REST views. Add security declarations to
94
            self.authorize_method(req, self.PATCH)
1099.1.53 by William Grant
ivle.webapp.base.rest#JSONRESTView: Check for bad JSON input, rather than
95
            try:
1801.1.1 by William Grant
Replace cjson with json, or simplejson if json is not available (Python <2.6)
96
                input = json.loads(req.read())
97
            except ValueError:
1099.1.53 by William Grant
ivle.webapp.base.rest#JSONRESTView: Check for bad JSON input, rather than
98
                raise BadRequest('Invalid JSON data')
99
            outjson = self.PATCH(req, input)
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
100
        elif req.method == 'PUT':
1099.1.112 by William Grant
Implement authorization in JSON REST views. Add security declarations to
101
            self.authorize_method(req, self.PUT)
1099.1.53 by William Grant
ivle.webapp.base.rest#JSONRESTView: Check for bad JSON input, rather than
102
            try:
1801.1.1 by William Grant
Replace cjson with json, or simplejson if json is not available (Python <2.6)
103
                input = json.loads(req.read())
104
            except ValueError:
1099.1.53 by William Grant
ivle.webapp.base.rest#JSONRESTView: Check for bad JSON input, rather than
105
                raise BadRequest('Invalid JSON data')
106
            outjson = self.PUT(req, input)
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
107
        # POST implies named operation.
108
        elif req.method == 'POST':
109
            # TODO: Check Content-Type and implement multipart/form-data.
1099.4.1 by Nick Chadwick
Working on putting worksheets into the database.
110
            data = req.read()
1099.4.3 by Nick Chadwick
Updated the tutorial service, to now allow users to edit worksheets
111
            opargs = dict(cgi.parse_qsl(data, keep_blank_values=1))
1796.1.1 by William Grant
Factor out named operation execution, so we can tie it into the GET handler too.
112
            outjson = self._named_operation(req, opargs)
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
113
114
        req.content_type = self.content_type
1165.2.1 by Nick Chadwick
Added an XHTMLRESTView, which returns normal json, with the addition
115
        self.write_json(req, outjson)
116
117
    #This is a separate function to allow additional data to be passed through
118
    def write_json(self, req, outjson):
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
119
        if outjson is not None:
1801.1.1 by William Grant
Replace cjson with json, or simplejson if json is not available (Python <2.6)
120
            req.write(json.dumps(outjson))
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
121
            req.write("\n")
122
1796.1.2 by William Grant
Replace @named_operation with @read_operation and @write_operation. Allow execution of read operations with a GET rather than a POST.
123
    def _named_operation(self, req, opargs, readonly=False):
1796.1.1 by William Grant
Factor out named operation execution, so we can tie it into the GET handler too.
124
        try:
125
            opname = opargs['ivle.op']
126
            del opargs['ivle.op']
127
        except KeyError:
128
            raise BadRequest('No named operation specified.')
129
130
        try:
131
            op = getattr(self, opname)
132
        except AttributeError:
133
            raise BadRequest('Invalid named operation.')
134
135
        if not hasattr(op, '_rest_api_callable') or \
136
           not op._rest_api_callable:
137
            raise BadRequest('Invalid named operation.')
138
1796.1.2 by William Grant
Replace @named_operation with @read_operation and @write_operation. Allow execution of read operations with a GET rather than a POST.
139
        if readonly and op._rest_api_write_operation:
140
            raise BadRequest('POST required for write operation.')
141
1796.1.1 by William Grant
Factor out named operation execution, so we can tie it into the GET handler too.
142
        self.authorize_method(req, op)
143
144
        # Find any missing arguments, except for the first two (self, req)
145
        (args, vaargs, varkw, defaults) = inspect.getargspec(op)
146
        args = args[2:]
147
148
        # To find missing arguments, we eliminate the provided arguments
149
        # from the set of remaining function signature arguments. If the
150
        # remaining signature arguments are in the args[-len(defaults):],
151
        # we are OK.
152
        unspec = set(args) - set(opargs.keys())
153
        if unspec and not defaults:
154
            raise BadRequest('Missing arguments: ' + ', '.join(unspec))
155
156
        unspec = [k for k in unspec if k not in args[-len(defaults):]]
157
158
        if unspec:
159
            raise BadRequest('Missing arguments: ' + ', '.join(unspec))
160
161
        # We have extra arguments if the are no match args in the function
162
        # signature, AND there is no **.
163
        extra = set(opargs.keys()) - set(args)
164
        if extra and not varkw:
165
            raise BadRequest('Extra arguments: ' + ', '.join(extra))
166
167
        return op(req, **opargs)
168
1165.2.1 by Nick Chadwick
Added an XHTMLRESTView, which returns normal json, with the addition
169
1720 by William Grant
Share one TemplateLoader between every instance of every view, so we cache EVERYTHING.
170
class XHTMLRESTView(GenshiLoaderMixin, JSONRESTView):
1165.2.1 by Nick Chadwick
Added an XHTMLRESTView, which returns normal json, with the addition
171
    """A special type of RESTView which takes enhances the standard JSON
172
    with genshi XHTML functions.
173
    
174
    XHTMLRESTViews should have a template, which is rendered using their
175
    context. This is returned in the JSON as 'html'"""
176
    template = None
177
    ctx = genshi.template.Context()
178
179
    def render_fragment(self):
180
        if self.template is None:
181
            raise NotImplementedError()
182
1165.3.4 by Nick Chadwick
Fixed an omission in XHTMLRESTView in which a template which had not
183
        rest_template = os.path.join(os.path.dirname(
184
                inspect.getmodule(self).__file__), self.template)
1720 by William Grant
Share one TemplateLoader between every instance of every view, so we cache EVERYTHING.
185
        tmpl = self._loader.load(rest_template)
1165.3.4 by Nick Chadwick
Fixed an omission in XHTMLRESTView in which a template which had not
186
1165.2.1 by Nick Chadwick
Added an XHTMLRESTView, which returns normal json, with the addition
187
        return tmpl.generate(self.ctx).render('xhtml', doctype='xhtml')
188
    
189
    # This renders the template and adds it to the json
190
    def write_json(self, req, outjson):
191
        outjson["html"] = self.render_fragment()
1801.1.1 by William Grant
Replace cjson with json, or simplejson if json is not available (Python <2.6)
192
        req.write(json.dumps(outjson))
1165.2.1 by Nick Chadwick
Added an XHTMLRESTView, which returns normal json, with the addition
193
        req.write("\n")
194
1796.1.2 by William Grant
Replace @named_operation with @read_operation and @write_operation. Allow execution of read operations with a GET rather than a POST.
195
class _named_operation(object):
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
196
    '''Declare a function to be accessible to HTTP users via the REST API.
197
    '''
1796.1.2 by William Grant
Replace @named_operation with @read_operation and @write_operation. Allow execution of read operations with a GET rather than a POST.
198
    def __init__(self, write_operation, permission):
199
        self.write_operation = write_operation
1099.1.112 by William Grant
Implement authorization in JSON REST views. Add security declarations to
200
        self.permission = permission
201
202
    def __call__(self, func):
203
        func._rest_api_callable = True
1796.1.2 by William Grant
Replace @named_operation with @read_operation and @write_operation. Allow execution of read operations with a GET rather than a POST.
204
        func._rest_api_write_operation = self.write_operation
1099.1.112 by William Grant
Implement authorization in JSON REST views. Add security declarations to
205
        func._rest_api_permission = self.permission
206
        return func
207
1796.1.2 by William Grant
Replace @named_operation with @read_operation and @write_operation. Allow execution of read operations with a GET rather than a POST.
208
write_operation = functools.partial(_named_operation, True)
209
read_operation = functools.partial(_named_operation, False)
210
1099.1.112 by William Grant
Implement authorization in JSON REST views. Add security declarations to
211
class require_permission(object):
212
    '''Declare the permission required for use of a method via the REST API.
213
    '''
214
    def __init__(self, permission):
215
        self.permission = permission
216
217
    def __call__(self, func):
218
        func._rest_api_permission = self.permission
219
        return func