diff --git a/.gitignore b/.gitignore index cc747adf4..86a87a7d3 100644 --- a/.gitignore +++ b/.gitignore @@ -78,3 +78,4 @@ target/ # specific to this project static bower +bower_components diff --git a/sapl/crud.py b/sapl/crud.py index 1d6f9b9b5..1032a5cc1 100644 --- a/sapl/crud.py +++ b/sapl/crud.py @@ -10,6 +10,30 @@ from django.views.generic import ( 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): field = obj._meta.get_field(fieldname) verbose_name = str(field.verbose_name) @@ -81,9 +105,11 @@ def build_crud(model, *layout): def get_context_data(self, **kwargs): context_data = super(CrudListView, self).get_context_data(**kwargs) paginator = context_data['paginator'] + 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'] = paginator.page_range + context_data['custom_page_range'] = makePagination( + current_page.number, paginator.num_pages) return context_data class CrudCreateView(BaseMixin, FormMessagesMixin, CreateView): diff --git a/sapl/test_crud.py b/sapl/test_crud.py index 538905c88..f0de6b106 100644 --- a/sapl/test_crud.py +++ b/sapl/test_crud.py @@ -3,7 +3,7 @@ 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 +from .crud import get_field_display, build_crud, makePagination pytestmark = pytest.mark.django_db @@ -11,6 +11,17 @@ pytestmark = pytest.mark.django_db # 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(): stub = mommy.prepare(Comissao, unidade_deliberativa=True) assert get_field_display(stub, 'nome')[1] == stub.nome