close Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Version 1 (modified by cmlenz, 13 years ago) (diff)

--

Request Filters

Request filters are Python functions that can act on the request/response before/after the actual request handler is invoked. The application establishes a chain of request filters that are to be applied. Request filters generally look as follows:

def my_filter(request, response, chain):
    # Here you do anything you need to do before the request handler kicks in
    try:
        return chain.next()(request, response, chain)
    finally:
        # Here you do anything you want after the request handler has processed the request

The chain parameter is simply an iterator over the request filter functions, and the last function is the actual request handler.

A request filter can of course also manipulate the return value of the handler. For example, if the return value is a Genshi markup stream, it could apply stream filters.

Default Request Filters

The following request filters are available by default unless you remove them:

diva.errors.error_filter
Intercepts exceptions and displays either a friendly error page or the debugging page
diva.templating.template_filter
Serializes Genshi template output streams
diva.ui.chrome.chrome_filter
Injects any programmatically added scripts, style sheets, and links into the output stream
diva.i18n.l10n_filter
Handles locale negotiation and makes the corresponding translations catalog available
diva.http.caching_filter
Adds default cache control headers to responses
diva.forms.form_filter
Automatically populates HTML forms from previously entered data, and provides optional CSRF protection

Setting up Request Filters

To customize which request filters are run, you can override the get_filters() method of the Application class. For example:

from diva.core import Application
from diva.errors import error_filter
from diva.http import caching_filter
from diva.templating import template_filter

class MyApp(Application):

    def get_filters(self, environ):
        return [
            error_filter,
            template_filter,
            caching_filter
        ]

This implementation removes a number of the default filters. You could also add any custom filters you may have, etc.

Note that the ordering of request filters is quite important. For example, if you insert your custom filter before the error_filter, an exception raised in your filter function will not result in a friendly error page, but rather see the exception propagated all the way up. If you insert your filter before the template_filter, you will not get a Genshi stream as return value of the next function in the chain, but rather the rendered text.

Generally it makes the most sense to add custom filters close to the end of the list.

When to Use Request Filters

Some things that you may be tempted to implement as request filters would be better done by using WSGI middleware or features of the web server. For example, GZIP compression is available in Apache as a module, and there are various implementations of GZIP compression as WSGI middleware.

A good rule of thumb is that request filters are better suited if the functionality needs access to parts of the application or framework.