changeset 4188:a2709307da3d

New (sample) session service
author Florian Krupicka <florian.krupicka@googlemail.com>
date Wed, 25 Jun 2008 00:04:36 +0200
parents c659fd8d792d
children 6246e8f813b7
files MoinMoin/config/multiconfig.py MoinMoin/web/session.py
diffstat 2 files changed, 55 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/config/multiconfig.py	Tue Jun 24 23:54:44 2008 +0200
+++ b/MoinMoin/config/multiconfig.py	Wed Jun 25 00:04:36 2008 +0200
@@ -25,6 +25,7 @@
 from MoinMoin.packages import packLine
 from MoinMoin.security import AccessControlList
 from MoinMoin.support.python_compatibility import set
+from MoinMoin.web.session import FileSessionService
 
 _url_re_cache = None
 _farmconfig_mtime = None
@@ -529,6 +530,9 @@
 
     session_handler = session.DefaultSessionHandler()
     session_id_handler = session.MoinCookieSessionIDHandler()
+    
+    # new session service
+    session_service = FileSessionService()
 
     shared_intermap = None # can be string or list of strings (filenames)
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/web/session.py	Wed Jun 25 00:04:36 2008 +0200
@@ -0,0 +1,51 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - WSGI session handling
+
+    Session handling
+
+    @copyright: 2008 MoinMoin:FlorianKrupicka
+    @license: GNU GPL, see COPYING for details.
+"""
+
+from werkzeug.contrib.sessions import FilesystemSessionStore, Session
+
+from MoinMoin.web.api import ISessionService
+from MoinMoin import caching
+
+class MoinSession(Session):
+    """ Compatibility interface to Werkzeug-sessions for old Moin-code. """
+    is_new = property(lambda s: s.new)
+    is_stored = property(lambda s: True)
+
+class FileSessionService(object):
+    """
+    This sample session service stores session information in a temporary
+    directory and identifis the session via a cookie in the request/response
+    cycle.
+    """
+    
+    __implements__ = (ISessionService,)
+
+    def __init__(self, cookie_name='MOIN_SESSION'):
+        self.store = FilesystemSessionStore(session_class=MoinSession)
+        self.cookie_name = cookie_name
+        
+    def get_session(self, request):
+        sid = request.cookies.get(self.cookie_name, None)
+        if sid is None:
+            session = self.store.new()
+        else:
+            session = self.store.get(sid)
+        return session
+
+    def finalize(self, request, session):
+        if session.should_save:
+            self.store.save(session)
+            cookie_lifetime = request.cfg.cookie_lifetime * 3600
+            cookie_expires = time.time() + cookie_lifetime
+            cookie = dump_cookie(self.cookie_name, session.sid,
+                                 cookie_lifetime, cookie_expires,
+                                 request.cfg.cookie_domain,
+                                 request.cfg.cookie_path)
+            request.headers.append(('Set-Cookie', cookie))