Browse Source

Add fields display to DetailView in crud

pull/6/head
Marcio Mazza 10 years ago
parent
commit
c016f5227b
  1. 30
      sapl/crud.py
  2. 75
      sapl/test_crud.py
  3. 20
      templates/comissoes/comissao_detail.html

30
sapl/crud.py

@ -10,6 +10,17 @@ from django.views.generic import (
from sapl.layout import SaplFormLayout 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): class Crud(object):
pass pass
@ -85,6 +96,25 @@ def build_crud(model, *layout):
def title(self): def title(self):
return self.get_object() 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): class CrudUpdateView(BaseMixin, FormMessagesMixin, UpdateView):
form_class = crud.model_form form_class = crud.model_form
form_valid_message = _('Registro alterado com sucesso!') form_valid_message = _('Registro alterado com sucesso!')

75
sapl/test_crud.py

@ -3,12 +3,87 @@ 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
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
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(): def test_reverse():
assert '/comissoes/' == reverse('comissao:list') assert '/comissoes/' == reverse('comissao:list')
assert '/comissoes/create' == reverse('comissao:create') assert '/comissoes/create' == reverse('comissao:create')

20
templates/comissoes/comissao_detail.html

@ -13,6 +13,24 @@
</a></dd> </a></dd>
</dl> </dl>
TODO ... {# TODO replace fieldset for something semantically correct, but with similar visual grouping style #}
{% for fieldset in view.fieldsets %}
<fieldset>
<legend>{{ fieldset.legend }}</legend>
{% for row in fieldset.rows %}
<div class="row">
{% for column in row %}
<div class="columns large-{{ column.span }}">
<div id="div_id_{{ column.id }}" class="holder">
<label>{{ column.verbose_name }}</label> {# TODO replace labels, probably (are they correct here?) #}
<p>{{ column.text }}</p>
</div>
</div>
{% endfor %}
</div>
{% endfor %}
</fieldset>
{% endfor %}
{% endblock %} {% endblock %}

Loading…
Cancel
Save