From 3658c7516e30f3221592e1029aa0b115305a76f9 Mon Sep 17 00:00:00 2001 From: Paul Grau Date: Thu, 28 Jul 2016 20:21:13 +0200 Subject: [PATCH] Fixes #354 - Restore default pagination for SingleTableMixin - Add more test cases for pagination in views - Fix get_table_pagination to exhibit the old 1.2.2 behavior --- django_tables2/views.py | 9 ++++--- tests/test_views.py | 59 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/django_tables2/views.py b/django_tables2/views.py index 01077977..ebdaa27e 100644 --- a/django_tables2/views.py +++ b/django_tables2/views.py @@ -39,13 +39,17 @@ def get_table_pagination(self, table): Returns pagination options: True for standard pagination (default), False for no pagination, and a dictionary for custom pagination. ''' - paginate = self.table_pagination or {} + paginate = self.table_pagination if hasattr(self, 'paginate_by') and self.paginate_by is not None: # Since ListView knows the concept paginate_by, we use that if no # other pagination is configured. + paginate = paginate or {} paginate['per_page'] = self.paginate_by + if paginate is None: + return True + return paginate @@ -61,7 +65,7 @@ class SingleTableMixin(TableMixinBase): 'table') table_pagination (dict): controls table pagination. If a `dict`, passed as the *paginate* keyword argument to `.RequestConfig`. As such, any - Truthy value enables pagination. + Truthy value enables pagination. (default: enable pagination) This mixin plays nice with the Django's`.MultipleObjectMixin` by using `.get_queryset`` as a fallback for the table data source. @@ -76,7 +80,6 @@ def get_table(self, **kwargs): ''' table_class = self.get_table_class() table = table_class(self.get_table_data(), **kwargs) - RequestConfig(self.request, paginate=self.get_table_pagination(table)).configure(table) return table diff --git a/tests/test_views.py b/tests/test_views.py index c725f33f..ca6a5e18 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -30,6 +30,11 @@ class Meta: class SimpleView(DispatchHookMixin, tables.SingleTableView): + table_class = SimpleTable + model = Region # required for ListView + + +class SimplePaginatedView(DispatchHookMixin, tables.SingleTableView): table_class = SimpleTable table_pagination = {'per_page': 1} model = Region # required for ListView @@ -40,8 +45,53 @@ def test_view_should_support_pagination_options(): for region in MEMORY_DATA: Region.objects.create(name=region['name']) - response, view = SimpleView.as_view()(build_request('/')) + response, view = SimplePaginatedView.as_view()(build_request('/')) assert view.get_table().paginator.num_pages == len(MEMORY_DATA) + assert view.get_table().paginator.per_page == 1 + + +@pytest.mark.django_db +def test_view_should_support_default_pagination(): + class PaginateDefault(DispatchHookMixin, tables.SingleTableView): + table_class = SimpleTable + model = Region + table_data = MEMORY_DATA + + response, view = PaginateDefault.as_view()(build_request('/')) + table = view.get_table() + assert table.paginator.per_page == 25 + assert len(table.page) == 4 + + +@pytest.mark.django_db +def test_view_should_support_default_pagination_with_table_options(): + class Table(tables.Table): + class Meta: + model = Region + per_page = 2 + + class PaginateByDefinedOnView(DispatchHookMixin, tables.SingleTableView): + table_class = Table + model = Region + table_data = MEMORY_DATA + + response, view = PaginateByDefinedOnView.as_view()(build_request('/')) + table = view.get_table() + assert table.paginator.per_page == 2 + assert len(table.page) == 2 + + +@pytest.mark.django_db +def test_view_should_support_disabling_pagination_options(): + class SimpleNotPaginatedView(DispatchHookMixin, tables.SingleTableView): + table_class = SimpleTable + table_data = MEMORY_DATA + table_pagination = False + model = Region # required for ListView + + response, view = SimpleNotPaginatedView.as_view()(build_request('/')) + table = view.get_table() + assert not hasattr(table, 'page') @pytest.mark.django_db @@ -69,7 +119,7 @@ class WithoutTableclassView(tables.SingleTableView): def test_should_support_explicit_table_data(): - class ExplicitDataView(SimpleView): + class ExplicitDataView(SimplePaginatedView): table_data = MEMORY_DATA response, view = ExplicitDataView.as_view()(build_request('/')) @@ -86,11 +136,10 @@ class PaginateByDefinedOnView(DispatchHookMixin, tables.SingleTableView): table_class = Table model = Region paginate_by = 2 - table_data = MEMORY_DATA response, view = PaginateByDefinedOnView.as_view()(build_request('/')) - assert view.get_table().paginator.num_pages == 2 + assert view.get_table().paginator.per_page == 2 @pytest.mark.django_db @@ -132,7 +181,7 @@ def get_table_pagination(self, table): return super(PaginationOverrideView, self).get_table_pagination(table) response, view = PaginationOverrideView.as_view()(build_request('/?p_per_page_override=2')) - assert view.get_table().paginator.num_pages == 2 + assert view.get_table().paginator.per_page == 2 def test_singletablemixin_with_non_paginated_view():