Commits

Anonymous committed 5acbe6a

[svn] * Added environ checking with Routes so that page will be automatically pulled
out of the query string, or from the Routes match dict if available.
* Added checking for objects that had assign_mapper applied to them.

Comments (0)

Files changed (3)

 WebHelpers ChangeLog
 
 0.2.3 (**svn**)
+* Added environ checking with Routes so that page will be automatically pulled
+  out of the query string, or from the Routes match dict if available.
+* Added checking for objects that had assign_mapper applied to them.
+* Added better range checking to paginator to require a positive value that is
+  less than the total amount of pages available for a page.
 * WARNING: Due to a typo, the Text helper highlight function no longer
   highlights text with the CSS class name 'hilight' by default: it now uses the
   CSS class name 'highlight' instead. The function's 'hilighter' keyword

webhelpers/pagination/__init__.py

 standard Python list slicing operations. These methods can also be used
 individually and customized to do as much or little as desired.
 
-The Paginator itself maintains pagination logic associated with each page, where
-it begins, what the first/last item on the page is, etc.
+The Paginator itself maintains pagination logic associated with each page, 
+where begins, what the first/last item on the page is, etc.
 
 Helper functions hook-up the Paginator in more conveinent methods for the more
 macro-style approach to return the Paginator and the slice of the collection
 desired.
 
 """
+import re
+
 from routes import request_config
 from orm import get_wrapper
 
-def paginate(collection, page=None, per_page=10, item_count=None, *args, **options):
+find_page = re.compile('page=(\d+)', re.I)
+
+def paginate(collection, page=None, per_page=10, item_count=None, *args,
+             **options):
     """Paginate a collection of data
     
     If the collection is a list, it will return the slice of the list along
     
     Example::
     
-        # In this case, Person is a SQLObject class, or it could be a list/tuple
+        # In this case, Person is a SQLObject class, or it could be a 
+        # list/tuple
         person_paginator, person_set = paginate(Person, page=1)
         
         set_count = int(person_paginator.current)
         total_pages = len(person_paginator)
     
-    Current ORM support is limited to SQLObject and SQLAlchemy. You can use any ORM
-    you'd like with the Paginator as it will give you the offset/limit data necessary
-    to make your own query.
+    Current ORM support is limited to SQLObject and SQLAlchemy. You can use any
+    ORM you'd like with the Paginator as it will give you the offset/limit 
+    data necessary to make your own query.
     
-    **WARNING:** Unless you pass in an item_count, a count will be performed on the
-    collection every time paginate is called. If using an ORM, it's suggested that
-    you count the items yourself and/or cache them.
+    **WARNING:** Unless you pass in an item_count, a count will be performed 
+    on the collection every time paginate is called. If using an ORM, it's 
+    suggested that you count the items yourself and/or cache them.
+    """
+    # If our page wasn't passed in, attempt to pull out either a page arg from
+    # the routes route path, or try the environ GET.
+    if page is None:
+        config = request_config()
+        if hasattr(config, 'mapper_dict'):
+            page = config.mapper_dict.get('page')
+        if page is not None:
+            if re.match(r'\d+', page):
+                page = int(page)
+            else:
+                page = 0
+        elif page is None and hasattr(config, 'environ'):
+            page_match = re.match(find_page, 
+                                  config.environ.get('QUERY_STRING', ''))
+            if page_match:
+                page = int(page_match.groups()[0])
+        
+        # If environ is set, and no page has been we will assume they wanted to
+        # find a page value but didn't so we default to 0 now.
+        if page is None:
+            page = 0
     
-    """
     collection = get_wrapper(collection, *args, **options)
     if not item_count:
         item_count = len(collection)
     paginator = Paginator(item_count, per_page, page)
-    subset = collection[paginator.current.first_item:paginator.current.last_item]
+    if page < 0 or page >= len(paginator):
+        subset = []
+    else:
+        subset = collection[paginator.current.first_item:paginator.current.last_item]
     
     return paginator, subset
     

webhelpers/pagination/orm.py

     if orms.get('sqlalchemy'):
         if hasattr(obj, '_is_primary_mapper') or isinstance(obj, sqlalchemy.Query):
             return SQLAlchemyLazyMapper(obj, *args, **kw)
+        if hasattr(obj, 'mapper') and hasattr(obj, 'select') and hasattr(obj, 'count'):
+            return SQLAlchemyLazyMapper(obj, *args, **kw)
         if isinstance(obj, sqlalchemy.Table):
             return SQLAlchemyLazyTable(obj, *args, **kw)
     raise TypeError("You must call paginate() with either a sequence, an "
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.