gdw2
gdw2

Reputation: 8016

CherryPy: Turn off tool for one handler

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

Answers (2)

gdw2
gdw2

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

cyraxjoe
cyraxjoe

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

Related Questions