views:

29

answers:

2

The app is setup as a basic WSGI application. I'm just trying to call a function before the requestHandler is run.

I would like something very much like the way before_filter works in Rails.

Thanks.

A: 

I would use decorators, it's not exactly the same as before_filter in rails, but maybe good enough for you:

def before_filter(fn):
    def inner_function(self):
        # do stuff before
        return fn(self)
    return inner_function

class MainPage(webapp.RequestHandler):

    @before_filter
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp World!')
stefanw
+1  A: 

You can install your "before" as WSGI middleware -- App Engine uses WSGI, like just about every web framework and server in Python these days. Here's an example -- it's doing things after the handler runs, but it's even easier to do them before... in any case, your middleware "wraps" the WSGI application that's the actual app;-), so of course you can do things before, after, or instead;-).

For more on WSGI, see here.

Alex Martelli