Ticket #17159: ticket17159-v2.diff

File ticket17159-v2.diff, 3.9 KB (added by neaf, 4 years ago)

Previous patch with tests and documentation update.

  • django/core/paginator.py

    diff --git a/django/core/paginator.py b/django/core/paginator.py
    index 8b4d289..6b0b354 100644
    a b class Page(object): 
    132132        return self.has_previous() or self.has_next()
    133133
    134134    def next_page_number(self):
    135         return self.number + 1
     135        return self.paginator.validate_number(self.number + 1)
    136136
    137137    def previous_page_number(self):
    138         return self.number - 1
     138        return self.paginator.validate_number(self.number - 1)
    139139
    140140    def start_index(self):
    141141        """
  • docs/topics/pagination.txt

    diff --git a/docs/topics/pagination.txt b/docs/topics/pagination.txt
    index 566319f..e00be31 100644
    a b Methods 
    253253
    254254.. method:: Page.next_page_number()
    255255
    256     Returns the next page number. Note that this is "dumb" and will return the
    257     next page number regardless of whether a subsequent page exists.
     256    Returns the next page number.
     257    Raises :exc:`InvalidPage` if next page doesn't exist.
    258258
    259259.. method:: Page.previous_page_number()
    260260
    261     Returns the previous page number. Note that this is "dumb" and will return
    262     the previous page number regardless of whether a previous page exists.
     261    Returns the previous page number.
     262    Raises :exc:`InvalidPage` if previous page doesn't exist.
    263263
    264264.. method:: Page.start_index()
    265265
  • tests/modeltests/pagination/tests.py

    diff --git a/tests/modeltests/pagination/tests.py b/tests/modeltests/pagination/tests.py
    index b7e217e..57cd3b4 100644
    a b class PaginationTests(TestCase): 
    4545        self.assertFalse(p.has_previous())
    4646        self.assertTrue(p.has_other_pages())
    4747        self.assertEqual(2, p.next_page_number())
    48         self.assertEqual(0, p.previous_page_number())
     48        self.assertRaises(InvalidPage, p.previous_page_number)
    4949        self.assertEqual(1, p.start_index())
    5050        self.assertEqual(5, p.end_index())
    5151
    class PaginationTests(TestCase): 
    6363        self.assertFalse(p.has_next())
    6464        self.assertTrue(p.has_previous())
    6565        self.assertTrue(p.has_other_pages())
    66         self.assertEqual(3, p.next_page_number())
     66        self.assertRaises(InvalidPage, p.next_page_number)
    6767        self.assertEqual(1, p.previous_page_number())
    6868        self.assertEqual(6, p.start_index())
    6969        self.assertEqual(9, p.end_index())
    class PaginationTests(TestCase): 
    104104
    105105    def test_paginate_list(self):
    106106        # Paginators work with regular lists/tuples, too -- not just with QuerySets.
    107         paginator = Paginator([1, 2, 3, 4, 5, 6, 7, 8, 9], 5)
     107        paginator = Paginator([1, 2, 3, 4, 5, 6, 7, 8, 9], 3)
    108108        self.assertEqual(9, paginator.count)
    109         self.assertEqual(2, paginator.num_pages)
    110         self.assertEqual([1, 2], paginator.page_range)
    111         p = paginator.page(1)
    112         self.assertEqual(u"<Page 1 of 2>", unicode(p))
    113         self.assertEqual([1, 2, 3, 4, 5], p.object_list)
     109        self.assertEqual(3, paginator.num_pages)
     110        self.assertEqual([1, 2, 3], paginator.page_range)
     111        p = paginator.page(2)
     112        self.assertEqual(u"<Page 2 of 3>", unicode(p))
     113        self.assertEqual([4, 5, 6], p.object_list)
    114114        self.assertTrue(p.has_next())
    115         self.assertFalse(p.has_previous())
     115        self.assertTrue(p.has_previous())
    116116        self.assertTrue(p.has_other_pages())
    117         self.assertEqual(2, p.next_page_number())
    118         self.assertEqual(0, p.previous_page_number())
    119         self.assertEqual(1, p.start_index())
    120         self.assertEqual(5, p.end_index())
     117        self.assertEqual(1, p.previous_page_number())
     118        self.assertEqual(3, p.next_page_number())
     119        self.assertEqual(4, p.start_index())
     120        self.assertEqual(6, p.end_index())
    121121
    122122    def test_paginate_misc_classes(self):
    123123        # Paginator can be passed other objects with a count() method.
Back to Top