Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Idea of multiple objects embedded in one class to use a particular handler depending on the Accept header.
import cherrypy
class MultiViewHandler(object):
def __new__(cls):
"""Do something to be able to use the right view to the
right Accept header
"""
class SpecialDispatcher(cherrypy.dispatcher.Dispatcher)
"""Custom dispatcher to use the special properties of the
`MultiViewHandler` childs.
"""
class JSONPost(object):
@cherrypy.expose
def index(self):
pass
@cherrypy.expose
def default(self, id):
pass
class HTMLPost(object):
@cherrypy.expose
def index(self):
pass
@cherrypy.expose
def default(self, id):
pass
class Post(MultiViewHandler):
__views = [('text/html', HTMLPost),
('application/json', JSONPost)]
cherrypy.quickstart(Post(), config={'/': {'request.dispatcher': SpecialDispatcher()}})
@cyraxjoe

This comment has been minimized.

Copy link
Owner Author

cyraxjoe commented Dec 22, 2012

Another way to do something similar is with metaclasses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.