Commits

Alexander Solovyov  committed bf765be

improve paginator

  • Participants
  • Parent commits ee41b0f

Comments (0)

Files changed (1)

File svarga/utils/paginator.py

             top = self.count
         return Page(self.object_list[bottom:top], number, self)
 
-    def _get_count(self):
+    @property
+    def count(self):
         "Returns the total number of objects, across all pages."
         if self._count is None:
             try:
                 # (i.e. is of type list).
                 self._count = len(self.object_list)
         return self._count
-    count = property(_get_count)
 
-    def _get_num_pages(self):
+    @property
+    def num_pages(self):
         "Returns the total number of pages."
         if self._num_pages is None:
             if self.count == 0 and not self.allow_empty_first_page:
                 hits = max(1, self.count - self.orphans)
                 self._num_pages = int(ceil(hits / float(self.per_page)))
         return self._num_pages
-    num_pages = property(_get_num_pages)
 
-    def _get_page_range(self):
+    @property
+    def page_range(self):
         """
         Returns a 1-based range of pages for iterating through within
         a template for loop.
         """
         return range(1, self.num_pages + 1)
-    page_range = property(_get_page_range)
 
 class Page(object):
     def __init__(self, object_list, number, paginator):
     def __repr__(self):
         return '<Page %s of %s>' % (self.number, self.paginator.num_pages)
 
+    def __iter__(self):
+        return iter(self.object_list)
+
+    @property
     def has_next(self):
         return self.number < self.paginator.num_pages
 
+    @property
     def has_previous(self):
         return self.number > 1
 
+    @property
     def has_other_pages(self):
         return self.has_previous() or self.has_next()
 
-    def next_page_number(self):
+    @property
+    def next(self):
         return self.number + 1
 
-    def previous_page_number(self):
+    @property
+    def previous(self):
         return self.number - 1
 
+    @property
     def start_index(self):
         """
         Returns the 1-based index of the first object on this page,
             return 0
         return (self.paginator.per_page * (self.number - 1)) + 1
 
+    @property
     def end_index(self):
         """
         Returns the 1-based index of the last object on this page,
         if self.number == self.paginator.num_pages:
             return self.paginator.count
         return self.number * self.paginator.per_page
+
+    def nearest(self, distance=2):
+        return range(max(1, self.number - distance), min(self.paginator.num_pages, self.number + distance) + 1)