From 3bfa3628f3a7d6065e9877df75c500d367940a31 Mon Sep 17 00:00:00 2001 From: Eduardo Edson Batista Cordeiro Alves Date: Wed, 13 Apr 2016 09:46:10 -0300 Subject: [PATCH] =?UTF-8?q?Adiciona=20edi=C3=A7=C3=A3o=20de=20Norma?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- norma/forms.py | 35 ++++++++++++++++++--------- norma/urls.py | 7 +++--- norma/views.py | 64 ++++++++++---------------------------------------- 3 files changed, 39 insertions(+), 67 deletions(-) diff --git a/norma/forms.py b/norma/forms.py index baa01e8bb..8320ebbe7 100644 --- a/norma/forms.py +++ b/norma/forms.py @@ -3,8 +3,10 @@ from datetime import datetime from crispy_forms.helper import FormHelper from crispy_forms.layout import Fieldset, Layout from django import forms +from django.contrib import messages from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.forms import ModelForm +from django.utils.translation import ugettext_lazy as _ import crispy_layout_mixin from crispy_layout_mixin import form_actions @@ -131,19 +133,30 @@ class NormaJuridicaForm(ModelForm): 'observacao', 'texto_integral'] - def save(self, commit=False): - norma = super(NormaJuridicaForm, self).save(commit) - norma.timestamp = datetime.now() + def clean(self): + cleaned_data = self.cleaned_data + + if (cleaned_data['tipo_materia'] and + cleaned_data['numero_materia'] and + cleaned_data['ano_materia']): - if ('tipo_materia' and - 'numero_materia' and - 'ano_materia' in self.cleaned_data): + try: + materia = MateriaLegislativa.objects.get( + tipo_id=cleaned_data['tipo_materia'], + numero=cleaned_data['numero_materia'], + ano=cleaned_data['ano_materia']) + except ObjectDoesNotExist: + raise forms.ValidationError("Matéria escolhida não existe!") + else: + cleaned_data['materia'] = materia - materia = MateriaLegislativa.objects.get( - tipo_id=self.cleaned_data['tipo_materia'], - numero=self.cleaned_data['numero_materia'], - ano=self.cleaned_data['ano_materia']) - norma.materia = materia + else: + cleaned_data['materia'] = None + return cleaned_data + def save(self, commit=False): + norma = super(NormaJuridicaForm, self).save(commit) + norma.timestamp = datetime.now() + norma.materia = self.cleaned_data['materia'] norma.save() return norma diff --git a/norma/urls.py b/norma/urls.py index 34d5083af..c75863a5f 100644 --- a/norma/urls.py +++ b/norma/urls.py @@ -1,9 +1,8 @@ from django.conf.urls import include, url -from norma.views import (AssuntoNormaCrud, NormaCrud, - NormaPesquisaView, NormaTaView, - NormaTemporarioCrud, PesquisaNormaListView, - TipoNormaCrud) +from norma.views import (AssuntoNormaCrud, NormaCrud, NormaPesquisaView, + NormaTaView, NormaTemporarioCrud, + PesquisaNormaListView, TipoNormaCrud) from .apps import AppConfig diff --git a/norma/views.py b/norma/views.py index c644f2cf5..64078ec97 100644 --- a/norma/views.py +++ b/norma/views.py @@ -5,8 +5,8 @@ from django.http import HttpResponseRedirect from django.shortcuts import redirect from django.views.generic import CreateView, FormView, ListView, UpdateView -from compilacao.views import IntegracaoTaView import crud.base +from compilacao.views import IntegracaoTaView from crud.base import Crud, make_pagination from materia.models import MateriaLegislativa @@ -24,6 +24,17 @@ class NormaCrud(Crud): model = NormaJuridica help_path = 'norma_juridica' + class UpdateView(crud.base.CrudUpdateView): + form_class = NormaJuridicaForm + + def get_initial(self): + norma = NormaJuridica.objects.get(id=self.kwargs['pk']) + if norma.materia: + self.initial['tipo_materia'] = norma.materia.tipo + self.initial['ano_materia'] = norma.materia.ano + self.initial['numero_materia'] = norma.materia.numero + return self.initial.copy() + class CreateView(crud.base.CrudCreateView): form_class = NormaJuridicaForm @@ -121,57 +132,6 @@ class PesquisaNormaListView(ListView): return context -# class NormaIncluirView(CreateView): -# template_name = "norma/normajuridica_incluir.html" -# form_class = NormaJuridicaForm -# success_url = reverse_lazy('norma:normajuridica_list') -# -# def get_success_url(self): -# return reverse_lazy('norma:norma_pesquisa') -# -# def form_valid(self, form): -# norma = form.save(commit=False) -# norma.timestamp = datetime.now() -# if form.cleaned_data['tipo_materia']: -# materia = MateriaLegislativa.objects.get( -# tipo_id=form.data['tipo_materia'], -# numero=form.data['numero_materia'], -# ano=form.data['ano_materia']) -# norma.materia = materia -# norma.save() -# return HttpResponseRedirect(self.get_success_url()) -# -# -# class NormaEditView(UpdateView): -# template_name = "norma/normajuridica_incluir.html" -# form_class = NormaJuridicaForm -# model = NormaJuridica -# success_url = reverse_lazy('norma:pesquisa_norma') -# -# def get_initial(self): -# data = super(NormaEditView, self).get_initial() -# norma = NormaJuridica.objects.get(id=self.kwargs['pk']) -# if norma.materia: -# data.update({ -# 'tipo_materia': norma.materia.tipo, -# 'numero_materia': norma.materia.numero, -# 'ano_materia': norma.materia.ano, -# }) -# return data -# -# def form_valid(self, form): -# norma = form.save(commit=False) -# norma.timestamp = datetime.now() -# if form.cleaned_data['tipo_materia']: -# materia = MateriaLegislativa.objects.get( -# tipo_id=form.data['tipo_materia'], -# numero=form.data['numero_materia'], -# ano=form.data['ano_materia']) -# norma.materia = materia -# norma.save() -# return HttpResponseRedirect(self.get_success_url()) - - class NormaTaView(IntegracaoTaView): model = NormaJuridica model_type_foreignkey = TipoNormaJuridica