Skip to content

Instantly share code, notes, and snippets.

@cyraxjoe
Created December 22, 2012 00:41
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 cyraxjoe/4356820 to your computer and use it in GitHub Desktop.
Save cyraxjoe/4356820 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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