views:

266

answers:

1

Hello Folks,

I'm using Routes for doing all the URL mapping job. Here's a typical route in my application:

map.routes('route', '/show/{title:[^/]+}', controller='generator', filter_=postprocess_title)

Quite often I have to strip some characters (like whitespace and underscore) from the {title} parameter. Currently there's one call per method in the controller to a function that does this conversion. It's not terribly convenient and I'd like Routes to do this job. Is it possible?

A: 

I am not familiar with Routes, and therefore I do not know if what you're after is possible with Routes.

But perhaps you could decorate your controller methods with a decorator that strips characters from parameters as needed?

Not sure if this would be more convenient. But to me, using a decorator has a different 'feel' than doing the same thing inline inside the controller method.

For instance:


@remove_spaces_from('title')
def my_controller(...):
    ...

If you are not familiar with decorators, a google search for "python decorators" will get you started. A key point to remember: When arguments are needed for a decorator, you need two levels of wrapping in the decorator.

codeape
Thank you for your suggestion, but I'd like to avoid code duplication as much as possible.