Created
December 22, 2012 00:41
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another way to do something similar is with metaclasses.