98
109
# TODO: Check Content-Type and implement multipart/form-data.
100
111
opargs = dict(cgi.parse_qsl(data, keep_blank_values=1))
102
opname = opargs['ivle.op']
103
del opargs['ivle.op']
105
raise BadRequest('No named operation specified.')
108
op = getattr(self, opname)
109
except AttributeError:
110
raise BadRequest('Invalid named operation.')
112
if not hasattr(op, '_rest_api_callable') or \
113
not op._rest_api_callable:
114
raise BadRequest('Invalid named operation.')
116
self.authorize_method(req, op)
118
# Find any missing arguments, except for the first two (self, req)
119
(args, vaargs, varkw, defaults) = inspect.getargspec(op)
122
# To find missing arguments, we eliminate the provided arguments
123
# from the set of remaining function signature arguments. If the
124
# remaining signature arguments are in the args[-len(defaults):],
126
unspec = set(args) - set(opargs.keys())
127
if unspec and not defaults:
128
raise BadRequest('Missing arguments: ' + ', '.join(unspec))
130
unspec = [k for k in unspec if k not in args[-len(defaults):]]
133
raise BadRequest('Missing arguments: ' + ', '.join(unspec))
135
# We have extra arguments if the are no match args in the function
136
# signature, AND there is no **.
137
extra = set(opargs.keys()) - set(args)
138
if extra and not varkw:
139
raise BadRequest('Extra arguments: ' + ', '.join(extra))
141
outjson = op(req, **opargs)
112
outjson = self._named_operation(req, opargs)
143
114
req.content_type = self.content_type
115
self.write_json(req, outjson)
117
#This is a separate function to allow additional data to be passed through
118
def write_json(self, req, outjson):
144
119
if outjson is not None:
145
req.write(cjson.encode(outjson))
120
req.write(json.dumps(outjson))
148
class named_operation(object):
123
def _named_operation(self, req, opargs, readonly=False):
125
opname = opargs['ivle.op']
126
del opargs['ivle.op']
128
raise BadRequest('No named operation specified.')
131
op = getattr(self, opname)
132
except AttributeError:
133
raise BadRequest('Invalid named operation.')
135
if not hasattr(op, '_rest_api_callable') or \
136
not op._rest_api_callable:
137
raise BadRequest('Invalid named operation.')
139
if readonly and op._rest_api_write_operation:
140
raise BadRequest('POST required for write operation.')
142
self.authorize_method(req, op)
144
# Find any missing arguments, except for the first two (self, req)
145
(args, vaargs, varkw, defaults) = inspect.getargspec(op)
148
# To find missing arguments, we eliminate the provided arguments
149
# from the set of remaining function signature arguments. If the
150
# remaining signature arguments are in the args[-len(defaults):],
152
unspec = set(args) - set(opargs.keys())
153
if unspec and not defaults:
154
raise BadRequest('Missing arguments: ' + ', '.join(unspec))
156
unspec = [k for k in unspec if k not in args[-len(defaults):]]
159
raise BadRequest('Missing arguments: ' + ', '.join(unspec))
161
# We have extra arguments if the are no match args in the function
162
# signature, AND there is no **.
163
extra = set(opargs.keys()) - set(args)
164
if extra and not varkw:
165
raise BadRequest('Extra arguments: ' + ', '.join(extra))
167
return op(req, **opargs)
170
class XHTMLRESTView(GenshiLoaderMixin, JSONRESTView):
171
"""A special type of RESTView which takes enhances the standard JSON
172
with genshi XHTML functions.
174
XHTMLRESTViews should have a template, which is rendered using their
175
context. This is returned in the JSON as 'html'"""
177
ctx = genshi.template.Context()
179
def render_fragment(self):
180
if self.template is None:
181
raise NotImplementedError()
183
rest_template = os.path.join(os.path.dirname(
184
inspect.getmodule(self).__file__), self.template)
185
tmpl = self._loader.load(rest_template)
187
return tmpl.generate(self.ctx).render('xhtml', doctype='xhtml')
189
# This renders the template and adds it to the json
190
def write_json(self, req, outjson):
191
outjson["html"] = self.render_fragment()
192
req.write(json.dumps(outjson))
195
class _named_operation(object):
149
196
'''Declare a function to be accessible to HTTP users via the REST API.
151
def __init__(self, permission):
198
def __init__(self, write_operation, permission):
199
self.write_operation = write_operation
152
200
self.permission = permission
154
202
def __call__(self, func):
155
203
func._rest_api_callable = True
204
func._rest_api_write_operation = self.write_operation
156
205
func._rest_api_permission = self.permission
208
write_operation = functools.partial(_named_operation, True)
209
read_operation = functools.partial(_named_operation, False)
159
211
class require_permission(object):
160
212
'''Declare the permission required for use of a method via the REST API.