141
142
outjson = op(req, **opargs)
143
144
req.content_type = self.content_type
145
self.write_json(req, outjson)
147
#This is a separate function to allow additional data to be passed through
148
def write_json(self, req, outjson):
144
149
if outjson is not None:
145
150
req.write(cjson.encode(outjson))
154
class XHTMLRESTView(JSONRESTView):
155
"""A special type of RESTView which takes enhances the standard JSON
156
with genshi XHTML functions.
158
XHTMLRESTViews should have a template, which is rendered using their
159
context. This is returned in the JSON as 'html'"""
161
ctx = genshi.template.Context()
163
def __init__(self, req, *args, **kwargs):
165
setattr(self, key, kwargs[key])
167
def render_fragment(self):
168
if self.template is None:
169
raise NotImplementedError()
171
return tmpl.generate(self.ctx).render('xhtml', doctype='xhtml')
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))
148
179
class named_operation(object):
149
180
'''Declare a function to be accessible to HTTP users via the REST API.