diff --git a/comissoes/forms.py b/comissoes/forms.py new file mode 100644 index 000000000..2732eb004 --- /dev/null +++ b/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)]], + ) diff --git a/comissoes/views.py b/comissoes/views.py index a75b0802f..9bd88c25b 100644 --- a/comissoes/views.py +++ b/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): diff --git a/sapl/layout.py b/sapl/layout.py new file mode 100644 index 000000000..920db7089 --- /dev/null +++ b/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) diff --git a/templates/comissoes/comissao_form.html b/templates/comissoes/comissao_form.html index f0e38178c..9d1bebbf8 100644 --- a/templates/comissoes/comissao_form.html +++ b/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 %}
- {# Display a form #} -
- {% csrf_token %} - - {% bootstrap_form form %} - - {% buttons %} - - {% endbuttons %} -
+ {% crispy form %}
{% endblock %}