1.1 --- a/WebStack/Generic.py Mon Aug 16 18:13:45 2004 +0000
1.2 +++ b/WebStack/Generic.py Thu Aug 26 20:37:21 2004 +0000
1.3 @@ -367,6 +367,34 @@
1.4
1.5 raise NotImplementedError, "delete_cookie"
1.6
1.7 + # Session-related methods.
1.8 +
1.9 + def get_session(self, create=1):
1.10 +
1.11 + """
1.12 + A framework-specific method which returns a session corresponding to
1.13 + an identifier supplied in the transaction.
1.14 +
1.15 + If no session has yet been established according to information
1.16 + provided in the transaction then the optional 'create' parameter
1.17 + determines whether a new session will be established.
1.18 +
1.19 + Where no session has been established and where 'create' is set to 0
1.20 + then None is returned. In all other cases, a session object is created
1.21 + (where appropriate) and returned.
1.22 + """
1.23 +
1.24 + raise NotImplementedError, "get_session"
1.25 +
1.26 + def expire_session(self):
1.27 +
1.28 + """
1.29 + A framework-specific method which expires any session established
1.30 + according to information provided in the transaction.
1.31 + """
1.32 +
1.33 + raise NotImplementedError, "expire_session"
1.34 +
1.35 # Application-specific methods.
1.36
1.37 def set_user(self, username):
2.1 --- a/WebStack/JavaServlet.py Mon Aug 16 18:13:45 2004 +0000
2.2 +++ b/WebStack/JavaServlet.py Thu Aug 26 20:37:21 2004 +0000
2.3 @@ -382,6 +382,36 @@
2.4 cookie.setMaxAge(0)
2.5 self.response.addCookie(cookie)
2.6
2.7 + # Session-related methods.
2.8 +
2.9 + def get_session(self, create=1):
2.10 +
2.11 + """
2.12 + A framework-specific method which returns a session corresponding to
2.13 + an identifier supplied in the transaction.
2.14 +
2.15 + If no session has yet been established according to information
2.16 + provided in the transaction then the optional 'create' parameter
2.17 + determines whether a new session will be established.
2.18 +
2.19 + Where no session has been established and where 'create' is set to 0
2.20 + then None is returned. In all other cases, a session object is created
2.21 + (where appropriate) and returned.
2.22 + """
2.23 +
2.24 + return self.request.getSession(create)
2.25 +
2.26 + def expire_session(self):
2.27 +
2.28 + """
2.29 + A framework-specific method which expires any session established
2.30 + according to information provided in the transaction.
2.31 + """
2.32 +
2.33 + session = self.get_session(create=0)
2.34 + if session:
2.35 + session.invalidate()
2.36 +
2.37 # Application-specific methods.
2.38
2.39 def set_user(self, username):
2.40 @@ -394,4 +424,45 @@
2.41
2.42 self.user = username
2.43
2.44 +class Session:
2.45 +
2.46 + """
2.47 + A simple session class with behaviour more similar to the Python framework
2.48 + session classes.
2.49 + """
2.50 +
2.51 + def __init__(self, session):
2.52 +
2.53 + "Initialise the session object with the framework 'session' object."
2.54 +
2.55 + self.session = session
2.56 +
2.57 + def keys(self):
2.58 + keys = []
2.59 + keys_enum = self.session.getAttributeNames()
2.60 + while keys_enum.hasMoreElements():
2.61 + keys.append(keys_enum.nextElement())
2.62 + return keys
2.63 +
2.64 + def values(self):
2.65 + values = []
2.66 + for key in self.keys():
2.67 + values.append(self[key])
2.68 + return values
2.69 +
2.70 + def items(self):
2.71 + items = []
2.72 + for key in self.keys():
2.73 + items.append((key, self[key]))
2.74 + return items
2.75 +
2.76 + def __getitem__(self, key):
2.77 + return self.session.getAttribute(key)
2.78 +
2.79 + def __setitem__(self, key, value):
2.80 + self.session.setAttribute(key, value)
2.81 +
2.82 + def __delitem__(self, key):
2.83 + self.session.removeAttribute(key)
2.84 +
2.85 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/WebStack/ModPython.py Mon Aug 16 18:13:45 2004 +0000
3.2 +++ b/WebStack/ModPython.py Thu Aug 26 20:37:21 2004 +0000
3.3 @@ -8,11 +8,13 @@
3.4 from Helpers.Response import ConvertingStream
3.5 from mod_python.util import parse_qs, FieldStorage
3.6 from mod_python import apache
3.7 -try:
3.8 - from mod_python import Cookie
3.9 -except ImportError:
3.10 - # NOTE: Should provide an alternative implementation.
3.11 - Cookie = None
3.12 +
3.13 +# NOTE: Should provide alternative implementations.
3.14 +
3.15 +try: from mod_python import Cookie
3.16 +except ImportError: Cookie = None
3.17 +try: from mod_python import Session
3.18 +except ImportError: Session = None
3.19
3.20 class Transaction(Generic.Transaction):
3.21
3.22 @@ -341,6 +343,40 @@
3.23 # NOTE: Should raise an exception or provide an implementation.
3.24 pass
3.25
3.26 + # Session-related methods.
3.27 +
3.28 + def get_session(self, create=1):
3.29 +
3.30 + """
3.31 + A framework-specific method which returns a session corresponding to
3.32 + an identifier supplied in the transaction.
3.33 +
3.34 + If no session has yet been established according to information
3.35 + provided in the transaction then the optional 'create' parameter
3.36 + determines whether a new session will be established.
3.37 +
3.38 + Where no session has been established and where 'create' is set to 0
3.39 + then None is returned. In all other cases, a session object is created
3.40 + (where appropriate) and returned.
3.41 + """
3.42 +
3.43 + if Session:
3.44 + # NOTE: Not exposing all functionality.
3.45 + return Session.Session(self.trans)
3.46 + else:
3.47 + return None
3.48 +
3.49 + def expire_session(self):
3.50 +
3.51 + """
3.52 + A framework-specific method which expires any session established
3.53 + according to information provided in the transaction.
3.54 + """
3.55 +
3.56 + session = self.get_session(create=0)
3.57 + if session:
3.58 + session.invalidate()
3.59 +
3.60 # Application-specific methods.
3.61
3.62 def set_user(self, username):
4.1 --- a/WebStack/Webware.py Mon Aug 16 18:13:45 2004 +0000
4.2 +++ b/WebStack/Webware.py Thu Aug 26 20:37:21 2004 +0000
4.3 @@ -342,6 +342,37 @@
4.4
4.5 self.trans.response().delCookie(cookie_name)
4.6
4.7 + # Session-related methods.
4.8 +
4.9 + def get_session(self, create=1):
4.10 +
4.11 + """
4.12 + A framework-specific method which returns a session corresponding to
4.13 + an identifier supplied in the transaction.
4.14 +
4.15 + If no session has yet been established according to information
4.16 + provided in the transaction then the optional 'create' parameter
4.17 + determines whether a new session will be established.
4.18 +
4.19 + Where no session has been established and where 'create' is set to 0
4.20 + then None is returned. In all other cases, a session object is created
4.21 + (where appropriate) and returned.
4.22 + """
4.23 +
4.24 + # NOTE: Should really use Webware's hasSession method.
4.25 +
4.26 + session = self.trans.session()
4.27 + return Session(session)
4.28 +
4.29 + def expire_session(self):
4.30 +
4.31 + """
4.32 + A framework-specific method which expires any session established
4.33 + according to information provided in the transaction.
4.34 + """
4.35 +
4.36 + self.trans.request().setSessionExpired(1)
4.37 +
4.38 # Application-specific methods.
4.39
4.40 def set_user(self, username):
4.41 @@ -354,4 +385,17 @@
4.42
4.43 self.user = username
4.44
4.45 +class Session:
4.46 +
4.47 + "A more dictionary-like session object than the one Webware provides."
4.48 +
4.49 + def __init__(self, session):
4.50 + self.session = session
4.51 +
4.52 + def items(self):
4.53 + return self.session.values().items()
4.54 +
4.55 + def __getattr__(self, name):
4.56 + return getattr(self.__dict__["session"], name)
4.57 +
4.58 # vim: tabstop=4 expandtab shiftwidth=4