Added support for the "_method" parameter in RESTController.

This commit is contained in:
Jonathan LaCour
2010-11-17 12:10:24 -05:00
parent 3e2386cf9e
commit b1fbdc3802
2 changed files with 17 additions and 6 deletions

View File

@@ -5,20 +5,21 @@ from pecan import request
class RestController(object):
# TODO: implement the following:
# - get, new, edit, post_delete, get_delete
# - implement the "_method" parameter
# - see: http://turbogears.org/2.1/docs/modules/tgcontroller.html
@expose()
def _route(self, args):
if request.method == 'GET':
method = request.GET.get('_method', request.method)
if method == 'GET':
if len(args):
return self.get_one, args
return self.get_all, []
elif request.method == 'POST':
elif method == 'POST':
return self.post, []
elif request.method == 'PUT' and len(args):
elif method == 'PUT' and len(args):
return self.put, args
elif request.method == 'DELETE' and len(args):
elif method == 'DELETE' and len(args):
return self.delete, args
@expose()

View File

@@ -81,4 +81,14 @@ class TestRestController(object):
# make sure it works
r = app.get('/things')
assert r.status_int == 200
assert len(loads(r.body)['items']) == 4
assert len(loads(r.body)['items']) == 4
# test _method parameter
r = app.get('/things/3?_method=DELETE')
assert r.status_int == 200
assert r.body == dumps('DELETED')
# make sure it works
r = app.get('/things')
assert r.status_int == 200
assert len(loads(r.body)['items']) == 3