Code

Ticket #17159: ticket17159-v2.diff

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

Previous patch with tests and documentation update.

Line 
1diff --git a/django/core/paginator.py b/django/core/paginator.py
2index 8b4d289..6b0b354 100644
3--- a/django/core/paginator.py
4+++ b/django/core/paginator.py
5@@ -132,10 +132,10 @@ class Page(object):
6         return self.has_previous() or self.has_next()
7 
8     def next_page_number(self):
9-        return self.number + 1
10+        return self.paginator.validate_number(self.number + 1)
11 
12     def previous_page_number(self):
13-        return self.number - 1
14+        return self.paginator.validate_number(self.number - 1)
15 
16     def start_index(self):
17         """
18diff --git a/docs/topics/pagination.txt b/docs/topics/pagination.txt
19index 566319f..e00be31 100644
20--- a/docs/topics/pagination.txt
21+++ b/docs/topics/pagination.txt
22@@ -253,13 +253,13 @@ Methods
23 
24 .. method:: Page.next_page_number()
25 
26-    Returns the next page number. Note that this is "dumb" and will return the
27-    next page number regardless of whether a subsequent page exists.
28+    Returns the next page number.
29+    Raises :exc:`InvalidPage` if next page doesn't exist.
30 
31 .. method:: Page.previous_page_number()
32 
33-    Returns the previous page number. Note that this is "dumb" and will return
34-    the previous page number regardless of whether a previous page exists.
35+    Returns the previous page number.
36+    Raises :exc:`InvalidPage` if previous page doesn't exist.
37 
38 .. method:: Page.start_index()
39 
40diff --git a/tests/modeltests/pagination/tests.py b/tests/modeltests/pagination/tests.py
41index b7e217e..57cd3b4 100644
42--- a/tests/modeltests/pagination/tests.py
43+++ b/tests/modeltests/pagination/tests.py
44@@ -45,7 +45,7 @@ class PaginationTests(TestCase):
45         self.assertFalse(p.has_previous())
46         self.assertTrue(p.has_other_pages())
47         self.assertEqual(2, p.next_page_number())
48-        self.assertEqual(0, p.previous_page_number())
49+        self.assertRaises(InvalidPage, p.previous_page_number)
50         self.assertEqual(1, p.start_index())
51         self.assertEqual(5, p.end_index())
52 
53@@ -63,7 +63,7 @@ class PaginationTests(TestCase):
54         self.assertFalse(p.has_next())
55         self.assertTrue(p.has_previous())
56         self.assertTrue(p.has_other_pages())
57-        self.assertEqual(3, p.next_page_number())
58+        self.assertRaises(InvalidPage, p.next_page_number)
59         self.assertEqual(1, p.previous_page_number())
60         self.assertEqual(6, p.start_index())
61         self.assertEqual(9, p.end_index())
62@@ -104,20 +104,20 @@ class PaginationTests(TestCase):
63 
64     def test_paginate_list(self):
65         # Paginators work with regular lists/tuples, too -- not just with QuerySets.
66-        paginator = Paginator([1, 2, 3, 4, 5, 6, 7, 8, 9], 5)
67+        paginator = Paginator([1, 2, 3, 4, 5, 6, 7, 8, 9], 3)
68         self.assertEqual(9, paginator.count)
69-        self.assertEqual(2, paginator.num_pages)
70-        self.assertEqual([1, 2], paginator.page_range)
71-        p = paginator.page(1)
72-        self.assertEqual(u"<Page 1 of 2>", unicode(p))
73-        self.assertEqual([1, 2, 3, 4, 5], p.object_list)
74+        self.assertEqual(3, paginator.num_pages)
75+        self.assertEqual([1, 2, 3], paginator.page_range)
76+        p = paginator.page(2)
77+        self.assertEqual(u"<Page 2 of 3>", unicode(p))
78+        self.assertEqual([4, 5, 6], p.object_list)
79         self.assertTrue(p.has_next())
80-        self.assertFalse(p.has_previous())
81+        self.assertTrue(p.has_previous())
82         self.assertTrue(p.has_other_pages())
83-        self.assertEqual(2, p.next_page_number())
84-        self.assertEqual(0, p.previous_page_number())
85-        self.assertEqual(1, p.start_index())
86-        self.assertEqual(5, p.end_index())
87+        self.assertEqual(1, p.previous_page_number())
88+        self.assertEqual(3, p.next_page_number())
89+        self.assertEqual(4, p.start_index())
90+        self.assertEqual(6, p.end_index())
91 
92     def test_paginate_misc_classes(self):
93         # Paginator can be passed other objects with a count() method.