Tidied up sessions' freezechecking.
authorFredrik Tolf <fredrik@dolda2000.com>
Thu, 1 Dec 2011 20:33:32 +0000 (21:33 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Thu, 1 Dec 2011 20:33:32 +0000 (21:33 +0100)
wrw/session.py

index 284d092..2abe650 100644 (file)
@@ -110,6 +110,7 @@ class db(object):
     def fetch(self, req):
         now = int(time.time())
         sessid = cookie.get(req, self.cookiename)
+        new = False
         with self.lock:
             if self.cthread is None:
                 self.cthread = threading.Thread(target = self.cleanloop)
@@ -129,21 +130,19 @@ class db(object):
             except KeyError:
                 sess = session()
                 self.live[sess.id] = sess
-                sess.new = True
-        req.oncommit(self.ckfreeze)
+                new = True
+
+        def ckfreeze(req):
+            if sess.dirty():
+                try:
+                    if new:
+                        cookie.add(req, self.cookiename, sess.id, self.path)
+                    self.freeze(sess)
+                except:
+                    pass
+        req.oncommit(ckfreeze)
         return sess
 
-    def ckfreeze(self, req):
-        sess = self.get(req)
-        if sess.dirty():
-            try:
-                if getattr(sess, "new", False):
-                    cookie.add(req, self.cookiename, sess.id, self.path)
-                    del sess.new
-                self.freeze(sess)
-            except:
-                pass
-
     def thaw(self, sessid):
         raise KeyError()