Anonymous avatar Anonymous committed f4f7d66

[svn] Tests SQLObject classness, also can handle being passed either an SQLAlchemy Table class, or an SQLAlchemy Mapper object.

Comments (0)

Files changed (1)

webhelpers/pagination/orm.py

 """ORM Wrappers"""
+import inspect
 from webhelpers.util import Partial
 
 orms = {}
     if isinstance(obj, (list, tuple)):
         return obj
     if orms.get('sqlobject'):
-        if issubclass(obj, sqlobject.SQLObject):
+        if inspect.isclass(obj) and issubclass(obj, sqlobject.SQLObject):
             return SQLObjectLazy(obj.select, *args, **kw)
     if orms.get('sqlalchemy'):
-        return SQLAlchemyLazy(obj.select, *args, **kw)
+        if hasattr(obj, '_is_primary_mapper'):
+            return SQLAlchemyLazyMapper(obj, *args, **kw)
+        if isinstance(obj, sqlalchemy.Table):
+            return SQLAlchemyLazyTable(obj, *args, **kw)
+    return "You shouldn't have this"
+    
 
 class SQLObjectLazy(Partial):
     def __getitem__(self, key):
     def __len__(self):
         return self().count()
 
-class SQLAlchemyLazy(Partial):
+class SQLAlchemyLazyTable(Partial):
     def __getitem__(self, key):
         if not isinstance(key, slice):
             raise Exception, "SQLAlchemyLazy doesn't support getitem without slicing"
-        limit = key.stop - key.start + 1
+        limit = key.stop - key.start
         offset = key.start
-        return self(limit=limit, offset=offset).execute()
+        fn = self.fn
+        self.fn = fn.select
+        results = self(limit=limit, offset=offset).execute()
+        self.fn = fn
+        return results
     
     def __len__(self):
-        s = self()
-        return self.fn([func.count(1)], from_obj=[s]).execute()
+        s = self.fn.select(*self.args, **self.kw)
+        return self.fn([func.count(1)], from_obj=[s])
+
+class SQLAlchemyLazyMapper(Partial):
+    def __getitem__(self, key):
+        if not isinstance(key, slice):
+            raise Exception, "SQLAlchemyLazy doesn't support getitem without slicing"
+        limit = key.stop - key.start
+        offset = key.start
+        fn = self.fn
+        self.fn = fn.select
+        results = self(limit=limit, offset=offset)
+        self.fn = fn
+        return results
+    
+    def __len__(self):
+        return self.fn.count(*self.args, **self.kw)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.