Commits

Anonymous committed 3b0d0a6

[svn] * Added capability to pass pagination a SA 0.4 Session object which will be
used for queries. This allows compatibility with Session.mapper'd objects
and normal SA 0.4 mapper relations.

Comments (0)

Files changed (2)

 WebHelpers ChangeLog
 
 0.3.2 (**svn**)
+* Added capability to pass pagination a SA 0.4 Session object which will be 
+  used for queries. This allows compatibility with Session.mapper'd objects
+  and normal SA 0.4 mapper relations.
 * Updated SQLAlchemy ORM pagination for SA 0.4 Session.mapper objects.
 * Updated Scriptaculous to 1.7.1 beta 3 (1.7.0 is incompatible with Prototype
   1.5.1). Thanks errcw. Fixes #288.

webhelpers/pagination/orm.py

         if isinstance(obj, sqlalchemy.Table):
             return SQLAlchemyLazyTable(obj, *args, **kw)
         if hasattr(obj, 'query'):
+            return SQLAlchemy04LazySessionMapper(obj, *args, **kw)
+        if '_session' in kw:
             return SQLAlchemy04LazyMapper(obj, *args, **kw)
     raise TypeError("You must call paginate() with either a sequence, an "
                     "SQLObject class or an SQLAlchemy query object.")
                 kw[k] = v
         return self.fn.count(*self.args, **kw)
 
-class SQLAlchemy04LazyMapper(Partial):
+class SQLAlchemy04LazySessionMapper(Partial):
     def __getitem__(self, key):
         if not isinstance(key, slice):
             raise Exception, "SQLAlchemyLazy doesn't support getitem without slicing"
         for key, val in self.kw.iteritems():
             fn = getattr(fn, key)(val)
         return fn.count()
+
+class SQLAlchemy04LazyMapper(Partial):
+    def __getitem__(self, key):
+        if not isinstance(key, slice):
+            raise Exception, "SQLAlchemyLazy doesn't support getitem without slicing"
+        Session = self.kw.pop('_session')
+        limit = key.stop - key.start
+        offset = key.start
+        fn = self.fn
+        result = Session.query(fn)
+        if self.args:
+            result = result.filter(*self.args)
+        
+        # Translate keyword args like 'order_by=blah' into func calls for SA 0.4
+        # such that its .order_by(blah) on the query object
+        for key, val in self.kw.iteritems():
+            result = getattr(result, key)(val)
+        query = result.limit(limit).offset(offset)
+        self.kw['_session'] = Session
+        return query.all()
+    
+    def __len__(self):
+        Session = self.kw.pop('_session')
+        kw = {}
+        fn = Session.query(self.fn)
+        if self.args:
+            fn = fn.filter(*self.args)
+        
+        for key, val in self.kw.iteritems():
+            fn = getattr(fn, key)(val)
+        count = fn.count()
+        self.kw['_session'] = Session
+        return count
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.