Enlightened by Daniel Klyuyev's answer, I ended up with web.application to add support for the default method in the _delegate method:
import types class application(web.application): def _delegate(self, f, fvars, args=[]): def handle_class(cls): meth = web.ctx.method if meth == 'HEAD' and not hasattr(cls, meth): meth = 'GET' if not hasattr(cls, meth): if hasattr(cls, '_default'): tocall = getattr(cls(), '_default') return tocall(*args) raise web.nomethod(cls) tocall = getattr(cls(), meth) return tocall(*args) def is_class(o): return isinstance(o, (types.ClassType, type)) ...
Instantiation:
app = application(urls, globals())
Page Class:
class secret(): def _default(self): raise web.notfound() def GET(self): ...
I prefer this solution because it keeps the page classes clean and further configures the delegation process in one place. For example, another function I wanted was transparent overloaded POST (for example, redirecting a POST request using method=DELETE to the DELETE method of the page class), and this is just to add this too:
... meth = web.ctx.method if meth == 'POST' and 'method' in web.input(): meth = web.input()['method'] ...
Ian mackinnon
source share