Fix for session.clear for newer versions of SQLAlchemy.
Stephen Soltesz [Thu, 6 Oct 2011 23:31:16 +0000 (19:31 -0400)]
web/MonitorWeb/monitorweb/controllers.py
web/MonitorWeb/monitorweb/monitor_xmlrpc.py

index c67f118..c540888 100644 (file)
@@ -24,6 +24,13 @@ from monitor.wrapper.plccache import plcdb_hn2lb as site_hn2lb
 
 from monitorweb.templates.links import *
 
+def session_clear_all():
+    session.flush()
+    try: 
+        session.expunge_all() 
+    except AttributeError: # SQLAlchemy < 0.5.1 
+        session.clear() 
+
 class ObjectQueryFields(widgets.WidgetsList):
        """The WidgetsList defines the fields of the form."""
        pass
@@ -387,7 +394,7 @@ class Root(controllers.RootController, MonitorXmlrpcServer, LocalExtensions):
        def nodeslow(self, filter='boot'):
                print "NODE------------------"
                print "befor-len: ", len( [ i for i in session] )
-               session.flush(); session.expunge_all()
+        session_clear_all()
                print "after-len: ", len( [ i for i in session] )
                fbquery = FindbadNodeRecord.get_all_latest()
                query = []
@@ -511,7 +518,7 @@ class Root(controllers.RootController, MonitorXmlrpcServer, LocalExtensions):
 
 
        def pre_view(self, **data):
-               session.flush(); session.expunge_all()
+        session_clear_all()
 
                loginbase=None
                loginbase_list=[]
@@ -612,7 +619,7 @@ class Root(controllers.RootController, MonitorXmlrpcServer, LocalExtensions):
        @expose(template="monitorweb.templates.pcuview")
        @exception_handler(nodeaction_handler,"isinstance(tg_exceptions,RuntimeError)")
        def pcuviewold(self, loginbase=None, pcuid=None, hostname=None, since=20, **data):
-               session.flush(); session.expunge_all()
+        session_clear_all()
                sitequery=[]
                pcuquery=[]
                nodequery=[]
@@ -729,7 +736,7 @@ class Root(controllers.RootController, MonitorXmlrpcServer, LocalExtensions):
        def pcu(self, filter='all'):
                print "PCUVIEW------------------"
                print "befor-len: ", len( [ i for i in session] )
-               session.flush(); session.expunge_all()
+        session_clear_all()
                print "after-len: ", len( [ i for i in session] )
                fbquery = FindbadPCURecord.get_all_latest()
                query = []
@@ -767,7 +774,7 @@ class Root(controllers.RootController, MonitorXmlrpcServer, LocalExtensions):
        def site(self, filter='all'):
                print "SITE------------------"
                print "befor-len: ", len( [ i for i in session] )
-               session.flush(); session.expunge_all()
+        session_clear_all()
                print "after-len: ", len( [ i for i in session] )
                filtercount = {'good' : 0, 'down': 0, 'online':0, 'offline' : 0, 'new' : 0, 'pending' : 0, 'all' : 0}
                fbquery = HistorySiteRecord.query.all()
index 026a9ae..7d04c48 100644 (file)
@@ -98,7 +98,10 @@ class MonitorXmlrpcServer(object):
                        if method is None or not getattr(method, "exposed", False):
                                raise AssertionError("method does not exist")
 
-                       session.expunge_all()
+            try:
+                           session.expunge_all()
+            except AttributeError: # SQLAlchemy < 0.5.1
+                session.clear()
                        # Call the method, convert it into a 1-element tuple
                        # as expected by dumps                                     
                        response = method(*params)