Skip to content

Instantly share code, notes, and snippets.

@asplake
Created December 28, 2009 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asplake/264659 to your computer and use it in GitHub Desktop.
Save asplake/264659 to your computer and use it in GitHub Desktop.
Easy 405's and OPTIONS in Pylons
# Changes to your config/middleware.py:
# -from pylons.wsgiapp import PylonsApp
# +from pylons.wsgiapp import PylonsApp as _PylonsApp
# +from webob.exc import HTTPMethodNotAllowed, HTTPNotFound, HTTPOk
# Then:
methods = ['GET', 'PUT', 'POST', 'DELETE']
class PylonsApp(_PylonsApp):
def dispatch(self, controller, environ, start_response):
if not controller:
try:
old_method = environ['REQUEST_METHOD']
allowed_methods=[]
mapper = config['routes.map']
for method in methods:
environ['REQUEST_METHOD'] = method
if mapper.match(environ['PATH_INFO']):
allowed_methods.append(method)
if allowed_methods:
exc = HTTPOk() if old_method =='OPTIONS' else
HTTPMethodNotAllowed()
exc.allow = allowed_methods
return exc(environ, start_response)
finally:
environ['REQUEST_METHOD'] = old_method
return HTTPNotFound()(environ, start_response)
else:
return _PylonsApp.dispatch(self, controller, environ,
start_response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment