From 53dd7538d2ef7b8291e2e2e726050ac39f0ebf6c Mon Sep 17 00:00:00 2001 From: Eduardo Edson Batista Cordeiro Alves Date: Tue, 17 Nov 2015 15:51:47 -0200 Subject: [PATCH] Fix form and upload file in Fomulario Simplifcado (Materia) --- materia/migrations/0010_auto_20151117_1551.py | 20 +++ materia/models.py | 2 +- materia/views.py | 136 +++++++----------- .../materia/formulario_simplificado.html | 6 +- 4 files changed, 74 insertions(+), 90 deletions(-) create mode 100644 materia/migrations/0010_auto_20151117_1551.py diff --git a/materia/migrations/0010_auto_20151117_1551.py b/materia/migrations/0010_auto_20151117_1551.py new file mode 100644 index 000000000..93f67f486 --- /dev/null +++ b/materia/migrations/0010_auto_20151117_1551.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations +import materia.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('materia', '0009_auto_20151029_1420'), + ] + + operations = [ + migrations.AlterField( + model_name='materialegislativa', + name='texto_original', + field=models.FileField(verbose_name='Texto Original (PDF)', upload_to=materia.models.texto_upload_path, blank=True, null=True), + ), + ] diff --git a/materia/models.py b/materia/models.py index 37692f49a..4274222fd 100644 --- a/materia/models.py +++ b/materia/models.py @@ -120,7 +120,7 @@ class MateriaLegislativa(models.Model): blank=True, null=True, upload_to=texto_upload_path, - verbose_name=_('Texto original (PDF)')) + verbose_name=_('Texto Original (PDF)')) class Meta: verbose_name = _('Matéria Legislativa') diff --git a/materia/views.py b/materia/views.py index e082247de..13320eaeb 100644 --- a/materia/views.py +++ b/materia/views.py @@ -1,7 +1,8 @@ from datetime import date +from comissoes.models import Comissao from crispy_forms.helper import FormHelper -from crispy_forms.layout import ButtonHolder, Fieldset, Layout, Submit, HTML +from crispy_forms.layout import HTML, ButtonHolder, Fieldset, Layout, Submit from django import forms from django.core.exceptions import ObjectDoesNotExist from django.core.urlresolvers import reverse @@ -10,11 +11,9 @@ from django.shortcuts import render from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ from django.views.generic.edit import FormMixin -from vanilla import GenericView - -from comissoes.models import Comissao from norma.models import LegislacaoCitada, NormaJuridica, TipoNormaJuridica from sapl.crud import build_crud +from vanilla import GenericView from .models import (Anexada, Autor, Autoria, DespachoInicial, DocumentoAcessorio, MateriaLegislativa, Numeracao, Orgao, @@ -246,58 +245,25 @@ class HorizontalRadioRenderer(forms.RadioSelect.renderer): return mark_safe(u' '.join([u'%s ' % w for w in self])) -class FormularioSimplificadoForm(forms.Form): +class FormularioSimplificadoForm(ModelForm): - tipo_materia = forms.ChoiceField(required=True, - label='Tipo Matéria', - choices=get_tipos_materia(), - widget=forms.Select( - attrs={'class': 'selector'})) + data_apresentacao = forms.DateField(label=u'Data Apresentação', + input_formats=['%d/%m/%Y'], + required=False, + widget=forms.DateInput( + format='%d/%m/%Y')) - numero_materia = forms.CharField( - label='Núm. Matéria', required=True) - - ano_materia = forms.ChoiceField(required=True, - label='Ano', - choices=get_range_anos(), - widget=forms.Select( - attrs={'class': 'selector'})) - - data_materia = forms.DateField(label='Data Apresentação', - required=True, - widget=forms.TextInput( - attrs={'class': 'dateinput'})) - - numero_protocolo = forms.CharField( - label='Número de Protocolo', required=True) - - regime_tramitacao = forms.ChoiceField(required=False, - label='Regime de Tramitação', - choices=get_regimes_tramitacao(), - widget=forms.Select( - attrs={'class': 'selector'})) - - em_tramitacao = forms.TypedChoiceField( - coerce=lambda x: x == 'Sim', - choices=((True, 'Sim'), (False, 'Não')), - widget=forms.RadioSelect - ) - - ementa = forms.CharField( - label='Ementa', required=True, widget=forms.Textarea) - - texto_original = forms.ChoiceField(required=False, - label='Regime de Tramitação', - choices=( - ('1', 'Arquivo'), - ('2', 'Proposição')), - widget=forms.RadioSelect) - - arquivo = forms.FileField(required=False, label='Arquivo') - - proposicao = forms.CharField(required=False, label='Proposição', - widget=forms.TextInput( - attrs={'disabled': 'True'})) + class Meta: + model = MateriaLegislativa + fields = ['tipo', + 'numero', + 'ano', + 'data_apresentacao', + 'numero_protocolo', + 'regime_tramitacao', + 'em_tramitacao', + 'ementa', + 'texto_original'] def __init__(self, *args, **kwargs): self.helper = FormHelper() @@ -306,17 +272,15 @@ class FormularioSimplificadoForm(forms.Form): 'Formulário Simplificado', Fieldset( 'Identificação Básica', - 'tipo_materia', - 'numero_materia', - 'ano_materia', - 'data_materia', + 'tipo', + 'numero', + 'ano', + 'data_apresentacao', 'numero_protocolo', 'regime_tramitacao', 'em_tramitacao', 'ementa', - 'texto_original', - 'arquivo', - 'proposicao' + 'texto_original' ), ButtonHolder( Submit('submit', 'Salvar', @@ -407,9 +371,7 @@ class FormularioCadastroForm(ModelForm): class FormularioSimplificadoView(FormMixin, GenericView): - template_name = "materia/formulario_simplificado.html" - model = MateriaLegislativa def get_success_url(self): return reverse('formulario_simplificado') @@ -422,21 +384,22 @@ class FormularioSimplificadoView(FormMixin, GenericView): form = FormularioSimplificadoForm(request.POST) if form.is_valid: - - materia = MateriaLegislativa() - materia.numero = request.POST['numero_materia'] - materia.ano = request.POST['ano_materia'] - materia.numero_protocolo = request.POST['numero_protocolo'] - materia.em_tramitacao = request.POST['em_tramitacao'] - materia.ementa = request.POST['ementa'] - materia.tipo_id = request.POST['tipo_materia'] - materia.regime_tramitacao_id = request.POST['regime_tramitacao'] + materia = form.save(commit=False) + if request.FILES['texto_original']: + materia.texto_original = request.FILES['texto_original'] materia.save() + # materia = MateriaLegislativa() + # materia.numero = request.POST['numero_materia'] + # materia.ano = request.POST['ano_materia'] + # materia.numero_protocolo = request.POST['numero_protocolo'] + # materia.em_tramitacao = request.POST['em_tramitacao'] + # materia.ementa = request.POST['ementa'] + # materia.tipo_id = request.POST['tipo_materia'] + # materia.regime_tramitacao_id = request.POST['regime_tramitacao'] + # materia.texto_original = request.FILES['texto_original'] + # materia.save() - message = "Materia Legislativa criada com sucesso" - return render(request, - reverse('formulario_simplificado'), - {'form': form, 'message': message}) + return self.form_valid(form) else: return self.form_invalid(form) @@ -1262,7 +1225,8 @@ class TramitacaoForm(ModelForm): self.helper = FormHelper() self.helper.layout = Layout( Fieldset('Incluir Tramitação', - HTML("