Ticket #11596: 11596.2.diff

File 11596.2.diff, 2.0 KB (added by SmileyChris, 5 years ago)
  • django/core/paginator.py

    diff --git a/django/core/paginator.py b/django/core/paginator.py
    index 495cdf2..479cb49 100644
    a b  
    11from math import ceil
     2import collections
    23
    34class InvalidPage(Exception):
    45    pass
    class Paginator(object): 
    7576
    7677QuerySetPaginator = Paginator # For backwards-compatibility.
    7778
    78 class Page(object):
     79class Page(collections.Sequence):
    7980    def __init__(self, object_list, number, paginator):
    8081        self.object_list = object_list
    8182        self.number = number
    class Page(object): 
    8485    def __repr__(self):
    8586        return '<Page %s of %s>' % (self.number, self.paginator.num_pages)
    8687
     88    def __len__(self):
     89        return len(self.object_list)
     90
     91    def __getitem__(self, index):
     92        # The object_list is converted to a list so that if it was a QuerySet
     93        # it won't be a database hit per __getitem__.
     94        return list(self.object_list)[index]
     95
    8796    def has_next(self):
    8897        return self.number < self.paginator.num_pages
    8998
  • tests/regressiontests/pagination_regress/tests.py

    diff --git a/tests/regressiontests/pagination_regress/tests.py b/tests/regressiontests/pagination_regress/tests.py
    index 28fe316..1cddd43 100644
    a b class PaginatorTests(TestCase): 
    154154        self.assertRaises(EmptyPage, self.check_indexes, ([], 4, 0, False), 1, None)
    155155        self.assertRaises(EmptyPage, self.check_indexes, ([], 4, 1, False), 1, None)
    156156        self.assertRaises(EmptyPage, self.check_indexes, ([], 4, 2, False), 1, None)
     157
     158    def test_page_sequence(self):
     159        """
     160        Tests that a paginator page acts like a standard sequence.
     161        """
     162        eleven = 'abcdefghijk'
     163        page2 = Paginator(eleven, per_page=5, orphans=1).page(2)
     164        self.assertEqual(len(page2), 6)
     165        self.assertTrue('k' in page2)
     166        self.assertFalse('a' in page2)
     167        self.assertEqual(''.join(page2), 'fghijk')
Back to Top