1. Lee McFadden
  2. WebHelpers


bbangert  committed 3d3ec80

[svn] Added full docstring for paginate, also fixed Page repr

  • Participants
  • Parent commits 7d202d1
  • Branches trunk

Comments (0)

Files changed (1)

File webhelpers/pagination/__init__.py

View file
  • Ignore whitespace
 from routes import request_config
 from orm import get_wrapper
-def paginate(collection, page=None, per_page=10, *args, **options):
+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
     used that will generate the proper query and extend properly for the
+    **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.
     collection = get_wrapper(collection, *args, **options)
-    count = len(collection)
-    page = getattr(request_config(), environ, {}).get('QUERY_ARGS')
-    paginator = Paginator(count, per_page, page)
+    if not item_count:
+        item_count = len(collection)
+    paginator = Paginator(item_count, per_page, page)
     subset = collection[paginator.current.first_item-1:paginator.current.last_item]
     return paginator, subset
             raise AttributeError("Page/Paginator mismatch")
         page = int(page)
         self.current_page_number = page in self and page or 1
-    current_page = property(current_page__get, current_page__set)
+    current = current_page = property(current_page__get, current_page__set)
     def first_page__get(self):
         return self[1]
     def window(self, padding = 2):
         return Window(self, padding)
+    def __repr__(self):
+        return str(self.number)
 class Window(object):
     def __init__(self, page, padding = 2):