1. Gustavo Picon
  2. feedjack

Commits

Gustavo Picon  committed 3b4372a

taking old code from django to get pagination working again

  • Participants
  • Parent commits 6c8b5a4
  • Branches default

Comments (0)

Files changed (1)

File feedjack/fjlib.py

View file
 
 from django.conf import settings
 from django.db import connection
-from django.core.paginator import ObjectPaginator, InvalidPage
+from django.core.paginator import Paginator, InvalidPage
 from django.http import Http404
 from django.utils.encoding import smart_unicode
 
 from feedjack import models
 from feedjack import fjcache
 
+
+# this is taken from django, it was removed in r8191
+class ObjectPaginator(Paginator):
+    """
+    Legacy ObjectPaginator class, for backwards compatibility.
+
+    Note that each method on this class that takes page_number expects a
+    zero-based page number, whereas the new API (Paginator/Page) uses one-based
+    page numbers.
+    """
+    def __init__(self, query_set, num_per_page, orphans=0):
+        Paginator.__init__(self, query_set, num_per_page, orphans)
+        import warnings
+        warnings.warn("The ObjectPaginator is deprecated. Use django.core.paginator.Paginator instead.", DeprecationWarning)
+
+        # Keep these attributes around for backwards compatibility.
+        self.query_set = query_set
+        self.num_per_page = num_per_page
+        self._hits = self._pages = None
+
+    def validate_page_number(self, page_number):
+        try:
+            page_number = int(page_number) + 1
+        except ValueError:
+            raise PageNotAnInteger
+        return self.validate_number(page_number)
+
+    def get_page(self, page_number):
+        try:
+            page_number = int(page_number) + 1
+        except ValueError:
+            raise PageNotAnInteger
+        return self.page(page_number).object_list
+
+    def has_next_page(self, page_number):
+        return page_number < self.pages - 1
+
+    def has_previous_page(self, page_number):
+        return page_number > 0
+
+    def first_on_page(self, page_number):
+        """
+        Returns the 1-based index of the first object on the given page,
+        relative to total objects found (hits).
+        """
+        page_number = self.validate_page_number(page_number)
+        return (self.num_per_page * (page_number - 1)) + 1
+
+    def last_on_page(self, page_number):
+        """
+        Returns the 1-based index of the last object on the given page,
+        relative to total objects found (hits).
+        """
+        page_number = self.validate_page_number(page_number)
+        if page_number == self.num_pages:
+            return self.count
+        return page_number * self.num_per_page
+
+    # The old API called it "hits" instead of "count".
+    hits = Paginator.count
+
+    # The old API called it "pages" instead of "num_pages".
+    pages = Paginator.num_pages
+
+
 def sitefeeds(siteobj):
     """ Returns the active feeds of a site.
     """