Browse Source

Refatora parlamentares e norma

pull/269/head
Edward Ribeiro 9 years ago
parent
commit
55df6c2e5a
  1. 30
      norma/forms.py
  2. 86
      norma/views.py
  3. 1
      parlamentares/views.py
  4. 1
      sessao/views.py

30
norma/forms.py

@ -1,13 +1,14 @@
from crispy_forms.helper import FormHelper from crispy_forms.helper import FormHelper
from crispy_forms.layout import Fieldset, Layout from crispy_forms.layout import Fieldset, Layout
from django import forms from django import forms
from django.core.exceptions import ValidationError from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.forms import ModelForm from django.forms import ModelForm
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
import crispy_layout_mixin import crispy_layout_mixin
from crispy_layout_mixin import form_actions from crispy_layout_mixin import form_actions
from materia.models import TipoMateriaLegislativa from materia.models import MateriaLegislativa, TipoMateriaLegislativa
from sapl.utils import RANGE_ANOS
from .models import NormaJuridica from .models import NormaJuridica
@ -89,16 +90,18 @@ class NormaJuridicaPesquisaForm(ModelForm):
class NormaJuridicaForm(ModelForm): class NormaJuridicaForm(ModelForm):
# Campos de MateriaLegislativa
tipo_materia = forms.ModelChoiceField( tipo_materia = forms.ModelChoiceField(
label='Matéria Legislativa', label='Matéria Legislativa',
required=False, required=False,
queryset=TipoMateriaLegislativa.objects.all(), queryset=TipoMateriaLegislativa.objects.all(),
empty_label='Selecione' empty_label='Selecione'
) )
numero_materia = forms.CharField(label='Número',
numero_materia = forms.CharField(label='Número', required=False) required=False)
ano_materia = forms.ChoiceField(label='Ano',
ano_materia = forms.CharField(label='Ano', required=False) required=False,
choices=RANGE_ANOS)
def clean_texto_integral(self): def clean_texto_integral(self):
texto_integral = self.cleaned_data.get('texto_integral', False) texto_integral = self.cleaned_data.get('texto_integral', False)
@ -130,6 +133,21 @@ class NormaJuridicaForm(ModelForm):
'texto_integral', 'texto_integral',
] ]
def clean(self):
data = super(NormaJuridicaForm, self).clean()
if self.cleaned_data['tipo_materia']:
try:
MateriaLegislativa.objects.get(
tipo=self.cleaned_data['tipo_materia'],
numero=self.cleaned_data['numero_materia'],
ano=self.cleaned_data['ano_materia'])
except ObjectDoesNotExist:
msg = 'Matéria adicionada não existe!'
raise forms.ValidationError(msg)
return data
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
row1 = crispy_layout_mixin.to_row( row1 = crispy_layout_mixin.to_row(

86
norma/views.py

@ -1,11 +1,9 @@
from datetime import datetime from datetime import datetime
from django.contrib import messages from django.core.urlresolvers import reverse_lazy
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import redirect from django.shortcuts import redirect
from django.views.generic import CreateView, FormView, ListView from django.views.generic import CreateView, FormView, ListView, UpdateView
from compilacao.views import IntegracaoTaView from compilacao.views import IntegracaoTaView
from crud.base import Crud, make_pagination from crud.base import Crud, make_pagination
@ -24,13 +22,8 @@ LegislacaoCitadaCrud = Crud.build(LegislacaoCitada, '')
class NormaPesquisaView(FormView): class NormaPesquisaView(FormView):
template_name = "norma/pesquisa.html" template_name = "norma/pesquisa.html"
success_url = "normajuridica:norma_pesquisa"
def get_success_url(self): form_class = NormaJuridicaPesquisaForm
return reverse('normajuridica:norma_pesquisa')
def get(self, request, *args, **kwargs):
form = NormaJuridicaPesquisaForm()
return self.render_to_response({'form': form})
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
form = NormaJuridicaPesquisaForm(request.POST) form = NormaJuridicaPesquisaForm(request.POST)
@ -120,74 +113,49 @@ class PesquisaNormaListView(ListView):
class NormaIncluirView(CreateView): class NormaIncluirView(CreateView):
template_name = "norma/normajuridica_incluir.html" template_name = "norma/normajuridica_incluir.html"
form_class = NormaJuridicaForm form_class = NormaJuridicaForm
success_url = reverse_lazy('normajuridica:list')
def get_success_url(self): def form_valid(self, form):
return reverse('normajuridica:list')
def get(self, request, *args, **kwargs):
form = NormaJuridicaForm()
return self.render_to_response({'form': form})
def post(self, request, *args, **kwargs):
form = self.get_form()
if form.is_valid():
norma = form.save(commit=False) norma = form.save(commit=False)
norma.timestamp = datetime.now()
if form.cleaned_data['tipo_materia']: if form.cleaned_data['tipo_materia']:
try:
materia = MateriaLegislativa.objects.get( materia = MateriaLegislativa.objects.get(
tipo_id=form.cleaned_data['tipo_materia'], tipo_id=form.data['tipo_materia'],
numero=form.cleaned_data['numero_materia'], numero=form.data['numero_materia'],
ano=form.cleaned_data['ano_materia']) ano=form.data['ano_materia'])
except ObjectDoesNotExist:
msg = 'Matéria adicionada não existe!'
messages.add_message(request, messages.INFO, msg)
return self.render_to_response({'form': form})
else:
norma.materia = materia norma.materia = materia
norma.timestamp = datetime.now()
norma.save() norma.save()
return HttpResponseRedirect(self.get_success_url()) return HttpResponseRedirect(self.get_success_url())
else:
return self.render_to_response({'form': form})
class NormaEditView(CreateView): class NormaEditView(UpdateView):
template_name = "norma/normajuridica_incluir.html" template_name = "norma/normajuridica_incluir.html"
form_class = NormaJuridicaForm form_class = NormaJuridicaForm
model = NormaJuridica
success_url = reverse_lazy('normajuridica:list')
def get(self, request, *args, **kwargs): def get_initial(self):
norma = NormaJuridica.objects.get(id=self.kwargs['pk']) data = super(NormaEditView, self).get_initial()
form = NormaJuridicaForm(instance=norma)
return self.render_to_response({'form': form})
def post(self, request, *args, **kwargs):
norma = NormaJuridica.objects.get(id=self.kwargs['pk']) norma = NormaJuridica.objects.get(id=self.kwargs['pk'])
form = NormaJuridicaForm(instance=norma, data=request.POST) if norma.materia:
data.update({
if form.is_valid(): 'tipo_materia': norma.materia.tipo,
if form.data['tipo_materia']: 'numero_materia': norma.materia.numero,
try: '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( materia = MateriaLegislativa.objects.get(
tipo_id=form.data['tipo_materia'], tipo_id=form.data['tipo_materia'],
numero=form.data['numero_materia'], numero=form.data['numero_materia'],
ano=form.data['ano_materia']) ano=form.data['ano_materia'])
except ObjectDoesNotExist:
msg = 'Matéria adicionada não existe!'
messages.add_message(request, messages.INFO, msg)
return self.render_to_response({'form': form})
else:
norma.materia = materia norma.materia = materia
norma = form.save(commit=False)
norma.timestamp = datetime.now()
norma.save() norma.save()
return HttpResponseRedirect(self.get_success_url()) return HttpResponseRedirect(self.get_success_url())
else:
return self.render_to_response({'form': form})
def get_success_url(self):
return reverse('normajuridica:list')
class NormaTaView(IntegracaoTaView): class NormaTaView(IntegracaoTaView):

1
parlamentares/views.py

@ -185,7 +185,6 @@ class ParlamentaresCadastroView(CreateView):
return context return context
def form_valid(self, form): def form_valid(self, form):
import ipdb; ipdb.set_trace()
form.save() form.save()
mandato = Mandato() mandato = Mandato()
mandato.parlamentar = form.instance mandato.parlamentar = form.instance

1
sessao/views.py

@ -2053,7 +2053,6 @@ class VotacaoExpedienteEditView(FormMixin, SessaoCrud.CrudDetailView):
yield tipo yield tipo
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
# import ipdb; ipdb.set_trace()
self.object = self.get_object() self.object = self.get_object()
context = self.get_context_data(object=self.object) context = self.get_context_data(object=self.object)

Loading…
Cancel
Save