Example #1
0
    def test_finite_paginator_2(self):
        p = FinitePaginator(range(20), 20, offset=10, link_template='/bacon/page/%d')

        p2 = p.page(2)
        self.assertEqual(repr(p2), u'<Page 2>')

        self.assertFalse(p2.has_next())
        self.assertTrue(p2.has_previous())

        self.assertEqual(p2.next_link(), None)
        self.assertEqual(p2.previous_link(), '/bacon/page/1')
Example #2
0
    def test_finite_paginator_1(self):
        p = FinitePaginator(range(20), 2, offset=10, link_template='/bacon/page/%d')
        self.assertEqual(p.validate_number(2), 2)
        self.assertEqual(p.orphans, 0)

        p3 = p.page(3)
        self.assertEqual(repr(p3), u'<Page 3>')
        self.assertEqual(p3.start_index(), 10)
        self.assertEqual(p3.end_index(), 6)

        self.assertTrue(p3.has_next())
        self.assertTrue(p3.has_previous())

        self.assertEqual(p3.next_link(), '/bacon/page/4')
        self.assertEqual(p3.previous_link(), '/bacon/page/2')
Example #3
0
 def render(self, context):
     key = self.queryset_var.var
     value = self.queryset_var.resolve(context)
     if isinstance(self.paginate_by, int):
         paginate_by = self.paginate_by
     else:
         paginate_by = self.paginate_by.resolve(context)
     page_num = context['request'].page
     if self.style in ('standard', 'backwards'):
         paginator = Paginator(value, paginate_by, self.orphans)
         if page_num is None and self.style == 'backwards':
             page_num = paginator.num_pages
     elif self.style == 'infinite':
         paginator = InfinitePaginator(value, paginate_by, self.orphans)
     elif self.style == 'finite':
         paginator = FinitePaginator(value, paginate_by)
     if page_num is None:
         page_num = 1
     try:
         page_obj = paginator.page(page_num)
     except InvalidPage:
         if INVALID_PAGE_RAISES_404:
             raise Http404(
                 'Invalid page requested.  If DEBUG were set to ' +
                 'False, an HTTP 404 page would have been shown instead.')
         context[key] = []
         context['invalid_page'] = True
         return u''
     if self.context_var is not None:
         context[self.context_var] = page_obj.object_list
     else:
         context[key] = page_obj.object_list
     context['paginator'] = paginator
     context['page_obj'] = page_obj
     context['style'] = self.style
     return u''