~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-26 02:08:28 UTC
  • Revision ID: grantw@unimelb.edu.au-20090226020828-0qrhe3llq9r5olmr
ivle-showenrolment: Swap year and semester, and show the role.

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