# HG changeset patch # User paulb # Date 1093552641 0 # Node ID e9593916fb23d647a127a0f98310352aa0e4e8f7 # Parent bc8d8d7923ee17f9e176baa1217b487796276e4c [project @ 2004-08-26 20:37:21 by paulb] Added some session support for Webware, mod_python, Java Servlet API. diff -r bc8d8d7923ee -r e9593916fb23 WebStack/Generic.py --- a/WebStack/Generic.py Mon Aug 16 18:13:45 2004 +0000 +++ b/WebStack/Generic.py Thu Aug 26 20:37:21 2004 +0000 @@ -367,6 +367,34 @@ raise NotImplementedError, "delete_cookie" + # Session-related methods. + + def get_session(self, create=1): + + """ + A framework-specific method which returns a session corresponding to + an identifier supplied in the transaction. + + If no session has yet been established according to information + provided in the transaction then the optional 'create' parameter + determines whether a new session will be established. + + Where no session has been established and where 'create' is set to 0 + then None is returned. In all other cases, a session object is created + (where appropriate) and returned. + """ + + raise NotImplementedError, "get_session" + + def expire_session(self): + + """ + A framework-specific method which expires any session established + according to information provided in the transaction. + """ + + raise NotImplementedError, "expire_session" + # Application-specific methods. def set_user(self, username): diff -r bc8d8d7923ee -r e9593916fb23 WebStack/JavaServlet.py --- a/WebStack/JavaServlet.py Mon Aug 16 18:13:45 2004 +0000 +++ b/WebStack/JavaServlet.py Thu Aug 26 20:37:21 2004 +0000 @@ -382,6 +382,36 @@ cookie.setMaxAge(0) self.response.addCookie(cookie) + # Session-related methods. + + def get_session(self, create=1): + + """ + A framework-specific method which returns a session corresponding to + an identifier supplied in the transaction. + + If no session has yet been established according to information + provided in the transaction then the optional 'create' parameter + determines whether a new session will be established. + + Where no session has been established and where 'create' is set to 0 + then None is returned. In all other cases, a session object is created + (where appropriate) and returned. + """ + + return self.request.getSession(create) + + def expire_session(self): + + """ + A framework-specific method which expires any session established + according to information provided in the transaction. + """ + + session = self.get_session(create=0) + if session: + session.invalidate() + # Application-specific methods. def set_user(self, username): @@ -394,4 +424,45 @@ self.user = username +class Session: + + """ + A simple session class with behaviour more similar to the Python framework + session classes. + """ + + def __init__(self, session): + + "Initialise the session object with the framework 'session' object." + + self.session = session + + def keys(self): + keys = [] + keys_enum = self.session.getAttributeNames() + while keys_enum.hasMoreElements(): + keys.append(keys_enum.nextElement()) + return keys + + def values(self): + values = [] + for key in self.keys(): + values.append(self[key]) + return values + + def items(self): + items = [] + for key in self.keys(): + items.append((key, self[key])) + return items + + def __getitem__(self, key): + return self.session.getAttribute(key) + + def __setitem__(self, key, value): + self.session.setAttribute(key, value) + + def __delitem__(self, key): + self.session.removeAttribute(key) + # vim: tabstop=4 expandtab shiftwidth=4 diff -r bc8d8d7923ee -r e9593916fb23 WebStack/ModPython.py --- a/WebStack/ModPython.py Mon Aug 16 18:13:45 2004 +0000 +++ b/WebStack/ModPython.py Thu Aug 26 20:37:21 2004 +0000 @@ -8,11 +8,13 @@ from Helpers.Response import ConvertingStream from mod_python.util import parse_qs, FieldStorage from mod_python import apache -try: - from mod_python import Cookie -except ImportError: - # NOTE: Should provide an alternative implementation. - Cookie = None + +# NOTE: Should provide alternative implementations. + +try: from mod_python import Cookie +except ImportError: Cookie = None +try: from mod_python import Session +except ImportError: Session = None class Transaction(Generic.Transaction): @@ -341,6 +343,40 @@ # NOTE: Should raise an exception or provide an implementation. pass + # Session-related methods. + + def get_session(self, create=1): + + """ + A framework-specific method which returns a session corresponding to + an identifier supplied in the transaction. + + If no session has yet been established according to information + provided in the transaction then the optional 'create' parameter + determines whether a new session will be established. + + Where no session has been established and where 'create' is set to 0 + then None is returned. In all other cases, a session object is created + (where appropriate) and returned. + """ + + if Session: + # NOTE: Not exposing all functionality. + return Session.Session(self.trans) + else: + return None + + def expire_session(self): + + """ + A framework-specific method which expires any session established + according to information provided in the transaction. + """ + + session = self.get_session(create=0) + if session: + session.invalidate() + # Application-specific methods. def set_user(self, username): diff -r bc8d8d7923ee -r e9593916fb23 WebStack/Webware.py --- a/WebStack/Webware.py Mon Aug 16 18:13:45 2004 +0000 +++ b/WebStack/Webware.py Thu Aug 26 20:37:21 2004 +0000 @@ -342,6 +342,37 @@ self.trans.response().delCookie(cookie_name) + # Session-related methods. + + def get_session(self, create=1): + + """ + A framework-specific method which returns a session corresponding to + an identifier supplied in the transaction. + + If no session has yet been established according to information + provided in the transaction then the optional 'create' parameter + determines whether a new session will be established. + + Where no session has been established and where 'create' is set to 0 + then None is returned. In all other cases, a session object is created + (where appropriate) and returned. + """ + + # NOTE: Should really use Webware's hasSession method. + + session = self.trans.session() + return Session(session) + + def expire_session(self): + + """ + A framework-specific method which expires any session established + according to information provided in the transaction. + """ + + self.trans.request().setSessionExpired(1) + # Application-specific methods. def set_user(self, username): @@ -354,4 +385,17 @@ self.user = username +class Session: + + "A more dictionary-like session object than the one Webware provides." + + def __init__(self, session): + self.session = session + + def items(self): + return self.session.values().items() + + def __getattr__(self, name): + return getattr(self.__dict__["session"], name) + # vim: tabstop=4 expandtab shiftwidth=4