Browse Source

Adiciona edição de Norma

pull/323/head
Eduardo Edson Batista Cordeiro Alves 9 years ago
committed by Edward Ribeiro
parent
commit
3bfa3628f3
  1. 35
      norma/forms.py
  2. 7
      norma/urls.py
  3. 64
      norma/views.py

35
norma/forms.py

@ -3,8 +3,10 @@ from datetime import datetime
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.contrib import messages
from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.forms import ModelForm from django.forms import ModelForm
from django.utils.translation import ugettext_lazy as _
import crispy_layout_mixin import crispy_layout_mixin
from crispy_layout_mixin import form_actions from crispy_layout_mixin import form_actions
@ -131,19 +133,30 @@ class NormaJuridicaForm(ModelForm):
'observacao', 'observacao',
'texto_integral'] 'texto_integral']
def save(self, commit=False): def clean(self):
norma = super(NormaJuridicaForm, self).save(commit) cleaned_data = self.cleaned_data
norma.timestamp = datetime.now()
if (cleaned_data['tipo_materia'] and
cleaned_data['numero_materia'] and
cleaned_data['ano_materia']):
if ('tipo_materia' and try:
'numero_materia' and materia = MateriaLegislativa.objects.get(
'ano_materia' in self.cleaned_data): 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( else:
tipo_id=self.cleaned_data['tipo_materia'], cleaned_data['materia'] = None
numero=self.cleaned_data['numero_materia'], return cleaned_data
ano=self.cleaned_data['ano_materia'])
norma.materia = materia
def save(self, commit=False):
norma = super(NormaJuridicaForm, self).save(commit)
norma.timestamp = datetime.now()
norma.materia = self.cleaned_data['materia']
norma.save() norma.save()
return norma return norma

7
norma/urls.py

@ -1,9 +1,8 @@
from django.conf.urls import include, url from django.conf.urls import include, url
from norma.views import (AssuntoNormaCrud, NormaCrud, from norma.views import (AssuntoNormaCrud, NormaCrud, NormaPesquisaView,
NormaPesquisaView, NormaTaView, NormaTaView, NormaTemporarioCrud,
NormaTemporarioCrud, PesquisaNormaListView, PesquisaNormaListView, TipoNormaCrud)
TipoNormaCrud)
from .apps import AppConfig from .apps import AppConfig

64
norma/views.py

@ -5,8 +5,8 @@ from django.http import HttpResponseRedirect
from django.shortcuts import redirect from django.shortcuts import redirect
from django.views.generic import CreateView, FormView, ListView, UpdateView from django.views.generic import CreateView, FormView, ListView, UpdateView
from compilacao.views import IntegracaoTaView
import crud.base import crud.base
from compilacao.views import IntegracaoTaView
from crud.base import Crud, make_pagination from crud.base import Crud, make_pagination
from materia.models import MateriaLegislativa from materia.models import MateriaLegislativa
@ -24,6 +24,17 @@ class NormaCrud(Crud):
model = NormaJuridica model = NormaJuridica
help_path = 'norma_juridica' 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): class CreateView(crud.base.CrudCreateView):
form_class = NormaJuridicaForm form_class = NormaJuridicaForm
@ -121,57 +132,6 @@ class PesquisaNormaListView(ListView):
return context 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): class NormaTaView(IntegracaoTaView):
model = NormaJuridica model = NormaJuridica
model_type_foreignkey = TipoNormaJuridica model_type_foreignkey = TipoNormaJuridica

Loading…
Cancel
Save