Reputation: 8016
I have a class with several routes and I'd like them to all use the json tools except for one. How can I exclude a specific route from a tool (foo
in the example below)?
import cherrypy
class HelloWorld(object):
_cp_config = {
'tools.json_out.on': True,
'tools.json_in.on': True,
'/foo': {
'tools.json_out.on': True,
'tools.json_in.on': True
}
}
@cherrypy.expose()
def index(self):
return "Hello World!"
@cherrypy.expose()
def foo(self):
return "Hello World!"
cherrypy.quickstart(HelloWorld())
Upvotes: 1
Views: 408
Reputation: 8016
An alternative, but equivalent, approach to @cyraxjoe's answer is
import cherrypy
class HelloWorld(object):
_cp_config = {
'tools.json_out.on': True,
'tools.json_in.on': True
}
@cherrypy.expose
def index(self):
return "Hello World!"
@cherrypy.expose
@cherrypy.config(**{'tools.json_in.on': False, 'tools.json_out.on': False})
def foo(self):
return "Hello World!"
foo._cp_config = {
'tools.json_out.on': False,
'tools.json_in.on': False,
}
cherrypy.quickstart(HelloWorld())
Upvotes: 1
Reputation: 5741
You can do that with the cherrypy.config
decorator:
import cherrypy
class HelloWorld(object):
_cp_config = {
'tools.json_out.on': True,
'tools.json_in.on': True
}
@cherrypy.expose
def index(self):
return "Hello World!"
@cherrypy.expose
@cherrypy.config(**{'tools.json_in.on': False, 'tools.json_out.on': False})
def foo(self):
return "Hello World!"
cherrypy.quickstart(HelloWorld())
Upvotes: 3