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

« back to all changes in this revision

Viewing changes to ivle/webapp/base/rest.py

Give console and tutorial services security declarations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# along with this program; if not, write to the Free Software
16
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
17
 
18
 
# Author: Matt Giuca, Will Grant, Nick Chadwick
 
18
# Author: Matt Giuca, Will Grant
19
19
 
20
 
import os
21
20
import cgi
22
 
import urlparse
23
21
import inspect
24
22
 
25
23
import cjson
26
 
import genshi.template
27
24
 
28
25
from ivle.webapp.base.views import BaseView
29
26
from ivle.webapp.errors import BadRequest, MethodNotAllowed, Unauthorized
61
58
 
62
59
        if op._rest_api_permission not in self.get_permissions(req.user):
63
60
            raise Unauthorized()
64
 
    
65
 
    def convert_bool(self, value):
66
 
        if value in ('True', 'true', True):
67
 
            return True
68
 
        elif value in ('False', 'false', False):
69
 
            return False
70
 
        else:
71
 
            raise BadRequest()
72
61
 
73
62
    def render(self, req):
74
63
        if req.method not in self._allowed_methods:
98
87
        # POST implies named operation.
99
88
        elif req.method == 'POST':
100
89
            # TODO: Check Content-Type and implement multipart/form-data.
101
 
            data = req.read()
102
 
            opargs = dict(cgi.parse_qsl(data, keep_blank_values=1))
 
90
            opargs = dict(cgi.parse_qsl(req.read()))
103
91
            try:
104
92
                opname = opargs['ivle.op']
105
93
                del opargs['ivle.op']
143
131
            outjson = op(req, **opargs)
144
132
 
145
133
        req.content_type = self.content_type
146
 
        self.write_json(req, outjson)
147
 
 
148
 
    #This is a separate function to allow additional data to be passed through
149
 
    def write_json(self, req, outjson):
150
134
        if outjson is not None:
151
135
            req.write(cjson.encode(outjson))
152
136
            req.write("\n")
153
137
 
154
 
 
155
 
class XHTMLRESTView(JSONRESTView):
156
 
    """A special type of RESTView which takes enhances the standard JSON
157
 
    with genshi XHTML functions.
158
 
    
159
 
    XHTMLRESTViews should have a template, which is rendered using their
160
 
    context. This is returned in the JSON as 'html'"""
161
 
    template = None
162
 
    ctx = genshi.template.Context()
163
 
 
164
 
    def __init__(self, req, *args, **kwargs):
165
 
        for key in kwargs:
166
 
            setattr(self, key, kwargs[key])
167
 
    
168
 
    def render_fragment(self):
169
 
        if self.template is None:
170
 
            raise NotImplementedError()
171
 
 
172
 
        rest_template = os.path.join(os.path.dirname(
173
 
                inspect.getmodule(self).__file__), self.template)
174
 
        loader = genshi.template.TemplateLoader(".", auto_reload=True)
175
 
        tmpl = loader.load(rest_template)
176
 
 
177
 
        return tmpl.generate(self.ctx).render('xhtml', doctype='xhtml')
178
 
    
179
 
    # This renders the template and adds it to the json
180
 
    def write_json(self, req, outjson):
181
 
        outjson["html"] = self.render_fragment()
182
 
        req.write(cjson.encode(outjson))
183
 
        req.write("\n")
184
 
 
185
138
class named_operation(object):
186
139
    '''Declare a function to be accessible to HTTP users via the REST API.
187
140
    '''
202
155
    def __call__(self, func):
203
156
        func._rest_api_permission = self.permission
204
157
        return func
 
158