Anonymous avatar Anonymous committed 3b5ab2c

[svn] Preliminary pagination module

Comments (0)

Files changed (3)

webhelpers/pagination/__init__.py

+"""Pagination functions and wrappers"""
+from orm import *
+
+class Paginator(object):
+    def __init__(self, collection, item_count, items_per_page, current_page=1):
+        self.collection = collection
+        self.item_count = item_count
+        self.items_per_page = items_per_page
+        self.pages = {}
+        self.current_page = current_page
+
+    def current_page__get(self):
+        return self[self.current_page_number]
+        
+    def current_page__set(self, page):
+        if isinstance(page, Page) and page.paginator != self:
+            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)
+
+    def first_page__get(self):
+        return self[1]
+    first = first_page = property(first_page__get)
+
+    def __getitem__(self, index):
+        return self.pages.setdefault(index, Page(self, index))
+
+    def __contains__(self, value):
+        if value >= 1 and value <= 100:
+            return True
+        return False
+
+class Page(object):
+    def __init__(self, paginator, number);
+        self.paginator = paginator
+        self.number = int(number)
+        if self.number not in paginator: self.number = 1
+
+    def __int__(self):
+        return self.number
+
+    def __eq__(self, page):
+        return self.paginator == page.paginator and self.number = page.number

webhelpers/pagination/links.py

+"""Pagination Link Generators"""
+
+def pagelist(page, pages):
+    """PHPbb style Pagination Links
+    
+    Example (Using Kid)::
+    
+        <div py:def="display_pager(url, page, pages)" class="pager">Page ${page} of ${pages}:
+            <span py:if="page != 1">
+            <a href="${url + str(page-1)}">&lt;&lt; Previous</a>
+            </span>
+            <span py:for="i in pagelist(page, pages)"> 
+                    <span py:if="i == None">...</span>
+                            <a py:if="i != page and i != None" href="${url + str(i)}">${i}</a>
+                            <span py:if="i == page">${i}</span>
+                    </span>
+                    <span py:if="page != pages">
+                             <a href="${url + str(page+1)}">Next &gt;&gt;</a>
+                    </span>
+            </span>
+        </div>
+    """
+    page = max(min(page, pages), 1)
+    topstart = pages < 1 + 3 and pages or 4
+    botmid = page-1 < 1 and 1 or page - 1
+    topmid = page+2 < pages and page+2 or pages
+    bottop = pages-2 < 1 and 1 or pages - 2
+    startpastmid = topstart >= botmid
+    midpasttop = topmid >= bottop
+    if startpastmid:
+        if midpasttop:
+            display = range(1, pages+1)
+        else:
+            display = range(1, topmid+1) + [None] + range(bottop, pages+1)
+    else:
+        if midpasttop:
+            display = range(1, topstart) + [None] + range(botmid-1, pages+1)
+        else:
+            display = (
+                    range(1, topstart) + [None] + range(botmid, topmid) + [None] +
+                    range(bottop, pages+1))
+    return display
Add a comment to this file

webhelpers/pagination/orm.py

Empty file added.

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.