diff options
author | Filipp Lepalaan <filipp@mac.com> | 2015-11-11 15:24:37 +0200 |
---|---|---|
committer | Filipp Lepalaan <filipp@mac.com> | 2015-11-11 15:24:37 +0200 |
commit | c95f56283781cbe3f372eeaccb804d2708606d6e (patch) | |
tree | 58f37aada00ab5e4efba312ff780efa83a450839 /servo/lib | |
parent | 1049e8d11c80a1838255ab37b07282a5c5d82c5b (diff) | |
download | Servo-c95f56283781cbe3f372eeaccb804d2708606d6e.tar.gz Servo-c95f56283781cbe3f372eeaccb804d2708606d6e.tar.bz2 Servo-c95f56283781cbe3f372eeaccb804d2708606d6e.zip |
Added paginator
Diffstat (limited to 'servo/lib')
-rw-r--r-- | servo/lib/utils.py | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/servo/lib/utils.py b/servo/lib/utils.py index 6d1ee0b..8d4bb91 100644 --- a/servo/lib/utils.py +++ b/servo/lib/utils.py @@ -6,8 +6,24 @@ from django.db.models import Model from django.core.cache import cache from django.http import HttpResponse from django.core.serializers.json import DjangoJSONEncoder +from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger +def paginate(queryset, page, count=10): + """ + Shortcut for paginating a queryset + """ + paginator = Paginator(queryset, count) + + try: + results = paginator.page(page) + except PageNotAnInteger: + results = paginator.page(1) + except EmptyPage: + results = paginator.page(paginator.num_pages) + + return results + def csv_response(data): """ Shortcut for sending a CSV response |