Revised penfield's pagination helper to work more generically.
authorStephen Burrows <stephen.r.burrows@gmail.com>
Thu, 5 Aug 2010 20:02:21 +0000 (16:02 -0400)
committerStephen Burrows <stephen.r.burrows@gmail.com>
Mon, 9 Aug 2010 11:43:29 +0000 (07:43 -0400)
contrib/penfield/models.py
contrib/penfield/utils.py

index 2a10e26..94ad5c3 100644 (file)
@@ -3,6 +3,7 @@ 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
@@ -129,13 +130,14 @@ class BlogView(MultiView):
                        )
                return base_patterns + entry_patterns
        
+       def paginate(self, objects, page_number):
+               try:
+                       return paginate(objects, self.entries_per_page, page_number)
+               except EmptyPage:
+                       raise Http404
+       
        def index_view(self, request, node=None, extra_context=None):
-               entries = self.blog.entries.all()
-               if self.entries_per_page:
-                       paginated_page = paginate(request, entries, self.entries_per_page)
-                       entries = paginated_page.object_list
-               else:
-                       paginated_page = None
+               paginated_page, entries = self.paginate(self.blog.entries.all(), request.GET.get('page', 1))
                context = {}
                context.update(extra_context or {})
                context.update({'blog': self.blog, 'entries': entries, 'paginated_page': paginated_page})
@@ -168,11 +170,8 @@ class BlogView(MultiView):
                        entries = entries.filter(date__month=month)
                if day:
                        entries = entries.filter(date__day=day)
-               if self.entries_per_page:
-                       paginated_page = paginate(request, entries, self.entries_per_page)
-                       entries = paginated_page.object_list
-               else:
-                       paginated_page = None
+               
+               paginated_page, entries = self.paginate(entries, request.GET.get('page', 1))
                context = {}
                context.update(extra_context or {})
                context.update({'blog': self.blog, 'year': year, 'month': month, 'day': day, 'entries': entries, 'paginated_page': paginated_page})
@@ -196,11 +195,7 @@ class BlogView(MultiView):
                if entries.count() <= 0:
                        raise Http404
                
-               if self.entries_per_page:
-                       paginated_page = paginate(request, entries, self.entries_per_page)
-                       entries = paginated_page.object_list
-               else:
-                       paginated_page = None
+               paginated_page, entries = self.paginate(entries, request.GET.get('page', 1))
                context = {}
                context.update(extra_context or {})
                context.update({'blog': self.blog, 'tags': tags, 'entries': entries, 'paginated_page': paginated_page})
index 5df800c..0318e5e 100644 (file)
@@ -1,20 +1,33 @@
-from django.core.paginator import Paginator, InvalidPage, EmptyPage
+from django.core.paginator import Paginator
 
 
-def paginate(request, entries, entries_per_page):
-       paginator = Paginator(entries, entries_per_page)
+def paginate(objects, per_page=None, page_number=1):
+       """
+       Given a list of objects, return a (page, obj_list) tuple.
+       """
        try:
-               page_number = int(request.GET.get('page', '1'))
-               entries = paginator.page(page_number).object_list
-               page = paginator.page(page_number)
-       except ValueError:
-               page_number = 1
-               entries = paginator.page(page_number).object_list
-               page = paginator.page(page_number)
+               per_page = int(per_page)
+       except (TypeError, ValueError):
+               # Then either it wasn't set or it was set to an invalid value
+               return None, 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 None, objects
+       except AttributeError:
+               if len(objects) <= per_page:
+                       return None, objects
+       
+       paginator = Paginator(objects, per_page)
        try:
-               entries = paginator.page(page_number).object_list
-               page = paginator.page(page_number)
-       except (EmptyPage, InvalidPage):
-               entries = paginator.page(paginator.num_pages).object_list
-               page = paginator.page(page_number)
-       return page
\ No newline at end of file
+               page_number = int(page_number)
+       except:
+               page_number = 1
+       
+       # This will raise an EmptyPage error if the page number is out of range.
+       # This error is intentionally left for the calling function to handle.
+       page = paginator.page(page_number)
+       
+       return page, page.object_list
\ No newline at end of file