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

« back to all changes in this revision

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

  • Committer: Matt Giuca
  • Date: 2009-05-19 03:51:14 UTC
  • mto: This revision was merged to the branch mainline in revision 1322.
  • Revision ID: matt.giuca@gmail.com-20090519035114-4scjhvg7ai73anq0
doc/man: Renamed filesystem to directory hierarchy.

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
 
18
# Author: Matt Giuca, Will Grant, Nick Chadwick
19
19
 
 
20
import os
20
21
import cgi
21
22
import urlparse
22
23
import inspect
23
24
 
24
25
import cjson
 
26
import genshi.template
25
27
 
26
28
from ivle.webapp.base.views import BaseView
27
29
from ivle.webapp.errors import BadRequest, MethodNotAllowed, Unauthorized
141
143
            outjson = op(req, **opargs)
142
144
 
143
145
        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):
144
150
        if outjson is not None:
145
151
            req.write(cjson.encode(outjson))
146
152
            req.write("\n")
147
153
 
 
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
 
148
185
class named_operation(object):
149
186
    '''Declare a function to be accessible to HTTP users via the REST API.
150
187
    '''
165
202
    def __call__(self, func):
166
203
        func._rest_api_permission = self.permission
167
204
        return func
168