From abb8f8d1494156a87dff52f21253b160a9dcac72 Mon Sep 17 00:00:00 2001 From: Stephen Burrows Date: Tue, 10 Aug 2010 13:24:36 -0400 Subject: [PATCH] Unimplemented PaginationProxy; moved pagination from penfield to core. --- contrib/penfield/models.py | 15 ++++----- contrib/penfield/utils.py | 66 -------------------------------------- utils.py | 39 ++++++++++++++++++++++ 3 files changed, 46 insertions(+), 74 deletions(-) delete mode 100644 contrib/penfield/utils.py diff --git a/contrib/penfield/models.py b/contrib/penfield/models.py index 449b63d..957c277 100644 --- a/contrib/penfield/models.py +++ b/contrib/penfield/models.py @@ -3,11 +3,10 @@ from django.conf import settings from philo.models import Tag, Titled, Entity, MultiView, Page, register_value_model from philo.exceptions import ViewCanNotProvideSubpath from django.conf.urls.defaults import url, patterns -from django.core.paginator import EmptyPage from django.core.urlresolvers import reverse from django.http import Http404, HttpResponse from datetime import datetime -from philo.contrib.penfield.utils import paginate +from philo.utils import paginate from philo.contrib.penfield.validators import validate_pagination_count @@ -131,10 +130,10 @@ class BlogView(MultiView): return base_patterns + entry_patterns def index_view(self, request, node=None, extra_context=None): - pagination = paginate(self.blog.entries.all(), self.entries_per_page, request.GET.get('page', 1)) + paginator, page, entries = paginate(self.blog.entries.all(), self.entries_per_page, request.GET.get('page', 1)) context = {} context.update(extra_context or {}) - context.update({'blog': self.blog, 'pagination': pagination}) + context.update({'blog': self.blog, 'paginator': paginator, 'paginated_page': page, 'entries': entries}) return self.index_page.render_to_response(node, request, extra_context=context) def entry_view(self, request, slug, year=None, month=None, day=None, node=None, extra_context=None): @@ -165,10 +164,10 @@ class BlogView(MultiView): if day: entries = entries.filter(date__day=day) - pagination = paginate(entries, self.entries_per_page, request.GET.get('page', 1)) + paginator, page, entries = paginate(entries, self.entries_per_page, request.GET.get('page', 1)) context = {} context.update(extra_context or {}) - context.update({'blog': self.blog, 'year': year, 'month': month, 'day': day, 'pagination': pagination}) + context.update({'blog': self.blog, 'year': year, 'month': month, 'day': day, 'paginator': paginator, 'paginated_page': page, 'entries': entries}) return self.entry_archive_page.render_to_response(node, request, extra_context=context) def tag_view(self, request, tag_slugs, node=None, extra_context=None): @@ -189,10 +188,10 @@ class BlogView(MultiView): if entries.count() <= 0: raise Http404 - pagination = paginate(entries, self.entries_per_page, request.GET.get('page', 1)) + paginator, page, entries = paginate(entries, self.entries_per_page, request.GET.get('page', 1)) context = {} context.update(extra_context or {}) - context.update({'blog': self.blog, 'tags': tags, 'pagination': pagination}) + context.update({'blog': self.blog, 'tags': tags, 'paginator': paginator, 'paginated_page': page, 'entries': entries}) return self.tag_page.render_to_response(node, request, extra_context=context) def tag_archive_view(self, request, node=None, extra_context=None): diff --git a/contrib/penfield/utils.py b/contrib/penfield/utils.py deleted file mode 100644 index 89d01a7..0000000 --- a/contrib/penfield/utils.py +++ /dev/null @@ -1,66 +0,0 @@ -from django.core.paginator import Paginator, EmptyPage - - -class PaginationProxy(object): - def __init__(self, paginator=None, page=None, objects=None): - self.paginator = paginator - self.page = page - self.objects = objects - - @property - def page_range(self): - if not self.paginator: - return None - - return self.paginator.page_range - - @property - def num_pages(self): - if not self.paginator: - return None - - return self.paginator.num_pages - - @property - def page_number(self): - if not self.page: - return None - - return self.page.number - - # Why doesn't this work? - #def __bool__(self): - # return bool(self.paginator) - - -def paginate(objects, per_page=None, page_number=1): - """ - Given a list of objects, return a (page, obj_list) tuple. - """ - try: - per_page = int(per_page) - except (TypeError, ValueError): - # Then either it wasn't set or it was set to an invalid value - return PaginationProxy(objects=objects) - - # There also shouldn't be pagination if the list is too short. Try count() - # first - good chance it's a queryset, where count is more efficient. - try: - if objects.count() <= per_page: - return PaginationProxy(objects=objects) - except AttributeError: - if len(objects) <= per_page: - return PaginationProxy(objects=objects) - - paginator = Paginator(objects, per_page) - try: - page_number = int(page_number) - except: - page_number = 1 - - try: - page = paginator.page(page_number) - except EmptyPage: - page = None - - return PaginationProxy(paginator, page, objects) \ No newline at end of file diff --git a/utils.py b/utils.py index 340e9e4..11f9848 100644 --- a/utils.py +++ b/utils.py @@ -1,5 +1,6 @@ from django.db import models from django.contrib.contenttypes.models import ContentType +from django.core.paginator import Paginator, EmptyPage class ContentTypeLimiter(object): @@ -62,3 +63,41 @@ def fattr(*args, **kwargs): setattr(function, key, kwargs[key]) return function return wrapper + + +def paginate(objects, per_page=None, page_number=1): + """ + Given a list of objects, return a (paginator, page, objects) tuple. + """ + try: + per_page = int(per_page) + except (TypeError, ValueError): + # Then either it wasn't set or it was set to an invalid value + paginator = page = None + + # There also shouldn't be pagination if the list is too short. Try count() + # first - good chance it's a queryset, where count is more efficient. + try: + if objects.count() <= per_page: + paginator = page = None + except AttributeError: + if len(objects) <= per_page: + paginator = page = None + + try: + return paginator, page, objects + except NameError: + pass + + paginator = Paginator(objects, per_page) + try: + page_number = int(page_number) + except: + page_number = 1 + + try: + page = paginator.page(page_number) + except EmptyPage: + page = None + + return paginator, page, objects -- 2.20.1