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

« back to all changes in this revision

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

  • Committer: William Grant
  • Date: 2009-02-23 23:02:35 UTC
  • mto: (1100.1.8 new-ui)
  • mto: This revision was merged to the branch mainline in revision 1119.
  • Revision ID: grantw@unimelb.edu.au-20090223230235-w6n1q408qnbz4is6
Shave off the corners of some worksheet boxes (notes, exercises).

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import inspect
23
23
 
24
24
import cjson
25
 
import genshi.template
26
25
 
27
26
from ivle.webapp.base.views import BaseView
28
27
from ivle.webapp.errors import BadRequest, MethodNotAllowed, Unauthorized
142
141
            outjson = op(req, **opargs)
143
142
 
144
143
        req.content_type = self.content_type
145
 
        self.write_json(req, outjson)
146
 
 
147
 
    #This is a separate function to allow additional data to be passed through
148
 
    def write_json(self, req, outjson):
149
144
        if outjson is not None:
150
145
            req.write(cjson.encode(outjson))
151
146
            req.write("\n")
152
147
 
153
 
 
154
 
class XHTMLRESTView(JSONRESTView):
155
 
    """A special type of RESTView which takes enhances the standard JSON
156
 
    with genshi XHTML functions.
157
 
    
158
 
    XHTMLRESTViews should have a template, which is rendered using their
159
 
    context. This is returned in the JSON as 'html'"""
160
 
    template = None
161
 
    ctx = genshi.template.Context()
162
 
 
163
 
    def __init__(self, req, *args, **kwargs):
164
 
        for key in kwargs:
165
 
            setattr(self, key, kwargs[key])
166
 
    
167
 
    def render_fragment(self):
168
 
        if self.template is None:
169
 
            raise NotImplementedError()
170
 
 
171
 
        return tmpl.generate(self.ctx).render('xhtml', doctype='xhtml')
172
 
    
173
 
    # This renders the template and adds it to the json
174
 
    def write_json(self, req, outjson):
175
 
        outjson["html"] = self.render_fragment()
176
 
        req.write(cjson.encode(outjson))
177
 
        req.write("\n")
178
 
 
179
148
class named_operation(object):
180
149
    '''Declare a function to be accessible to HTTP users via the REST API.
181
150
    '''
196
165
    def __call__(self, func):
197
166
        func._rest_api_permission = self.permission
198
167
        return func
 
168