Browse Source

Show 10 pages at most in pagination

pull/6/head
Marcio Mazza 10 years ago
parent
commit
fa15600c17
  1. 36
      sapl/crud.py
  2. 54
      sapl/test_crud.py

36
sapl/crud.py

@ -10,28 +10,26 @@ from django.views.generic import (
from sapl.layout import SaplFormLayout
def makePagination(current_page, total_pages):
current = current_page
total = total_pages
def from_to(start, end):
return list(range(start, end + 1))
if total_pages > 10:
if current <= 3:
return [1, 2, 3, 4, 5, None, total - 1, total]
elif current >= total - 3:
return [1, 2, None, total - 4, total - 3,
total - 2, total - 1, total]
def make_pagination(index, num_pages):
PAGINATION_LENGTH = 10
if num_pages <= PAGINATION_LENGTH:
return from_to(1, num_pages)
else:
return [1, 2, None, current - 1, current, current + 1,
None, total - 1, total]
if index - 1 <= 5:
tail = [num_pages - 1, num_pages]
head = from_to(1, PAGINATION_LENGTH - 3)
else:
if index + 1 >= num_pages - 3:
tail = from_to(index - 1, num_pages)
else:
pages = []
i = 1
while i <= total:
pages.append(i)
i = i + 1
return pages
tail = [index - 1, index, index + 1,
None, num_pages - 1, num_pages]
head = from_to(1, PAGINATION_LENGTH - len(tail) - 1)
return head + [None] + tail
def get_field_display(obj, fieldname):
@ -108,7 +106,7 @@ def build_crud(model, *layout):
current_page = context_data['page_obj']
# TODO set custom_page_range to something like
# [1, 2, None, 10, 11, 12, None, 29, 30]
context_data['custom_page_range'] = makePagination(
context_data['custom_page_range'] = make_pagination(
current_page.number, paginator.num_pages)
return context_data

54
sapl/test_crud.py

@ -3,23 +3,55 @@ from django.core.urlresolvers import reverse
from model_mommy import mommy
from comissoes.models import Comissao, TipoComissao
from .crud import get_field_display, build_crud, makePagination
from .crud import get_field_display, build_crud, make_pagination, from_to
pytestmark = pytest.mark.django_db
# XXX These tests are based on comissoes app
# but could be done with a stub one
def test_pagination():
assert makePagination(1, 1) == [1]
assert makePagination(1, 10) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
assert makePagination(3, 5) == [1, 2, 3, 4, 5]
assert makePagination(9, 11) == [1, 2, None, 7, 8, 9, 10, 11]
assert makePagination(8, 22) == [1, 2, None, 7, 8, 9, None, 21, 22]
assert makePagination(2, 5) == [1, 2, 3, 4, 5]
assert makePagination(1, 17) == [1, 2, 3, 4, 5, None, 16, 17]
assert makePagination(22, 25) == [1, 2, None, 21, 22, 23, 24, 25]
__ = None # for test readability
@pytest.mark.parametrize(
"index, num_pages, result",
[(i, k, from_to(1, k))
for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for k in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
] + [
(11, 11, [1, 2, 3, 4, 5, 6, 7, __, 10, (11)]),
(10, 11, [1, 2, 3, 4, 5, 6, __, 9, (10), 11]),
(9, 11, [1, 2, 3, 4, 5, __, 8, (9), 10, 11]),
(8, 11, [1, 2, 3, 4, __, 7, (8), 9, 10, 11]),
(7, 11, [1, 2, 3, __, 6, (7), 8, 9, 10, 11]),
(6, 11, [1, 2, 3, 4, 5, (6), 7, __, 10, 11]),
(5, 11, [1, 2, 3, 4, (5), 6, 7, __, 10, 11]),
(4, 11, [1, 2, 3, (4), 5, 6, 7, __, 10, 11]),
(3, 11, [1, 2, (3), 4, 5, 6, 7, __, 10, 11]),
(2, 11, [1, (2), 3, 4, 5, 6, 7, __, 10, 11]),
(1, 11, [(1), 2, 3, 4, 5, 6, 7, __, 10, 11]),
(12, 12, [1, 2, 3, 4, 5, 6, 7, __, 11, (12)]),
(11, 12, [1, 2, 3, 4, 5, 6, __, 10, (11), 12]),
(10, 12, [1, 2, 3, 4, 5, __, 9, (10), 11, 12]),
(9, 12, [1, 2, 3, 4, __, 8, (9), 10, 11, 12]),
(8, 12, [1, 2, 3, __, 7, (8), 9, 10, 11, 12]),
(7, 12, [1, 2, 3, __, 6, (7), 8, __, 11, 12]),
(6, 12, [1, 2, 3, 4, 5, (6), 7, __, 11, 12]),
(5, 12, [1, 2, 3, 4, (5), 6, 7, __, 11, 12]),
(4, 12, [1, 2, 3, (4), 5, 6, 7, __, 11, 12]),
(3, 12, [1, 2, (3), 4, 5, 6, 7, __, 11, 12]),
(2, 12, [1, (2), 3, 4, 5, 6, 7, __, 11, 12]),
(1, 12, [(1), 2, 3, 4, 5, 6, 7, __, 11, 12]),
# some random entries
(8, 22, [1, 2, 3, __, 7, (8), 9, __, 21, 22]),
(1, 17, [(1), 2, 3, 4, 5, 6, 7, __, 16, 17]),
(22, 25, [1, 2, 3, 4, __, 21, (22), 23, 24, 25]),
])
def test_pagination(index, num_pages, result):
assert num_pages < 10 or len(result) == 10
assert make_pagination(index, num_pages) == result
def test_get_field_display():

Loading…
Cancel
Save