From 9d9412e3444cb81c8f95fe1cdc4e636a22afefd3 Mon Sep 17 00:00:00 2001 From: Eduardo Edson Batista Cordeiro Alves Date: Tue, 24 Nov 2015 13:54:31 -0200 Subject: [PATCH] Add form to PreposicaoView --- materia/migrations/0012_auto_20151124_1328.py | 19 ++++++ materia/models.py | 4 +- materia/views.py | 59 ++++++++++++++----- templates/materia/proposicao.html | 4 +- 4 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 materia/migrations/0012_auto_20151124_1328.py diff --git a/materia/migrations/0012_auto_20151124_1328.py b/materia/migrations/0012_auto_20151124_1328.py new file mode 100644 index 000000000..c886d6329 --- /dev/null +++ b/materia/migrations/0012_auto_20151124_1328.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('materia', '0011_proposicao_texto_original'), + ] + + operations = [ + migrations.AlterField( + model_name='proposicao', + name='autor', + field=models.ForeignKey(null=True, to='materia.Autor', blank=True), + ), + ] diff --git a/materia/models.py b/materia/models.py index 292cda6f6..caa245371 100644 --- a/materia/models.py +++ b/materia/models.py @@ -46,7 +46,7 @@ class Origem(models.Model): def get_materia_media_path(instance, subpath, filename): - return './materia/%s/%s/%s' % (instance.numero, subpath, filename) + return './materia/%s/%s/%s' % (instance, subpath, filename) def texto_upload_path(instance, filename): @@ -425,7 +425,7 @@ class TipoProposicao(models.Model): class Proposicao(models.Model): - autor = models.ForeignKey(Autor) + autor = models.ForeignKey(Autor, null=True, blank=True) tipo = models.ForeignKey(TipoProposicao, verbose_name=_('Tipo')) # XXX data_envio was not null, but actual data said otherwise!!! data_envio = models.DateTimeField( diff --git a/materia/views.py b/materia/views.py index 680c56622..82c035657 100644 --- a/materia/views.py +++ b/materia/views.py @@ -1,4 +1,4 @@ -from datetime import date +from datetime import date, datetime import sapl from comissoes.models import Comissao, Composicao @@ -1699,14 +1699,10 @@ class AutoriaEditView(GenericView): class ProposicaoForm(ModelForm): - tipo = forms.ChoiceField(required=True, - label='Tipo', - choices=get_tipos_proposicao(), - widget=forms.Select( - attrs={'class': 'selector'})) descricao = forms.CharField( - label='Descrição', required=True) + label='Descrição', required=True, + widget=forms.Textarea()) tipo_materia = forms.ChoiceField(required=False, label='Matéria Vinculada', @@ -1725,6 +1721,15 @@ class ProposicaoForm(ModelForm): fields = ['tipo', 'descricao', 'texto_original'] + exclude = ['autor', + 'data_envio', + 'data_recebimento', + 'data_devolucao', + 'justificativa_devolucao', + 'numero_proposicao', + 'status', + 'materia', + 'documento'] def __init__(self, *args, **kwargs): @@ -1743,17 +1748,13 @@ class ProposicaoForm(ModelForm): Column( ButtonHolder( Submit('sumbmit', 'Salvar', - css_class='button primary') - ), css_class='columns large-2')) + css_class='button primary') + ), css_class='columns large-2')) self.helper = FormHelper() self.helper.layout = Layout( Fieldset('Incluir Proposição', - row1, - row2, - row3, - row4, - ) + row1, row2, row3, row4) ) super(ProposicaoForm, self).__init__( *args, **kwargs) @@ -1767,6 +1768,32 @@ class ProposicaoView(FormMixin, GenericView): def get(self, request, *args, **kwargs): form = ProposicaoForm() + return self.render_to_response({'form': form}) - return self.render_to_response( - {'form': form}) + def post(self, request, *args, **kwargs): + form = ProposicaoForm(request.POST) + + if form.is_valid(): + + proposicao = form.save(commit=False) + if 'texto_original' in request.FILES: + proposicao.texto_original = request.FILES['texto_original'] + + try: + materia = MateriaLegislativa.objects.get( + tipo_id=int(form.data['tipo_materia']), + ano=int(form.data['ano_materia']), + numero=int(form.data['numero_materia'])) + except ObjectDoesNotExist: + return self.render_to_response( + {'form': form, + 'error': 'Matéria adiconada não existe!'}) + else: + proposicao.autor = materia.autoria_set.first().autor + proposicao.materia = materia + proposicao.data_envio = datetime.now() + proposicao.save() + + return self.form_valid(form) + else: + self.render_to_response({'form': form}) diff --git a/templates/materia/proposicao.html b/templates/materia/proposicao.html index 93a889772..abd8d9d0f 100644 --- a/templates/materia/proposicao.html +++ b/templates/materia/proposicao.html @@ -1,6 +1,8 @@ -{% extends "crud/form.html" %} +{% extends "crud/detail.html" %} {% load i18n %} {% load crispy_forms_tags %} {% block detail_content %} + {% if error %}
{{ error }}
{% endif %} + {% crispy form %} {% endblock %} \ No newline at end of file