summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorChris Beaven <smileychris@gmail.com>2012-11-21 14:58:14 +1300
committerChris Beaven <smileychris@gmail.com>2012-11-21 15:19:44 +1300
commitfbfa654a15487f9a49e9a5d34a4e9694fe878a8f (patch)
tree6c922e07069e20efc75344804f47bd71f30d26b0 /tests
parent8fdb28219df22245d7e50b4e8374d8567ca84f0f (diff)
Paginator._get_page hook
This allows for Paginator subclasses to easily override the Page class that gets used. Took the opportunity to also do some non-invasive PEP8 tidying of the paginator module.
Diffstat (limited to 'tests')
-rw-r--r--tests/regressiontests/pagination/custom.py20
-rw-r--r--tests/regressiontests/pagination/tests.py15
2 files changed, 35 insertions, 0 deletions
diff --git a/tests/regressiontests/pagination/custom.py b/tests/regressiontests/pagination/custom.py
new file mode 100644
index 0000000000..47a932c7e3
--- /dev/null
+++ b/tests/regressiontests/pagination/custom.py
@@ -0,0 +1,20 @@
+from django.core.paginator import Paginator, Page
+
+
+class ValidAdjacentNumsPage(Page):
+
+ def next_page_number(self):
+ if not self.has_next():
+ return None
+ return super(ValidAdjacentNumsPage, self).next_page_number()
+
+ def previous_page_number(self):
+ if not self.has_previous():
+ return None
+ return super(ValidAdjacentNumsPage, self).previous_page_number()
+
+
+class ValidAdjacentNumsPaginator(Paginator):
+
+ def _get_page(self, *args, **kwargs):
+ return ValidAdjacentNumsPage(*args, **kwargs)
diff --git a/tests/regressiontests/pagination/tests.py b/tests/regressiontests/pagination/tests.py
index 63ccd8f61c..b06bebf770 100644
--- a/tests/regressiontests/pagination/tests.py
+++ b/tests/regressiontests/pagination/tests.py
@@ -9,6 +9,7 @@ from django.utils import six
from django.utils import unittest
from .models import Article
+from .custom import ValidAdjacentNumsPaginator
class PaginationTests(unittest.TestCase):
@@ -217,6 +218,20 @@ class PaginationTests(unittest.TestCase):
self.assertEqual(''.join(page2), 'fghijk')
self.assertEqual(''.join(reversed(page2)), 'kjihgf')
+ def test_get_page_hook(self):
+ """
+ Tests that a Paginator subclass can use the ``_get_page`` hook to
+ return an alternative to the standard Page class.
+ """
+ eleven = 'abcdefghijk'
+ paginator = ValidAdjacentNumsPaginator(eleven, per_page=6)
+ page1 = paginator.page(1)
+ page2 = paginator.page(2)
+ self.assertEquals(page1.previous_page_number(), None)
+ self.assertEquals(page1.next_page_number(), 2)
+ self.assertEquals(page2.previous_page_number(), 1)
+ self.assertEquals(page2.next_page_number(), None)
+
class ModelPaginationTests(TestCase):
"""