Created
February 23, 2016 19:58
-
-
Save level09/c979a69139ab76f8c819 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import ceil | |
class Pagination(object): | |
def __init__(self, page, per_page, total_count): | |
self.page = page | |
self.per_page = per_page | |
self.total_count = total_count | |
@property | |
def pages(self): | |
return int(ceil(self.total_count / float(self.per_page))) | |
@property | |
def has_prev(self): | |
return self.page > 1 | |
@property | |
def has_next(self): | |
return self.page < self.pages | |
def iter_pages(self, left_edge=2, left_current=2, | |
right_current=5, right_edge=2): | |
last = 0 | |
for num in xrange(1, self.pages + 1): | |
if num <= left_edge or \ | |
(num > self.page - left_current - 1 and \ | |
num < self.page + right_current) or \ | |
num > self.pages - right_edge: | |
if last + 1 != num: | |
yield None | |
yield num | |
last = num |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment