Browse Source

Add custom pagination

pull/6/head
Eduardo Edson Batista Cordeiro Alves 10 years ago
parent
commit
a9458e3e14
  1. 1
      .gitignore
  2. 28
      sapl/crud.py
  3. 13
      sapl/test_crud.py

1
.gitignore

@ -78,3 +78,4 @@ target/
# specific to this project # specific to this project
static static
bower bower
bower_components

28
sapl/crud.py

@ -10,6 +10,30 @@ from django.views.generic import (
from sapl.layout import SaplFormLayout from sapl.layout import SaplFormLayout
def makePagination(current_page, total_pages):
current = current_page
total = total_pages
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]
else:
return [1, 2, None, current - 1, current, current + 1,
None, total - 1, total]
else:
pages = []
i = 1
while i <= total:
pages.append(i)
i = i + 1
return pages
def get_field_display(obj, fieldname): def get_field_display(obj, fieldname):
field = obj._meta.get_field(fieldname) field = obj._meta.get_field(fieldname)
verbose_name = str(field.verbose_name) verbose_name = str(field.verbose_name)
@ -81,9 +105,11 @@ def build_crud(model, *layout):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context_data = super(CrudListView, self).get_context_data(**kwargs) context_data = super(CrudListView, self).get_context_data(**kwargs)
paginator = context_data['paginator'] paginator = context_data['paginator']
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'] = paginator.page_range context_data['custom_page_range'] = makePagination(
current_page.number, paginator.num_pages)
return context_data return context_data
class CrudCreateView(BaseMixin, FormMessagesMixin, CreateView): class CrudCreateView(BaseMixin, FormMessagesMixin, CreateView):

13
sapl/test_crud.py

@ -3,7 +3,7 @@ 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 from .crud import get_field_display, build_crud, makePagination
pytestmark = pytest.mark.django_db pytestmark = pytest.mark.django_db
@ -11,6 +11,17 @@ pytestmark = pytest.mark.django_db
# but could be done with a stub one # 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]
def test_get_field_display(): def test_get_field_display():
stub = mommy.prepare(Comissao, unidade_deliberativa=True) stub = mommy.prepare(Comissao, unidade_deliberativa=True)
assert get_field_display(stub, 'nome')[1] == stub.nome assert get_field_display(stub, 'nome')[1] == stub.nome

Loading…
Cancel
Save