Browse Source

WIP

pull/264/head
Edward Ribeiro 10 years ago
parent
commit
c60d50b18a
  1. 20
      norma/forms.py
  2. 116
      norma/views.py

20
norma/forms.py

@ -1,12 +1,13 @@
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 ObjectDoesNotExist
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 .models import NormaJuridica from .models import NormaJuridica
@ -97,7 +98,7 @@ class NormaJuridicaForm(ModelForm):
numero_materia = forms.CharField(label='Número', required=False) numero_materia = forms.CharField(label='Número', required=False)
ano_materia = forms.CharField(label='Ano', required=False) ano_materia = forms.CharField(label='Ano', required=False,)
class Meta: class Meta:
model = NormaJuridica model = NormaJuridica
@ -120,6 +121,21 @@ class NormaJuridicaForm(ModelForm):
'texto_integral', 'texto_integral',
] ]
def clean(self):
data = super(NormaJuridicaForm, self).clean()
if self.cleaned_data['tipo_materia']:
try:
materia = 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(

116
norma/views.py

@ -2,10 +2,10 @@ from datetime import datetime
from django.contrib import messages from django.contrib import messages
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse, reverse_lazy
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 import Crud, make_pagination from crud import Crud, make_pagination
@ -24,13 +24,8 @@ legislacao_citada_crud = Crud(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 +115,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):
return reverse('normajuridica:list') def form_valid(self, form):
norma = form.save(commit=False)
def get(self, request, *args, **kwargs): norma.timestamp = datetime.now()
form = NormaJuridicaForm() if form.cleaned_data['tipo_materia']:
return self.render_to_response({'form': form}) materia = MateriaLegislativa.objects.get(
tipo_id=form.data['tipo_materia'],
def post(self, request, *args, **kwargs): numero=form.data['numero_materia'],
form = self.get_form() ano=form.data['ano_materia'])
norma.materia = materia
if form.is_valid(): norma.save()
norma = form.save(commit=False) return HttpResponseRedirect(self.get_success_url())
if form.cleaned_data['tipo_materia']:
try: class NormaEditView(UpdateView):
materia = MateriaLegislativa.objects.get(
tipo_id=form.cleaned_data['tipo_materia'],
numero=form.cleaned_data['numero_materia'],
ano=form.cleaned_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.timestamp = datetime.now()
norma.save()
return HttpResponseRedirect(self.get_success_url())
else:
return self.render_to_response({'form': form})
class NormaEditView(CreateView):
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,
materia = MateriaLegislativa.objects.get( })
tipo_id=form.data['tipo_materia'], return data
numero=form.data['numero_materia'],
ano=form.data['ano_materia']) def form_valid(self, form):
except ObjectDoesNotExist: norma = form.save(commit=False)
msg = 'Matéria adicionada não existe!' norma.timestamp = datetime.now()
messages.add_message(request, messages.INFO, msg) if form.cleaned_data['tipo_materia']:
return self.render_to_response({'form': form}) materia = MateriaLegislativa.objects.get(
else: tipo_id=form.data['tipo_materia'],
norma.materia = materia numero=form.data['numero_materia'],
norma = form.save(commit=False) ano=form.data['ano_materia'])
norma.timestamp = datetime.now() norma.materia = materia
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):

Loading…
Cancel
Save