Created
March 1, 2015 15:27
-
-
Save eerien/46d5b93c3aa09190becc to your computer and use it in GitHub Desktop.
Paginator for infinite scroll on Django.
This file contains 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
class Paginator(object): | |
def __init__(self, object_list, per_page=10, latest_id=None, latest_id_field='id', order_by='desc'): | |
assert type(latest_id_field) == str | |
self.object_list = object_list | |
self.per_page = per_page | |
self.latest_id = latest_id | |
self.latest_id_field = latest_id_field | |
self.order_by = order_by | |
def next(self): | |
start_index = 0 | |
if self.latest_id: | |
for obj in self.object_list: | |
if self.order_by == 'desc' and getattr(obj, self.latest_id_field) < self.latest_id: | |
break | |
elif self.order_by == 'asc' and getattr(obj, self.latest_id_field) > self.latest_id: | |
break | |
start_index += 1 | |
else: | |
return [] | |
return self.object_list[start_index:start_index+self.per_page] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment