changeset 4531:83666cc9dc31

new cfg.session_dir setting, store sessions into cache_dir/__session__ by default
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 02 Feb 2009 05:54:21 +0100
parents 0ac99fdbe65d
children 65c0426f2b1f
files MoinMoin/config/multiconfig.py MoinMoin/web/session.py MoinMoin/wsgiapp.py
diffstat 3 files changed, 26 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/config/multiconfig.py	Mon Feb 02 04:51:57 2009 +0100
+++ b/MoinMoin/config/multiconfig.py	Mon Feb 02 05:54:21 2009 +0100
@@ -251,6 +251,11 @@
             name = dirname + '_dir'
             if not getattr(self, name, None):
                 setattr(self, name, os.path.abspath(os.path.join(data_dir, dirname)))
+        # directories below cache_dir (using __dirname__ to avoid conflicts)
+        for dirname in ('session', ):
+            name = dirname + '_dir'
+            if not getattr(self, name, None):
+                setattr(self, name, os.path.abspath(os.path.join(self.cache_dir, '__%s__' % dirname)))
 
         # Try to decode certain names which allow unicode
         self._decode()
@@ -898,6 +903,7 @@
     ('data_dir', './data/', "Path to the data directory containing your (locally made) wiki pages."),
     ('data_underlay_dir', './underlay/', "Path to the underlay directory containing distribution system and help pages."),
     ('cache_dir', None, "Directory for caching, by default computed from `data_dir`/cache."),
+    ('session_dir', None, "Directory for session storage, by default computed to be `cache_dir`/__session__."),
     ('user_dir', None, "Directory for user storage, by default computed to be `data_dir`/user."),
     ('plugin_dir', None, "Plugin directory, by default computed to be `data_dir`/plugin."),
     ('plugin_dirs', [], "Additional plugin directories."),
--- a/MoinMoin/web/session.py	Mon Feb 02 04:51:57 2009 +0100
+++ b/MoinMoin/web/session.py	Mon Feb 02 05:54:21 2009 +0100
@@ -16,6 +16,7 @@
 from werkzeug.contrib.sessions import FilesystemSessionStore, Session
 
 from MoinMoin import caching
+from MoinMoin.util import filesys
 
 from MoinMoin import log
 logging = log.getLogger(__name__)
@@ -55,21 +56,33 @@
     inherited from the builtin `dict`)
     """
     def __init__(self, cookie_name='MOIN_SESSION'):
-        self.store = FilesystemSessionStore(session_class=MoinSession)
         self.cookie_name = cookie_name
+        self.store = None
+
+    def _store_get(self, request):
+        if self.store is None:
+            path = request.cfg.session_dir
+            try:
+                filesys.mkdir(path)
+            except OSError:
+                pass
+            self.store = FilesystemSessionStore(path=path, filename_template='%s', session_class=MoinSession)
+        return self.store
 
     def get_session(self, request, sid=None):
         if sid is None:
             sid = request.cookies.get(self.cookie_name, None)
+        store = self._store_get(request)
         if sid is None:
-            session = self.store.new()
+            session = store.new()
         else:
-            session = self.store.get(sid)
+            session = store.get(sid)
         return session
 
     def destroy_session(self, request, session):
         session.clear()
-        self.store.delete(session)
+        store = self._store_get(request)
+        store.delete(session)
 
     def finalize(self, request, session):
         if request.user.auth_method == 'setuid':
@@ -105,5 +118,6 @@
             request.headers.add('Set-Cookie', cookie)
 
         if session.should_save:
-            self.store.save(session)
+            store = self._store_get(request)
+            store.save(session)
 
--- a/MoinMoin/wsgiapp.py	Mon Feb 02 04:51:57 2009 +0100
+++ b/MoinMoin/wsgiapp.py	Mon Feb 02 05:54:21 2009 +0100
@@ -32,7 +32,7 @@
 
     context.lang = setup_i18n_preauth(context)
 
-    context.session = context.cfg.session_service.get_session(request)
+    context.session = context.cfg.session_service.get_session(context)
 
     context.user = setup_user(context, context.session)