From b8e3484ff53e9f7a47a170f5a4d7252b9415fbb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Sconetto?= Date: Thu, 7 Jun 2018 14:37:32 -0300 Subject: [PATCH] Fix #1939 #1958 (#1978) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix #1958 * Fix #1939 #1958 Corrige erro de inicialização em formulários Co-authored-by: edwardoliveira --- sapl/audiencia/views.py | 20 +++++++---------- sapl/materia/views.py | 46 +++++++++++++++++++++----------------- sapl/norma/views.py | 22 +++++++++--------- sapl/protocoloadm/views.py | 11 ++++----- sapl/sessao/views.py | 41 ++++++++++++++++++--------------- 5 files changed, 75 insertions(+), 65 deletions(-) diff --git a/sapl/audiencia/views.py b/sapl/audiencia/views.py index c0e353139..2c16b0919 100644 --- a/sapl/audiencia/views.py +++ b/sapl/audiencia/views.py @@ -1,15 +1,10 @@ -from django.shortcuts import render from django.http import HttpResponse -from django.core.urlresolvers import reverse -from django.db.models import F from django.views.decorators.clickjacking import xframe_options_exempt -from django.views.generic import ListView -from sapl.comissoes.forms import ParticipacaoCreateForm, ParticipacaoEditForm -from sapl.crud.base import RP_DETAIL, RP_LIST, Crud, CrudAux, MasterDetailCrud -from sapl.materia.models import MateriaLegislativa +from django.views.generic import UpdateView +from sapl.crud.base import RP_DETAIL, RP_LIST, Crud from .forms import AudienciaForm -from .models import (AudienciaPublica, TipoAudienciaPublica) +from .models import AudienciaPublica def index(request): return HttpResponse("Audiência Pública") @@ -36,10 +31,11 @@ class AudienciaCrud(Crud): form_class = AudienciaForm def get_initial(self): - self.initial['tipo_materia'] = self.object.materia.tipo.id - self.initial['numero_materia'] = self.object.materia.numero - self.initial['ano_materia'] = self.object.materia.ano - return self.initial + initial = super(UpdateView, self).get_initial() + initial['tipo_materia'] = self.object.materia.tipo.id + initial['numero_materia'] = self.object.materia.numero + initial['ano_materia'] = self.object.materia.ano + return initial class DeleteView(Crud.DeleteView): pass diff --git a/sapl/materia/views.py b/sapl/materia/views.py index 74f2bd6b6..807db3d9c 100644 --- a/sapl/materia/views.py +++ b/sapl/materia/views.py @@ -17,7 +17,7 @@ from django.shortcuts import get_object_or_404, redirect from django.template import RequestContext, loader from django.utils import formats, timezone from django.utils.translation import ugettext_lazy as _ -from django.views.generic import CreateView, ListView, TemplateView, UpdateView +from django.views.generic import FormView, ListView, TemplateView, CreateView, UpdateView from django.views.generic.base import RedirectView from django.views.generic.edit import FormView from django_filters.views import FilterView @@ -1013,18 +1013,19 @@ class TramitacaoCrud(MasterDetailCrud): 'pk': self.kwargs['pk']}) def get_initial(self): + initial = super(CreateView, self).get_initial() local = MateriaLegislativa.objects.get( pk=self.kwargs['pk']).tramitacao_set.order_by( '-data_tramitacao', '-id').first() if local: - self.initial['unidade_tramitacao_local' + initial['unidade_tramitacao_local' ] = local.unidade_tramitacao_destino.pk else: - self.initial['unidade_tramitacao_local'] = '' - self.initial['data_tramitacao'] = timezone.now().date() - return self.initial + initial['unidade_tramitacao_local'] = '' + initial['data_tramitacao'] = timezone.now().date() + return initial def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) @@ -1154,9 +1155,10 @@ class DocumentoAcessorioCrud(MasterDetailCrud): super(MasterDetailCrud.CreateView, self).__init__(**kwargs) def get_initial(self): - self.initial['data'] = timezone.now().date() + initial = super(CreateView, self).get_initial() + initial['data'] = timezone.now().date() - return self.initial + return initial def get_context_data(self, **kwargs): context = super( @@ -1283,10 +1285,11 @@ class LegislacaoCitadaCrud(MasterDetailCrud): form_class = LegislacaoCitadaForm def get_initial(self): - self.initial['tipo'] = self.object.norma.tipo.id - self.initial['numero'] = self.object.norma.numero - self.initial['ano'] = self.object.norma.ano - return self.initial + initial = super(UpdateView, self).get_initial() + initial['tipo'] = self.object.norma.tipo.id + initial['numero'] = self.object.norma.numero + initial['ano'] = self.object.norma.ano + return initial class DetailView(MasterDetailCrud.DetailView): @@ -1319,10 +1322,11 @@ class AnexadaCrud(MasterDetailCrud): form_class = AnexadaForm def get_initial(self): - self.initial['tipo'] = self.object.materia_anexada.tipo.id - self.initial['numero'] = self.object.materia_anexada.numero - self.initial['ano'] = self.object.materia_anexada.ano - return self.initial + initial = super(UpdateView, self).get_initial() + initial['tipo'] = self.object.materia_anexada.tipo.id + initial['numero'] = self.object.materia_anexada.numero + initial['ano'] = self.object.materia_anexada.ano + return initial class DetailView(MasterDetailCrud.DetailView): @@ -1344,16 +1348,18 @@ class MateriaAssuntoCrud(MasterDetailCrud): form_class = MateriaAssuntoForm def get_initial(self): - self.initial['materia'] = self.kwargs['pk'] - return self.initial + initial = super(CreateView, self).get_initial() + initial['materia'] = self.kwargs['pk'] + return initial class UpdateView(MasterDetailCrud.UpdateView): form_class = MateriaAssuntoForm def get_initial(self): - self.initial['materia'] = self.get_object().materia - self.initial['assunto'] = self.get_object().assunto - return self.initial + initial = super(UpdateView, self).get_initial() + initial['materia'] = self.get_object().materia + initial['assunto'] = self.get_object().assunto + return initial class MateriaLegislativaCrud(Crud): diff --git a/sapl/norma/views.py b/sapl/norma/views.py index fd5396353..650442676 100644 --- a/sapl/norma/views.py +++ b/sapl/norma/views.py @@ -7,7 +7,7 @@ from django.http import HttpResponse, JsonResponse from django.template import RequestContext, loader from django.utils import timezone from django.utils.translation import ugettext_lazy as _ -from django.views.generic import CreateView, ListView, TemplateView, UpdateView +from django.views.generic import TemplateView, UpdateView from django.views.generic.base import RedirectView from django.views.generic.edit import FormView from django_filters.views import FilterView @@ -51,11 +51,12 @@ class NormaRelacionadaCrud(MasterDetailCrud): form_class = NormaRelacionadaForm def get_initial(self): - self.initial['tipo'] = self.object.norma_relacionada.tipo.id - self.initial['numero'] = self.object.norma_relacionada.numero - self.initial['ano'] = self.object.norma_relacionada.ano - self.initial['ementa'] = self.object.norma_relacionada.ementa - return self.initial + initial = super(UpdateView, self).get_initial() + initial['tipo'] = self.object.norma_relacionada.tipo.id + initial['numero'] = self.object.norma_relacionada.numero + initial['ano'] = self.object.norma_relacionada.ano + initial['ementa'] = self.object.norma_relacionada.ementa + return initial class DetailView(MasterDetailCrud.DetailView): @@ -171,12 +172,13 @@ class NormaCrud(Crud): layout_key = 'NormaJuridicaCreate' def get_initial(self): + initial = super(UpdateView, self).get_initial() 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() + initial['tipo_materia'] = norma.materia.tipo + initial['ano_materia'] = norma.materia.ano + initial['numero_materia'] = norma.materia.numero + return initial def recuperar_norma(request): diff --git a/sapl/protocoloadm/views.py b/sapl/protocoloadm/views.py index 7dd7206c7..c1a2b185a 100644 --- a/sapl/protocoloadm/views.py +++ b/sapl/protocoloadm/views.py @@ -11,7 +11,7 @@ from django.http.response import HttpResponseRedirect from django.shortcuts import redirect from django.utils import timezone from django.utils.translation import ugettext_lazy as _ -from django.views.generic import CreateView, ListView +from django.views.generic import ListView, CreateView from django.views.generic.base import RedirectView, TemplateView from django.views.generic.edit import FormView from django_filters.views import FilterView @@ -610,18 +610,19 @@ class TramitacaoAdmCrud(MasterDetailCrud): form_class = TramitacaoAdmForm def get_initial(self): + initial = super(CreateView, self).get_initial() local = DocumentoAdministrativo.objects.get( pk=self.kwargs['pk']).tramitacaoadministrativo_set.order_by( '-data_tramitacao', '-id').first() if local: - self.initial['unidade_tramitacao_local' + initial['unidade_tramitacao_local' ] = local.unidade_tramitacao_destino.pk else: - self.initial['unidade_tramitacao_local'] = '' - self.initial['data_tramitacao'] = timezone.now().date() - return self.initial + initial['unidade_tramitacao_local'] = '' + initial['data_tramitacao'] = timezone.now().date() + return initial def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) diff --git a/sapl/sessao/views.py b/sapl/sessao/views.py index 8700177c0..2f5a2c73f 100644 --- a/sapl/sessao/views.py +++ b/sapl/sessao/views.py @@ -16,7 +16,7 @@ from django.utils.decorators import method_decorator from django.utils.html import strip_tags from django.utils.translation import ugettext_lazy as _ from django.views.decorators.csrf import csrf_exempt -from django.views.generic import FormView, ListView, TemplateView +from django.views.generic import FormView, ListView, TemplateView, CreateView, UpdateView from django.views.generic.base import RedirectView from django.views.generic.detail import DetailView from django.views.generic.edit import FormMixin @@ -418,14 +418,15 @@ class MateriaOrdemDiaCrud(MasterDetailCrud): form_class = OrdemDiaForm def get_initial(self): - self.initial['data_ordem'] = SessaoPlenaria.objects.get( + initial = super(CreateView, self).get_inital() + initial['data_ordem'] = SessaoPlenaria.objects.get( pk=self.kwargs['pk']).data_inicio.strftime('%d/%m/%Y') max_numero_ordem = OrdemDia.objects.filter( sessao_plenaria=self.kwargs['pk']).aggregate( Max('numero_ordem'))['numero_ordem__max'] - self.initial['numero_ordem'] = ( + initial['numero_ordem'] = ( max_numero_ordem if max_numero_ordem else 0) + 1 - return self.initial + return initial def get_success_url(self): return reverse('sapl.sessao:ordemdia_list', @@ -435,10 +436,11 @@ class MateriaOrdemDiaCrud(MasterDetailCrud): form_class = OrdemDiaForm def get_initial(self): - self.initial['tipo_materia'] = self.object.materia.tipo.id - self.initial['numero_materia'] = self.object.materia.numero - self.initial['ano_materia'] = self.object.materia.ano - return self.initial + initial = super(UpdateView, self).get_initial() + initial['tipo_materia'] = self.object.materia.tipo.id + initial['numero_materia'] = self.object.materia.numero + initial['ano_materia'] = self.object.materia.ano + return initial class DetailView(MasterDetailCrud.DetailView): @@ -492,14 +494,15 @@ class ExpedienteMateriaCrud(MasterDetailCrud): form_class = ExpedienteMateriaForm def get_initial(self): - self.initial['data_ordem'] = SessaoPlenaria.objects.get( + initial = super(CreateView, self).get_initial() + initial['data_ordem'] = SessaoPlenaria.objects.get( pk=self.kwargs['pk']).data_inicio.strftime('%d/%m/%Y') max_numero_ordem = ExpedienteMateria.objects.filter( sessao_plenaria=self.kwargs['pk']).aggregate( Max('numero_ordem'))['numero_ordem__max'] - self.initial['numero_ordem'] = ( + initial['numero_ordem'] = ( max_numero_ordem if max_numero_ordem else 0) + 1 - return self.initial + return initial def get_success_url(self): return reverse('sapl.sessao:expedientemateria_list', @@ -509,10 +512,11 @@ class ExpedienteMateriaCrud(MasterDetailCrud): form_class = ExpedienteMateriaForm def get_initial(self): - self.initial['tipo_materia'] = self.object.materia.tipo.id - self.initial['numero_materia'] = self.object.materia.numero - self.initial['ano_materia'] = self.object.materia.ano - return self.initial + initial = super(UpdateView, self).get_initial() + initial['tipo_materia'] = self.object.materia.tipo.id + initial['numero_materia'] = self.object.materia.numero + initial['ano_materia'] = self.object.materia.ano + return initial class DetailView(MasterDetailCrud.DetailView): @@ -1146,9 +1150,10 @@ class ResumoOrdenacaoView(PermissionRequiredMixin, FormView): return reverse('sapl.base:sistema') def get_initial(self): + initial = super(ResumoOrdenacaoView, self).get_initial() ordenacao = ResumoOrdenacao.objects.first() if ordenacao: - return {'primeiro': ordenacao.primeiro, + initial.update({'primeiro': ordenacao.primeiro, 'segundo': ordenacao.segundo, 'terceiro': ordenacao.terceiro, 'quarto': ordenacao.quarto, @@ -1157,8 +1162,8 @@ class ResumoOrdenacaoView(PermissionRequiredMixin, FormView): 'setimo': ordenacao.setimo, 'oitavo': ordenacao.oitavo, 'nono': ordenacao.nono, - 'decimo': ordenacao.decimo} - return self.initial.copy() + 'decimo': ordenacao.decimo}) + return initial def form_valid(self, form): ordenacao = ResumoOrdenacao.objects.get_or_create()[0]