From c016f5227b91e585a66654ee99bd58f1761a1932 Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Thu, 23 Jul 2015 16:13:04 -0300 Subject: [PATCH] Add fields display to DetailView in crud --- sapl/crud.py | 30 ++++++++++ sapl/test_crud.py | 75 ++++++++++++++++++++++++ templates/comissoes/comissao_detail.html | 20 ++++++- 3 files changed, 124 insertions(+), 1 deletion(-) diff --git a/sapl/crud.py b/sapl/crud.py index 7cc675400..e352290ea 100644 --- a/sapl/crud.py +++ b/sapl/crud.py @@ -10,6 +10,17 @@ from django.views.generic import ( from sapl.layout import SaplFormLayout +def get_field_display(obj, fieldname): + field = obj._meta.get_field(fieldname) + verbose_name = str(field.verbose_name) + if field.choices: + value = getattr(obj, 'get_%s_display' % fieldname)() + else: + value = getattr(obj, fieldname) + display = '' if value is None else str(value) + return verbose_name, display + + class Crud(object): pass @@ -85,6 +96,25 @@ def build_crud(model, *layout): def title(self): return self.get_object() + def get_column(self, fieldname, span): + obj = self.get_object() + verbose_name, text = get_field_display(obj, fieldname) + return { + 'id': fieldname, + 'span': span, + 'verbose_name': verbose_name, + 'text': text, + } + + @property + def fieldsets(self): + return [ + {'legend': legend, + 'rows': [[self.get_column(fieldname, span) + for fieldname, span in row] + for row in rows] + } for legend, *rows in layout] + class CrudUpdateView(BaseMixin, FormMessagesMixin, UpdateView): form_class = crud.model_form form_valid_message = _('Registro alterado com sucesso!') diff --git a/sapl/test_crud.py b/sapl/test_crud.py index 17e1d4d8c..538905c88 100644 --- a/sapl/test_crud.py +++ b/sapl/test_crud.py @@ -3,12 +3,87 @@ 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 +pytestmark = pytest.mark.django_db # XXX These tests are based on comissoes app # but could be done with a stub one +def test_get_field_display(): + stub = mommy.prepare(Comissao, unidade_deliberativa=True) + assert get_field_display(stub, 'nome')[1] == stub.nome + assert get_field_display(stub, 'tipo')[1] == str(stub.tipo) + # must return choice display, not the value + assert stub.unidade_deliberativa is True + assert get_field_display(stub, 'unidade_deliberativa')[1] == 'Sim' + + # None is displayed as empty string + assert stub.finalidade is None + assert get_field_display(stub, 'finalidade')[1] == '' + + +def test_crud_detail_view_fieldsets(monkeypatch): + + crud = build_crud( + Comissao, + + ['Dados Básicos', + [('nome', 9), ('sigla', 3)], + [('tipo', 3), ('data_criacao', 3), ('unidade_deliberativa', 3), ] + ], + + ['Dados Complementares', + [('finalidade', 12)] + ], + ) + + view = crud.CrudDetailView() + stub = mommy.make(Comissao, + nome='nome!!!', + tipo__nome='tipo!!!', + sigla='sigla!!!', + data_criacao='2011-01-01', + unidade_deliberativa=True) + + # to test None displayed as empty string + assert stub.finalidade is None + + def get_object(): + return stub + monkeypatch.setattr(view, 'get_object', get_object) + + assert view.fieldsets == [ + {'legend': 'Dados Básicos', + 'rows': [[{'id': 'nome', + 'span': 9, + 'text': 'nome!!!', + 'verbose_name': 'Nome'}, + {'id': 'sigla', + 'span': 3, + 'text': 'sigla!!!', + 'verbose_name': 'Sigla'}], + + [{'id': 'tipo', + 'span': 3, + 'text': 'tipo!!!', + 'verbose_name': 'Tipo'}, + {'id': 'data_criacao', + 'span': 3, + 'text': '2011-01-01', + 'verbose_name': 'Data de Criação'}, + {'id': 'unidade_deliberativa', + 'span': 3, + 'text': 'Sim', + 'verbose_name': 'unidade deliberativa'}]]}, + {'legend': 'Dados Complementares', + 'rows': [[{'id': 'finalidade', + 'span': 12, + 'text': '', + 'verbose_name': 'Finalidade'}]]}] + + def test_reverse(): assert '/comissoes/' == reverse('comissao:list') assert '/comissoes/create' == reverse('comissao:create') diff --git a/templates/comissoes/comissao_detail.html b/templates/comissoes/comissao_detail.html index 61904074f..5e54ee75b 100644 --- a/templates/comissoes/comissao_detail.html +++ b/templates/comissoes/comissao_detail.html @@ -13,6 +13,24 @@ - TODO ... + {# TODO replace fieldset for something semantically correct, but with similar visual grouping style #} + {% for fieldset in view.fieldsets %} +
+ {{ fieldset.legend }} + {% for row in fieldset.rows %} +
+ {% for column in row %} +
+
+ {# TODO replace labels, probably (are they correct here?) #} +

{{ column.text }}

+
+
+ {% endfor %} +
+ {% endfor %} +
+ {% endfor %} + {% endblock %}