]> git.decadent.org.uk Git - dak.git/commitdiff
Attach commit_or_flush to session object to call either .commit() or .flush()
authorChris Lamb <lamby@debian.org>
Tue, 27 Oct 2009 09:19:00 +0000 (09:19 +0000)
committerChris Lamb <lamby@debian.org>
Tue, 27 Oct 2009 13:52:29 +0000 (13:52 +0000)
Signed-off-by: Chris Lamb <lamby@debian.org>
daklib/dbconn.py

index fb322aeaf674e22fa455c6af95c84e004f8d8f78..ca90ba8882bedaa6dc316d94efd44fb2c09086ae 100755 (executable)
@@ -63,23 +63,38 @@ def session_wrapper(fn):
     Wrapper around common ".., session=None):" handling. If the wrapped
     function is called without passing 'session', we create a local one
     and destroy it when the function ends.
+
+    Also attaches a commit_or_flush method to the session; if we created a
+    local session, this is a synonym for session.commit(), otherwise it is a
+    synonym for session.flush().
     """
 
     def wrapped(*args, **kwargs):
         private_transaction = False
-        session = kwargs.get('session')
 
-        # No session specified as last argument or in kwargs, create one.
-        if session is None and len(args) <= len(getargspec(fn)[0]) - 1:
-            private_transaction = True
-            kwargs['session'] = DBConn().session()
+        # Find the session object
+        try:
+            session = kwargs['session']
+        except KeyError:
+            if len(args) <= len(getargspec(fn)[0]) - 1:
+                # No session specified as last argument or in kwargs
+                private_transaction = True
+                session = kwargs['session'] = DBConn().session()
+            else:
+                # Session is last argument in args
+                session = args[-1]
+
+        if private_transaction:
+            session.commit_or_flush = session.commit
+        else:
+            session.commit_or_flush = session.flush
 
         try:
             return fn(*args, **kwargs)
         finally:
             if private_transaction:
                 # We created a session; close it.
-                kwargs['session'].close()
+                session.close()
 
     wrapped.__doc__ = fn.__doc__
     wrapped.func_name = fn.func_name