Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #354 - Restore default pagination for SingleTableMixin #359

Merged
merged 1 commit into from
Jul 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions django_tables2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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.
Expand All @@ -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

Expand Down
59 changes: 54 additions & 5 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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('/'))
Expand All @@ -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
Expand Down Expand Up @@ -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():
Expand Down