From 877e0a54e9dd87475a350d2c7c2451d1ec7789d2 Mon Sep 17 00:00:00 2001 From: Eduardo Edson Batista Cordeiro Alves Date: Fri, 31 Jul 2015 16:37:42 -0300 Subject: [PATCH] Add dynamic fields to form in sessao --- parlamentares/models.py | 7 ++++--- sessao/models.py | 2 +- sessao/views.py | 27 ++++++++++++++++++++------- templates/sessao/expediente.html | 13 +++++++++---- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/parlamentares/models.py b/parlamentares/models.py index 4f9bc4565..7530c62ea 100644 --- a/parlamentares/models.py +++ b/parlamentares/models.py @@ -1,8 +1,9 @@ +import datetime + from django.db import models from django.utils.translation import ugettext_lazy as _ from sapl.utils import YES_NO_CHOICES, make_choices -import datetime class Legislatura(models.Model): @@ -17,8 +18,8 @@ class Legislatura(models.Model): def __str__(self): # XXX Usar id mesmo? Ou criar campo para nÂș legislatura? current_date = datetime.datetime.now().year - if(self.data_inicio.year <= current_date - and self.data_fim.year >= current_date): + if(self.data_inicio.year <= current_date and + self.data_fim.year >= current_date): current = ' (%s)' % _('Atual') else: current = '' diff --git a/sessao/models.py b/sessao/models.py index 497f28733..47d1a5f20 100644 --- a/sessao/models.py +++ b/sessao/models.py @@ -4,7 +4,7 @@ from django.utils.translation import ugettext_lazy as _ from materia.models import MateriaLegislativa from parlamentares.models import (CargoMesa, Legislatura, Parlamentar, SessaoLegislativa) -from sapl.utils import make_choices, YES_NO_CHOICES +from sapl.utils import YES_NO_CHOICES, make_choices class TipoSessaoPlenaria(models.Model): diff --git a/sessao/views.py b/sessao/views.py index 627b881b1..a18bb4832 100644 --- a/sessao/views.py +++ b/sessao/views.py @@ -1,5 +1,4 @@ from django import forms -from django.core.urlresolvers import reverse from django.utils.translation import ugettext_lazy as _ from django.views.generic.edit import FormMixin @@ -83,8 +82,17 @@ registro_votacao_crud = build_crud( ]) +def expediente_form_field(index): + return 'ExpedienteForm_%d' % index + + class ExpedienteForm(forms.Form): - teste = forms.CharField(widget=forms.Textarea, max_length=100) + + def __init__(self, *args, **kwargs): + super(ExpedienteForm, self).__init__(*args, **kwargs) + for i, values in enumerate(TipoExpediente.objects.all()): + self.fields[expediente_form_field(i)] = forms.CharField( + widget=forms.Textarea, max_length=100, required=False) class ExpedienteView(FormMixin, sessao_crud.CrudDetailView): @@ -95,14 +103,19 @@ class ExpedienteView(FormMixin, sessao_crud.CrudDetailView): self.object = self.get_object() form = self.get_form() if form.is_valid(): - expediente = ExpedienteSessao() - expediente.sessao_plenaria = self.object - expediente.tipo = TipoExpediente.objects.first() - expediente.conteudo = form.fields['teste'] - expediente.save() + for i, values in enumerate(TipoExpediente.objects.all()): + expediente = ExpedienteSessao() + expediente.sessao_plenaria = self.object + expediente.tipo = values + expediente.conteudo = form.data[expediente_form_field(i)] + expediente.save() return self.form_valid(form) else: return self.form_invalid(form) def get_success_url(self): return self.detail_url + + def get_title_and_fieldnames(self): + for i, tipo in enumerate(TipoExpediente.objects.all()): + yield tipo.nome, expediente_form_field(i) diff --git a/templates/sessao/expediente.html b/templates/sessao/expediente.html index 73bcf4309..371b74806 100644 --- a/templates/sessao/expediente.html +++ b/templates/sessao/expediente.html @@ -2,10 +2,15 @@ {% load i18n %} {% block detail_content %} -
- {% csrf_token %} - - + + {% csrf_token %} + + {% for title, fieldname in view.get_title_and_fieldnames %} +
{{ title }}:
+ + {% endfor %} + +
{% endblock detail_content %}