From a8f96d11803bfa2847cee56e6f36a3f3007532b3 Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Fri, 24 Jul 2015 18:12:44 -0300 Subject: [PATCH] Start generic tabular display in crud ListView --- sapl/crud.py | 9 +++++++++ sapl/test_crud.py | 19 ++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/sapl/crud.py b/sapl/crud.py index aaa037a7c..f67825f7b 100644 --- a/sapl/crud.py +++ b/sapl/crud.py @@ -115,6 +115,15 @@ def build_crud(model, *layout): title = BaseMixin.verbose_name_plural paginate_by = 10 + def get_fieldnames(self): + '''The list of field names to display on table + + This base implementation returns the field names + in the first fieldset of the layout. + ''' + rows = layout[0][1:] + return [fieldname for row in rows for fieldname, __ in row] + def get_context_data(self, **kwargs): context_data = super(CrudListView, self).get_context_data(**kwargs) paginator = context_data['paginator'] diff --git a/sapl/test_crud.py b/sapl/test_crud.py index e2d8b9476..dbe5d2661 100644 --- a/sapl/test_crud.py +++ b/sapl/test_crud.py @@ -10,6 +10,23 @@ pytestmark = pytest.mark.django_db # XXX These tests are based on comissoes app # but could be done with a stub one + +@pytest.mark.parametrize("layout, result", [ + ([['Dados Complementares']], []), # missing rows definition + ([ + ['Dados Básicos', + [('nome', 9), ('sigla', 3)], + [('tipo', 3), ('data_criacao', 3), ('unidade_deliberativa', 3), ] + ], + ['Dados Complementares', [('finalidade', 12)]], ], + ['nome', 'sigla', 'tipo', 'data_criacao', 'unidade_deliberativa']), +]) +def test_listview_get_fieldnames(layout, result): + crud = build_crud(Comissao, *layout) + view = crud.CrudListView() + assert view.get_fieldnames() == result + + __ = None # for test readability @@ -49,7 +66,7 @@ __ = None # for test readability (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): +def test_make_pagination(index, num_pages, result): assert num_pages < 10 or len(result) == 10 assert make_pagination(index, num_pages) == result