From 9579f2f649dbe6b43dd24ee696fe019eb738b023 Mon Sep 17 00:00:00 2001 From: Eliseu Egewarth Date: Mon, 31 Jul 2017 14:35:39 -0300 Subject: [PATCH] Fixes #1290 (#1314) Fixes #1283 Signed-off-by: Eliseu Egewarth --- sapl/protocoloadm/forms.py | 34 ++++++++++++++++------------------ sapl/protocoloadm/views.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/sapl/protocoloadm/forms.py b/sapl/protocoloadm/forms.py index e513d4b1b..c8880960c 100644 --- a/sapl/protocoloadm/forms.py +++ b/sapl/protocoloadm/forms.py @@ -333,7 +333,10 @@ class ProtocoloDocumentForm(ModelForm): class ProtocoloMateriaForm(ModelForm): - autor = forms.IntegerField(widget=forms.HiddenInput(), required=False) + autor = forms.ModelChoiceField(required=True, + empty_label='------', + queryset=Autor.objects.all() + ) tipo_materia = forms.ModelChoiceField( label=_('Tipo de Matéria'), @@ -350,15 +353,6 @@ class ProtocoloMateriaForm(ModelForm): assunto_ementa = forms.CharField(required=True, widget=forms.Textarea, label='Ementa') - def clean_autor(self): - autor_field = self.cleaned_data['autor'] - try: - autor = Autor.objects.get(id=autor_field) - except ObjectDoesNotExist: - autor_field = None - else: - autor_field = autor - return autor_field class Meta: model = Protocolo @@ -368,19 +362,23 @@ class ProtocoloMateriaForm(ModelForm): 'assunto_ementa', 'observacao'] + def clean_autor(self): + autor_field = self.cleaned_data['autor'] + try: + autor = Autor.objects.get(id=autor_field.id) + except ObjectDoesNotExist: + autor_field = None + else: + autor_field = autor + return autor_field + def __init__(self, *args, **kwargs): row1 = to_row( [('tipo_materia', 4), ('numero_paginas', 4)]) row2 = to_row( - [('autor', 0), - (Button('pesquisar', - 'Pesquisar Autor', - css_class='btn btn-primary btn-sm'), 2), - (Button('limpar', - 'limpar Autor', - css_class='btn btn-primary btn-sm'), 10)]) + [('autor', 4)]) row3 = to_row( [('assunto_ementa', 12)]) row4 = to_row( @@ -389,7 +387,7 @@ class ProtocoloMateriaForm(ModelForm): self.helper = FormHelper() self.helper.layout = Layout( Fieldset(_('Identificação da Matéria'), - row1, HTML(autor_label), HTML(autor_modal), row2, row3, + row1, row2, row3, row4, form_actions(save_label='Protocolar Matéria'))) super(ProtocoloMateriaForm, self).__init__( diff --git a/sapl/protocoloadm/views.py b/sapl/protocoloadm/views.py index e11def026..9fffa3669 100644 --- a/sapl/protocoloadm/views.py +++ b/sapl/protocoloadm/views.py @@ -1,8 +1,10 @@ from datetime import date, datetime + from braces.views import FormValidMessageMixin from django.contrib import messages from django.contrib.auth.mixins import PermissionRequiredMixin +from django.db.models import Q from django.core.exceptions import ObjectDoesNotExist from django.core.urlresolvers import reverse from django.db.models import Max @@ -15,6 +17,7 @@ from django.views.generic.base import TemplateView from django_filters.views import FilterView import sapl +from sapl.base.models import Autor from sapl.crud.base import Crud, CrudAux, MasterDetailCrud, make_pagination from sapl.materia.models import MateriaLegislativa, TipoMateriaLegislativa from sapl.utils import create_barcode, get_client_ip @@ -27,6 +30,10 @@ from .forms import (AnularProcoloAdmForm, DocumentoAcessorioAdministrativoForm, from .models import (DocumentoAcessorioAdministrativo, DocumentoAdministrativo, Protocolo, StatusTramitacaoAdministrativo, TipoDocumentoAdministrativo, TramitacaoAdministrativo) +from sapl.parlamentares.models import Parlamentar +from sapl.protocoloadm.models import Protocolo +from sapl.comissoes.models import Comissao +from django.contrib.contenttypes.models import ContentType TipoDocumentoAdministrativoCrud = CrudAux.build( TipoDocumentoAdministrativo, '') @@ -438,6 +445,30 @@ class ProtocoloMateriaView(PermissionRequiredMixin, CreateView): protocolo.save() return redirect(self.get_success_url(protocolo)) + def get_context_data(self, **kwargs): + context = super(CreateView, self).get_context_data(**kwargs) + autores_ativos = self.autores_ativos() + + autores = [] + autores.append(['0', '------']) + for a in autores_ativos: + autores.append([a.id, a.__str__()]) + + context['form'].fields['autor'].choices = autores + return context + + def autores_ativos(self): + lista_parlamentares = Parlamentar.objects.filter(ativo=True).values_list('id', flat=True) + model_parlamentar = ContentType.objects.get_for_model(Parlamentar) + autor_parlamentar = Autor.objects.filter(content_type=model_parlamentar, object_id__in=lista_parlamentares) + + lista_comissoes = Comissao.objects.filter(Q(data_extincao__isnull=True)|Q(data_extincao__gt=date.today())).values_list('id', flat=True) + model_comissao = ContentType.objects.get_for_model(Comissao) + autor_comissoes = Autor.objects.filter(content_type=model_comissao, object_id__in=lista_comissoes) + autores_outros = Autor.objects.exclude(content_type__in=[model_parlamentar, model_comissao]) + q = autor_parlamentar | autor_comissoes | autores_outros + return q + class ProtocoloMateriaTemplateView(PermissionRequiredMixin, TemplateView):