Chapter 17.3.3. Creating Your Own Filters

If you want to create your own filters, all you have to do is to subclass BaseFilter from the cherrypy.filters.basefilter module:

class PrintMessageFilter(BaseFilter):
    def before_main(self):
        print "This happens before the main page handler is called"

If you define a new method with the name of one of CherryPy’s predefined filter hook locations, that method will be called at that time in the request cycle.

But, it’s probably not enough to define a new filter—you probably want to actually have it run on some set of your controller objects, too. Fortunately that’s easy: You can just define a _cp_filters list containing all the custom filter classes that you want to run on objects in that controller ...

Get Rapid Web Applications with TurboGears: Using Python to Create Ajax-Powered Sites now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.