Browse Source

Add custom crispy layout class

pull/6/head
Marcio Mazza 10 years ago
parent
commit
50096ea788
  1. 41
      comissoes/forms.py
  2. 5
      comissoes/views.py
  3. 37
      sapl/layout.py
  4. 15
      templates/comissoes/comissao_form.html

41
comissoes/forms.py

@ -0,0 +1,41 @@
from crispy_forms.helper import FormHelper
from django import forms
from django.utils.translation import ugettext as _
from comissoes.models import Comissao
from sapl.layout import SaplFormLayout
class ComissaoForm(forms.ModelForm):
class Meta:
model = Comissao
fields = [f.name for f in Comissao._meta.fields]
def __init__(self, *args, **kwargs):
super(ComissaoForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = SaplFormLayout(
[_('Dados Básicos'),
[('nome', 9), ('sigla', 3)],
[('tipo', 3),
('data_criacao', 3),
('unidade_deliberativa', 3),
('data_extincao', 3)]],
[_('Dados Complementares'),
[('local_reuniao', 4),
('agenda_reuniao', 4),
('telefone_reuniao', 4)],
[('endereco_secretaria', 4),
('telefone_secretaria', 4),
('fax_secretaria', 4)],
[('secretario', 4), ('email', 8)],
[('finalidade', 12)]],
[_('Temporária'),
[('apelido_temp', 8), ('data_instalacao_temp', 4)],
[('data_final_prevista_temp', 4),
('data_prorrogada_temp', 4),
('data_fim_comissao', 4)]],
)

5
comissoes/views.py

@ -3,6 +3,7 @@ from django.core.urlresolvers import reverse_lazy
from django.views.generic import CreateView, DeleteView, ListView, UpdateView, DetailView
from comissoes.models import Comissao
from .forms import ComissaoForm
class ComissaoListView(ListView):
@ -20,10 +21,8 @@ class ComissaoCreateView(CreateView):
class ComissaoUpdateView(FormMessagesMixin, UpdateView):
model = Comissao
fields = [f.name for f in Comissao._meta.fields]
form_class = ComissaoForm
success_url = reverse_lazy('comissao_list')
form_invalid_message = u"Something went wrong, post was not saved"
def get_form_valid_message(self):

37
sapl/layout.py

@ -0,0 +1,37 @@
from crispy_forms.bootstrap import FormActions
from crispy_forms.layout import Submit, Reset, Layout, Div, Fieldset
from django.utils.translation import ugettext as _
def to_column(name_span):
try:
fieldname, span = name_span
except:
return name_span
else:
return Div(fieldname, css_class='col-xs-%d' % span)
def to_row(names_spans):
return Div(*list(map(to_column, names_spans)), css_class='row-fluid')
def to_fieldsets(fields):
for field in fields:
if isinstance(field, list):
legend, *row_specs = field
rows = [to_row(name_span_list) for name_span_list in row_specs]
yield Fieldset(legend, *rows)
else:
yield field
class SaplFormLayout(Layout):
def __init__(self, *fields):
_fields = list(to_fieldsets(fields)) + [
FormActions(
Submit('save', _('Save'), css_class='btn btn-primary '),
Reset('reset', _('Cancel'), css_class='btn'))
]
super(SaplFormLayout, self).__init__(*_fields)

15
templates/comissoes/comissao_form.html

@ -1,22 +1,11 @@
{% extends "base.html" %}
{% load i18n bootstrap3 %}
{% load i18n bootstrap3 crispy_forms_tags %}
{% block title %}SAPL{% endblock %}
{% block content %}
<div id="content-main">
{# Display a form #}
<form method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">
{% bootstrap_icon "star" %} Submit
</button>
{% endbuttons %}
</form>
{% crispy form %}
</div>
{% endblock %}

Loading…
Cancel
Save