Skip to content

Commit 3758c3b

Browse files
committed
Fixed a crash when you used a custom paginator in django 2.0+
1 parent dded0c1 commit 3758c3b

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

HISTORY.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
Changelog
22
---------
33

4-
6.2.0 (2019-02-18)
4+
6.2.1 (2019-03-05)
5+
~~~~~~~~~~~~~~~~~~
6+
7+
* Fixed a crash when you used a custom paginator in django 2.0+
8+
9+
10+
6.2.0 (2019-03-04)
511
~~~~~~~~~~~~~~~~~~
612

713
* Fixes for jinja2 compatibility (still not fully working)

lib/tri/table/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
from tri.table.db_compat import setup_db_compat
2828

29-
__version__ = '6.2.0' # pragma: no mutate
29+
__version__ = '6.2.1' # pragma: no mutate
3030

3131
LAST = LAST
3232

@@ -1473,15 +1473,11 @@ def table_context(request,
14731473
pass
14741474
if paginator is None:
14751475
paginator = Paginator(table.data, paginate_by)
1476-
object_list = None
1477-
else: # pragma: no cover
1478-
object_list = table.data
14791476
if not page:
14801477
page = request.GET.get('page') # None is translated to the default page in paginator.get_page
14811478
try:
14821479
page_obj = paginator.get_page(page)
1483-
if object_list is None:
1484-
table.data = page_obj.object_list
1480+
table.data = page_obj.object_list
14851481
except (InvalidPage, ValueError): # pragma: no cover
14861482
raise Http404
14871483

tests/test_tri_table.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import json
66

7+
import django
78
from django.db.models import QuerySet
89
from django.http import HttpResponse
910
from django.template import Template
@@ -683,6 +684,49 @@ class TestTable(Table):
683684
</table>""")
684685

685686

687+
@pytest.mark.skipif(django.VERSION[0] < 2, reason='This requires the new paginator API in django 2.0+')
688+
@pytest.mark.django_db
689+
def test_django_table_pagination_custom_paginator():
690+
691+
for x in range(30):
692+
Foo(a=x, b="foo").save()
693+
694+
class TestTable(Table):
695+
a = Column.number(sortable=False) # turn off sorting to not get the link with random query params
696+
b = Column(show=False) # should still be able to filter on this though!
697+
698+
from django.core.paginator import Paginator
699+
700+
class CustomPaginator(Paginator):
701+
def __init__(self, object_list):
702+
super(CustomPaginator, self).__init__(object_list=object_list, per_page=2)
703+
704+
def get_page(self, number):
705+
del number
706+
return self.page(2)
707+
708+
data = Foo.objects.all().order_by('pk')
709+
verify_table_html(
710+
table=TestTable(data=data),
711+
paginator=CustomPaginator(data),
712+
expected_html="""
713+
<table class="listview">
714+
<thead>
715+
<tr>
716+
<th class="first_column subheader"> A </th>
717+
</tr>
718+
</thead>
719+
<tbody>
720+
<tr data-pk="3">
721+
<td class="rj"> 2 </td>
722+
</tr>
723+
<tr data-pk="4">
724+
<td class="rj"> 3 </td>
725+
</tr>
726+
</tbody>
727+
</table>""")
728+
729+
686730
def test_links():
687731
class TestTable(NoSortTable):
688732
foo = Column(header__attrs__title="Some title")

0 commit comments

Comments
 (0)