1. Problem

You wish to store server-side information about client agents; this information should follow around users as they browse through the application.

2. Solution

Enable sessions by using the SessionPublisher class instead of the regular Publisher. In your top-level script, simply do:

from quixote.publish import SessionPublisher
...
publisher = SessionPublisher()

3. Discussion

SessionPublisher uses two auxiliary classes:

  • SessionManager is a singleton (meaning there's only one instance of it) that's responsible for creating new Sessions and storing them.

  • Session contains information about the session. Most of its attributes are of little interest to a web application programmer; the single exception is the .user attribute, which you can use to record user information. The data type for .user isn't defined, so you can set it to a string, an instance of your own User class, or whatever you wish. The Dulcinea package includes a User class that you may want to look at.

The standard version of SessionManager stores sessions in a Python dictionary. This means that you must be invoking Quixote through some mechanism that uses long-lived processes; this rules out CGI, but any of FastCGI/SCGI/mod_python will work fine. If you have multiple processes, though, sessions will not be shared between them. For such situations you would have to write a SessionManager subclass that stored sessions in a relational database or disk file.


CategoryCookbook