durusmail: quixote-users: My SessionManagerTimeout class
My SessionManagerTimeout class
2004-07-20
PyHtmlTable + Quixote
2004-07-22
My SessionManagerTimeout class
Jesse Pavel
2004-07-20

Hello,

I'm writing a SessionManager subclass that deletes sessions
after they've been inactive for a certain period. I'm a Quixote
newbie, and am hoping that you can take a look at this simple
class and tell me if I've made any important oversights.

A related question is whether I can trap SessionErrors (for
instance caused by my timing out sessions) with the standard
_q_exception_handler() function, or whether _q_exception_handler
only catches PublishError.

Thanks for your advice,
Jesse

------------------------------------------------------------
from quixote.session import SessionManager
import time

# This is a subclass of Quixote's SessionManager that provides for a session
# timeout parameter. When a new session object is requested, it will look
# through all of its current sessions and expire those which have remained
# unused for a certain period.

class SessionManagerTimeout(SessionManager):

    def __init__(self, session_class=None, session_mapping=None, timeout=None):
        SessionManager.__init__(self, session_class = session_class,
                                      session_mapping = session_mapping)
        self.timeout = timeout

    def maintain_session(self, request, session):
        if self.timeout and session.has_info() and session.id is None:
            # These are the criteria by which the maintain_session() method of
the
            # default SessionManager decides to store a new session object in
its
            # internal dictionary, and thus it is here that we must look through
the
            # extant sessions in that dictionary to delete stale ones.
            extant_sessions = self.values()
            curtime = time.time()
            for s in extant_sessions:
                if s.get_access_age(curtime) > self.timeout:
                    del self[session.id]

        SessionManager.maintain_session(self, request, session)
------------------------------------------------------------

reply