Reputation: 73
I have several actions that I would like to perform and apply to every request sent to my cherrypy server. The most common example involves reading the users cookies to see if he has a 'remember me' cookie, and if so, automatically login the user, and load some default information into the session.
Another possibility might be to check to see if a session already exists, and then load some User information into a default Jinja2 context.
I'd like this done on every request, and ideally would like to avoid manually putting a decorator in front of each of my functions.
I'm new to cherrypy 3's tools, and the documentation isn't clear to me regarding exactly what I must do. Any links to examples greatly appreciated.
Thanks
Upvotes: 2
Views: 277
Reputation: 14559
You don't have to use a decorator in front of your functions. Instead, declare the tool in config. See http://docs.cherrypy.org/dev/concepts/tools.html#config-files which shows you several ways to configure and turn on tools per URL (and all it children) or per class (and all its children). For example, you can turn on a tool for all URL's in your config file:
[/]
tools.sessions.on: True
The same goes for custom tools.
Upvotes: 1