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

54
sapl/test_crud.py

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

Loading…
Cancel
Save