Introduced the PaginationProxy object to simplify use in templates.
authorStephen Burrows <stephen.r.burrows@gmail.com>
Thu, 5 Aug 2010 22:12:22 +0000 (18:12 -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 94ad5c3..449b63d 100644 (file)
@@ -130,17 +130,11 @@ 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):
-               paginated_page, entries = self.paginate(self.blog.entries.all(), request.GET.get('page', 1))
+               pagination = 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, 'entries': entries, 'paginated_page': paginated_page})
+               context.update({'blog': self.blog, 'pagination': pagination})
                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):
@@ -171,10 +165,10 @@ class BlogView(MultiView):
                if day:
                        entries = entries.filter(date__day=day)
                
-               paginated_page, entries = self.paginate(entries, request.GET.get('page', 1))
+               pagination = 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, 'entries': entries, 'paginated_page': paginated_page})
+               context.update({'blog': self.blog, 'year': year, 'month': month, 'day': day, 'pagination': pagination})
                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):
@@ -195,10 +189,10 @@ class BlogView(MultiView):
                if entries.count() <= 0:
                        raise Http404
                
-               paginated_page, entries = self.paginate(entries, request.GET.get('page', 1))
+               pagination = paginate(entries, self.entries_per_page, request.GET.get('page', 1))
                context = {}
                context.update(extra_context or {})
-               context.update({'blog': self.blog, 'tags': tags, 'entries': entries, 'paginated_page': paginated_page})
+               context.update({'blog': self.blog, 'tags': tags, 'pagination': pagination})
                return self.tag_page.render_to_response(node, request, extra_context=context)
        
        def tag_archive_view(self, request, node=None, extra_context=None):
index 0318e5e..e9415c0 100644 (file)
@@ -1,6 +1,37 @@
 from django.core.paginator import Paginator
 
 
+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
+       
+       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.
@@ -9,16 +40,16 @@ def paginate(objects, per_page=None, page_number=1):
                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
+               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 None, objects
+                       return PaginationProxy(objects=objects)
        except AttributeError:
                if len(objects) <= per_page:
-                       return None, objects
+                       return PaginationProxy(objects=objects)
        
        paginator = Paginator(objects, per_page)
        try:
@@ -26,8 +57,9 @@ def paginate(objects, per_page=None, page_number=1):
        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)
+       try:
+               page = paginator.page(page_number)
+       except EmptyPage:
+               page = None
        
-       return page, page.object_list
\ No newline at end of file
+       return PaginationProxy(paginator, page, objects)
\ No newline at end of file