@ -1,9 +1,7 @@
from datetime import datetime
from re import sub
from braces . views import PermissionRequiredMixin
from django . contrib import messages
from django . contrib . auth . decorators import permission_required
from django . core . exceptions import ObjectDoesNotExist , ValidationError
from django . core . urlresolvers import reverse
from django . forms . utils import ErrorList
@ -19,8 +17,10 @@ from django.views.generic.edit import FormMixin
from django_filters . views import FilterView
from rest_framework import generics
from sapl . base . models import AppConfig as AppsAppConfig
from sapl . crud . base import ( RP_DETAIL , RP_LIST , Crud , CrudAux ,
MasterDetailCrud , make_pagination )
MasterDetailCrud ,
PermissionRequiredForAppCrudMixin , make_pagination )
from sapl . materia . forms import pega_ultima_tramitacao
from sapl . materia . models import ( Autoria , DocumentoAcessorio ,
TipoMateriaLegislativa , Tramitacao )
@ -29,8 +29,9 @@ from sapl.norma.models import NormaJuridica
from sapl . parlamentares . models import ( Legislatura , Parlamentar ,
SessaoLegislativa )
from sapl . sessao . apps import AppConfig
from sapl . sessao . forms import ExpedienteMateriaForm , OrdemDiaForm
from sapl . sessao . serializers import SessaoPlenariaSerializer
from sapl . utils import permissoes_painel , permissoes_sessao
from sapl . utils import permission_required_for_app
from . forms import ( AdicionarVariasMateriasFilterSet , ExpedienteForm ,
ListMateriaForm , MesaForm , PautaSessaoFilterSet ,
@ -43,9 +44,8 @@ from .models import (Bancada, Bloco, CargoBancada, CargoMesa,
SessaoPlenariaPresenca , TipoExpediente ,
TipoResultadoVotacao , TipoSessaoPlenaria , VotoParlamentar )
OrdemDiaCrud = Crud . build ( OrdemDia , ' ' )
RegistroVotacaoCrud = Crud . build ( RegistroVotacao , ' ' )
# OrdemDiaCrud = Crud.build(OrdemDia, '')
# RegistroVotacaoCrud = Crud.build(RegistroVotacao, '')
TipoSessaoCrud = CrudAux . build ( TipoSessaoPlenaria , ' tipo_sessao_plenaria ' )
TipoExpedienteCrud = CrudAux . build ( TipoExpediente , ' tipo_expediente ' )
CargoBancadaCrud = CrudAux . build ( CargoBancada , ' ' )
@ -122,18 +122,21 @@ class MateriaOrdemDiaCrud(MasterDetailCrud):
model = OrdemDia
parent_field = ' sessao_plenaria '
help_path = ' '
public = [ RP_LIST , RP_DETAIL ]
class BaseMixin ( MasterDetailCrud . BaseMixin ) :
list_field_names = [ ' numero_ordem ' , ' materia ' , ' observacao ' ,
' resultado ' ]
class CreateView ( MasterDetailCrud . CreateView ) :
form_class = OrdemDiaForm
def get_success_url ( self ) :
return reverse ( ' sapl.sessao:ordemdia_list ' ,
kwargs = { ' pk ' : self . kwargs [ ' pk ' ] } )
class UpdateView ( MasterDetailCrud . UpdateView ) :
form_class = OrdemDiaForm
def get_initial ( self ) :
self . initial [ ' tipo_materia ' ] = self . object . materia . tipo . id
@ -173,11 +176,11 @@ class MateriaOrdemDiaCrud(MasterDetailCrud):
' pk ' : obj . sessao_plenaria_id ,
' oid ' : obj . materia_id ,
' mid ' : obj . pk } )
if self . request . user . has_perms ( permissoes_sessao ( ) ) :
if self . request . user . has_module_ perms ( AppConfig . label ) :
btn_registrar = '''
< a href = " %s "
class = " btn btn-primary "
role = " button " > Registrar Votação < / a > ''' % (
< a href = " %s "
class = " btn btn-primary "
role = " button " > Registrar Votação < / a > ''' % (
url )
obj . resultado = btn_registrar
else :
@ -245,12 +248,13 @@ class ExpedienteMateriaCrud(MasterDetailCrud):
model = ExpedienteMateria
parent_field = ' sessao_plenaria '
help_path = ' '
public = [ RP_LIST , RP_DETAIL ]
class BaseMixin ( MasterDetailCrud . BaseMixin ) :
list_field_names = [ ' numero_ordem ' , ' materia ' ,
' observacao ' , ' resultado ' ]
class ListView ( Crud . PublicMixin , MasterDetailCrud . ListView ) :
class ListView ( MasterDetailCrud . ListView ) :
ordering = [ ' numero_ordem ' , ' materia ' , ' resultado ' ]
def get_rows ( self , object_list ) :
@ -277,16 +281,19 @@ class ExpedienteMateriaCrud(MasterDetailCrud):
' oid ' : obj . materia_id ,
' mid ' : obj . pk } )
btn_registrar = '''
< a href = " %s "
class = " btn btn-primary "
role = " button " > Registrar Votação < / a > ''' % (url)
obj . resultado = btn_registrar
if self . request . user . has_module_perms ( AppConfig . label ) :
btn_registrar = '''
< a href = " %s " class = " btn btn-primary "
role = " button " >
Registrar Votação < / a > ''' % (url)
obj . resultado = btn_registrar
else :
url = reverse ( ' sapl.sessao:abrir_votacao_exp ' , kwargs = {
' pk ' : obj . pk , ' spk ' : obj . sessao_plenaria_id } )
btn_abrir = '''
Matéria não votada < br / >
btn_abrir = ''' Matéria não votada<br /> '''
if self . request . user . has_module_perms ( AppConfig . label ) :
btn_abrir + = '''
< a href = " %s "
class = " btn btn-primary "
role = " button " > Abrir Votação < / a > ''' % (url)
@ -294,35 +301,40 @@ class ExpedienteMateriaCrud(MasterDetailCrud):
obj . resultado = btn_abrir
else :
url = ' '
if obj . tipo_votacao == 1 :
url = reverse ( ' sapl.sessao:votacaosimbolicaexpedit ' ,
kwargs = {
' pk ' : obj . sessao_plenaria_id ,
' oid ' : obj . materia_id ,
' mid ' : obj . pk } )
elif obj . tipo_votacao == 2 :
url = reverse ( ' sapl.sessao:votacaonominalexpedit ' ,
kwargs = {
' pk ' : obj . sessao_plenaria_id ,
' oid ' : obj . materia_id ,
' mid ' : obj . pk } )
elif obj . tipo_votacao == 3 :
url = reverse ( ' sapl.sessao:votacaosecretaexpedit ' ,
kwargs = {
' pk ' : obj . sessao_plenaria_id ,
' oid ' : obj . materia_id ,
' mid ' : obj . pk } )
obj . resultado = ' <a href= " %s " > %s </a> ' % ( url ,
obj . resultado )
if self . request . user . has_module_perms ( AppConfig . label ) :
if obj . tipo_votacao == 1 :
url = reverse (
' sapl.sessao:votacaosimbolicaexpedit ' ,
kwargs = {
' pk ' : obj . sessao_plenaria_id ,
' oid ' : obj . materia_id ,
' mid ' : obj . pk } )
elif obj . tipo_votacao == 2 :
url = reverse ( ' sapl.sessao:votacaonominalexpedit ' ,
kwargs = {
' pk ' : obj . sessao_plenaria_id ,
' oid ' : obj . materia_id ,
' mid ' : obj . pk } )
elif obj . tipo_votacao == 3 :
url = reverse ( ' sapl.sessao:votacaosecretaexpedit ' ,
kwargs = {
' pk ' : obj . sessao_plenaria_id ,
' oid ' : obj . materia_id ,
' mid ' : obj . pk } )
obj . resultado = ' <a href= " %s " > %s </a> ' % ( url ,
obj . resultado )
return [ self . _as_row ( obj ) for obj in object_list ]
class CreateView ( MasterDetailCrud . CreateView ) :
form_class = ExpedienteMateriaForm
def get_success_url ( self ) :
return reverse ( ' sapl.sessao:expedientemateria_list ' ,
kwargs = { ' pk ' : self . kwargs [ ' pk ' ] } )
class UpdateView ( MasterDetailCrud . UpdateView ) :
form_class = ExpedienteMateriaForm
def get_initial ( self ) :
self . initial [ ' tipo_materia ' ] = self . object . materia . tipo . id
@ -403,9 +415,6 @@ class SessaoCrud(Crud):
list_field_names = [ ' data_inicio ' , ' legislatura ' , ' sessao_legislativa ' ,
' tipo ' ]
class CrudDetailView ( DetailView ) :
model = SessaoPlenaria
class ListView ( Crud . ListView ) :
ordering = [ ' -data_inicio ' ]
@ -419,6 +428,13 @@ class SessaoCrud(Crud):
' sessao_legislativa ' : sessao_legislativa }
class SessaoPermissionMixin ( PermissionRequiredForAppCrudMixin ,
FormMixin ,
DetailView ) :
model = SessaoPlenaria
app_label = AppConfig . label ,
class PresencaMixin :
def get_presencas ( self ) :
@ -455,14 +471,13 @@ class PresencaView(FormMixin, PresencaMixin, DetailView):
form_class = PresencaForm
model = SessaoPlenaria
@method_decorator ( permission_required ( (
' %s .add_ %s ' % (
AppConfig . label , SessaoPlenariaPresenca . _meta . model_name ) ,
' %s .change_ %s ' % (
AppConfig . label , SessaoPlenariaPresenca . _meta . model_name ) ,
' %s .delete_ %s ' % (
AppConfig . label , SessaoPlenariaPresenca . _meta . model_name ) ,
) ) )
def get_context_data ( self , * * kwargs ) :
context = FormMixin . get_context_data ( self , * * kwargs )
context [ ' title ' ] = ' %s <small>( %s )</small> ' % (
_ ( ' Presença ' ) , self . object )
return context
@method_decorator ( permission_required_for_app ( AppConfig . label ) )
def post ( self , request , * args , * * kwargs ) :
self . object = self . get_object ( )
form = self . get_form ( )
@ -502,9 +517,33 @@ class PresencaView(FormMixin, PresencaMixin, DetailView):
return reverse ( ' sapl.sessao:presenca ' , kwargs = { ' pk ' : pk } )
class PainelView ( PermissionRequiredMixin , TemplateView ) :
class PainelView ( PermissionRequiredForAppCrud Mixin , TemplateView ) :
template_name = ' sessao/painel.html '
permission_required = permissoes_painel ( )
app_label = ' painel '
def has_permission ( self ) :
painel_aberto = AppsAppConfig . attr ( ' painel_aberto ' )
if painel_aberto and self . request . user . is_anonymous ( ) :
return True
return PermissionRequiredForAppCrudMixin . has_permission ( self )
def get ( self , request , * args , * * kwargs ) :
if request . user . is_anonymous ( ) :
self . template_name = ' painel/index.html '
return TemplateView . get ( self , request , * args , * * kwargs )
def get_context_data ( self , * * kwargs ) :
context = TemplateView . get_context_data ( self , * * kwargs )
context . update ( {
' head_title ' : str ( _ ( ' Painel Plenário ' ) ) ,
' sessao_id ' : kwargs [ ' pk ' ] ,
' root_pk ' : kwargs [ ' pk ' ] ,
' sessaoplenaria ' : SessaoPlenaria . objects . get ( pk = kwargs [ ' pk ' ] ) } )
return context
class PresencaOrdemDiaView ( FormMixin , PresencaMixin , DetailView ) :
@ -512,11 +551,13 @@ class PresencaOrdemDiaView(FormMixin, PresencaMixin, DetailView):
form_class = PresencaForm
model = SessaoPlenaria
@method_decorator ( permission_required ( (
' %s .add_ %s ' % ( AppConfig . label , PresencaOrdemDia . _meta . model_name ) ,
' %s .change_ %s ' % ( AppConfig . label , PresencaOrdemDia . _meta . model_name ) ,
' %s .delete_ %s ' % ( AppConfig . label , PresencaOrdemDia . _meta . model_name ) ,
) ) )
def get_context_data ( self , * * kwargs ) :
context = FormMixin . get_context_data ( self , * * kwargs )
context [ ' title ' ] = ' %s <small>( %s )</small> ' % (
_ ( ' Presença Ordem do Dia ' ) , self . object )
return context
@method_decorator ( permission_required_for_app ( AppConfig . label ) )
def post ( self , request , * args , * * kwargs ) :
self . object = self . get_object ( )
@ -524,9 +565,6 @@ class PresencaOrdemDiaView(FormMixin, PresencaMixin, DetailView):
pk = kwargs [ ' pk ' ]
if not self . request . user . has_perms ( permissoes_sessao ( ) ) :
return self . form_invalid ( form )
if form . is_valid ( ) :
# Pegar os presentes salvos no banco
presentes_banco = PresencaOrdemDia . objects . filter (
@ -599,11 +637,7 @@ class ListMateriaOrdemDiaView(FormMixin, DetailView):
return self . render_to_response ( context )
@method_decorator ( permission_required ( (
' %s .add_ %s ' % ( AppConfig . label , OrdemDia . _meta . model_name ) ,
' %s .change_ %s ' % ( AppConfig . label , OrdemDia . _meta . model_name ) ,
' %s .delete_ %s ' % ( AppConfig . label , OrdemDia . _meta . model_name ) ,
) ) )
@method_decorator ( permission_required_for_app ( AppConfig . label ) )
def post ( self , request , * args , * * kwargs ) :
self . object = self . get_object ( )
context = self . get_context_data ( object = self . object )
@ -694,11 +728,13 @@ class MesaView(FormMixin, DetailView):
return self . render_to_response ( context )
@method_decorator ( permission_required ( (
' %s .add_integrantemesa ' % AppConfig . label ,
' %s .change_integrantemesa ' % AppConfig . label ,
' %s .delete_integrantemesa ' % AppConfig . label ,
) ) )
def get_context_data ( self , * * kwargs ) :
context = FormMixin . get_context_data ( self , * * kwargs )
context [ ' title ' ] = ' %s <small>( %s )</small> ' % (
_ ( ' Mesa Diretora ' ) , self . object )
return context
@method_decorator ( permission_required_for_app ( AppConfig . label ) )
def post ( self , request , * args , * * kwargs ) :
self . object = self . get_object ( )
form = MesaForm ( request . POST )
@ -761,7 +797,6 @@ class MesaView(FormMixin, DetailView):
lista_cargos_ocupados . append ( cargo )
lista = list ( set ( lista_cargos ) - set ( lista_cargos_ocupados ) )
lista . sort ( key = lambda x : x . descricao )
return lista
def get_success_url ( self ) :
@ -769,8 +804,9 @@ class MesaView(FormMixin, DetailView):
return reverse ( ' sapl.sessao:mesa ' , kwargs = { ' pk ' : pk } )
class ResumoView ( SessaoCrud . Crud DetailView) :
class ResumoView ( DetailView ) :
template_name = ' sessao/resumo.html '
model = SessaoPlenaria
def get ( self , request , * args , * * kwargs ) :
self . object = self . get_object ( )
@ -941,18 +977,22 @@ class ResumoView(SessaoCrud.CrudDetailView):
return self . render_to_response ( context )
class ExpedienteView ( FormMixin ,
SessaoCrud . CrudDetailView ) :
class ExpedienteView ( FormMixin , DetailView ) :
template_name = ' sessao/expediente.html '
form_class = ExpedienteForm
model = SessaoPlenaria
def get_context_data ( self , * * kwargs ) :
context = FormMixin . get_context_data ( self , * * kwargs )
context [ ' title ' ] = ' %s <small>( %s )</small> ' % (
_ ( ' Expediente Diversos ' ) , self . object )
return context
@method_decorator ( permission_required_for_app ( AppConfig . label ) )
def post ( self , request , * args , * * kwargs ) :
self . object = self . get_object ( )
form = ExpedienteForm ( request . POST )
if not self . request . user . has_perms ( permissoes_sessao ( ) ) :
return self . form_invalid ( form )
if form . is_valid ( ) :
list_tipo = request . POST . getlist ( ' tipo ' )
list_conteudo = request . POST . getlist ( ' conteudo ' )
@ -1009,16 +1049,13 @@ class ExpedienteView(FormMixin,
return reverse ( ' sapl.sessao:expediente ' , kwargs = { ' pk ' : pk } )
class VotacaoEditView ( PermissionRequiredMixin ,
FormMixin ,
SessaoCrud . CrudDetailView ) :
class VotacaoEditView ( SessaoPermissionMixin ) :
'''
Votação Simbólica e Secreta
'''
template_name = ' sessao/votacao/votacao_edit.html '
permission_required = permissoes_sessao ( )
def post ( self , request , * args , * * kwargs ) :
@ -1085,9 +1122,7 @@ class VotacaoEditView(PermissionRequiredMixin,
kwargs = { ' pk ' : pk } )
class VotacaoView ( PermissionRequiredMixin ,
FormMixin ,
SessaoCrud . CrudDetailView ) :
class VotacaoView ( SessaoPermissionMixin ) :
'''
Votação Simbólica e Secreta
@ -1095,7 +1130,6 @@ class VotacaoView(PermissionRequiredMixin,
template_name = ' sessao/votacao/votacao.html '
form_class = VotacaoForm
permission_required = permissoes_sessao ( )
def get ( self , request , * args , * * kwargs ) :
self . object = self . get_object ( )
@ -1207,11 +1241,8 @@ class VotacaoView(PermissionRequiredMixin,
kwargs = { ' pk ' : pk } )
class VotacaoNominalView ( PermissionRequiredMixin ,
FormMixin ,
SessaoCrud . CrudDetailView ) :
class VotacaoNominalView ( SessaoPermissionMixin ) :
template_name = ' sessao/votacao/nominal.html '
permission_required = permissoes_sessao ( )
def get ( self , request , * args , * * kwargs ) :
ordem_id = kwargs [ ' mid ' ]
@ -1335,11 +1366,8 @@ class VotacaoNominalView(PermissionRequiredMixin,
kwargs = { ' pk ' : pk } )
class VotacaoNominalEditView ( PermissionRequiredMixin ,
FormMixin ,
SessaoCrud . CrudDetailView ) :
class VotacaoNominalEditView ( SessaoPermissionMixin ) :
template_name = ' sessao/votacao/nominal_edit.html '
permission_required = permissoes_sessao ( )
def get ( self , request , * args , * * kwargs ) :
context = { }
@ -1415,11 +1443,8 @@ class VotacaoNominalEditView(PermissionRequiredMixin,
kwargs = { ' pk ' : pk } )
class VotacaoNominalExpedienteView ( PermissionRequiredMixin ,
FormMixin ,
SessaoCrud . CrudDetailView ) :
class VotacaoNominalExpedienteView ( SessaoPermissionMixin ) :
template_name = ' sessao/votacao/nominal.html '
permission_required = permissoes_sessao ( )
def get ( self , request , * args , * * kwargs ) :
expediente_id = kwargs [ ' mid ' ]
@ -1541,11 +1566,8 @@ class VotacaoNominalExpedienteView(PermissionRequiredMixin,
kwargs = { ' pk ' : pk } )
class VotacaoNominalExpedienteEditView ( PermissionRequiredMixin ,
FormMixin ,
SessaoCrud . CrudDetailView ) :
class VotacaoNominalExpedienteEditView ( SessaoPermissionMixin ) :
template_name = ' sessao/votacao/nominal_edit.html '
permission_required = permissoes_sessao ( )
def get ( self , request , * args , * * kwargs ) :
context = { }
@ -1620,9 +1642,7 @@ class VotacaoNominalExpedienteEditView(PermissionRequiredMixin,
kwargs = { ' pk ' : pk } )
class VotacaoExpedienteView ( PermissionRequiredMixin ,
FormMixin ,
SessaoCrud . CrudDetailView ) :
class VotacaoExpedienteView ( SessaoPermissionMixin ) :
'''
Votação Simbólica e Secreta
@ -1630,7 +1650,6 @@ class VotacaoExpedienteView(PermissionRequiredMixin,
template_name = ' sessao/votacao/votacao.html '
form_class = VotacaoForm
permission_required = permissoes_sessao ( )
def get ( self , request , * args , * * kwargs ) :
self . object = self . get_object ( )
@ -1744,9 +1763,7 @@ class VotacaoExpedienteView(PermissionRequiredMixin,
kwargs = { ' pk ' : pk } )
class VotacaoExpedienteEditView ( PermissionRequiredMixin ,
FormMixin ,
SessaoCrud . CrudDetailView ) :
class VotacaoExpedienteEditView ( SessaoPermissionMixin ) :
'''
Votação Simbólica e Secreta
@ -1754,7 +1771,6 @@ class VotacaoExpedienteEditView(PermissionRequiredMixin,
template_name = ' sessao/votacao/votacao_edit.html '
form_class = VotacaoEditForm
permission_required = permissoes_sessao ( )
def get_success_url ( self ) :
pk = self . kwargs [ ' pk ' ]
@ -1845,8 +1861,9 @@ class PautaSessaoListView(SessaoListView):
template_name = " sessao/pauta_sessao_list.html "
class PautaSessaoDetailView ( SessaoCrud . Crud DetailView) :
class PautaSessaoDetailView ( DetailView ) :
template_name = " sessao/pauta_sessao_detail.html "
model = SessaoPlenaria
def get ( self , request , * args , * * kwargs ) :
self . object = self . get_object ( )
@ -1956,8 +1973,9 @@ class SessaoPlenariaView(generics.ListAPIView):
serializer_class = SessaoPlenariaSerializer
class PautaExpedienteDetail ( SessaoCrud . Crud DetailView) :
class PautaExpedienteDetail ( DetailView ) :
template_name = " sessao/pauta/expediente.html "
model = SessaoPlenaria
def get ( self , request , * args , * * kwargs ) :
pk = self . kwargs [ ' pk ' ]
@ -1974,8 +1992,9 @@ class PautaExpedienteDetail(SessaoCrud.CrudDetailView):
' tramitacao ' : tramitacao } )
class PautaOrdemDetail ( SessaoCrud . Crud DetailView) :
class PautaOrdemDetail ( DetailView ) :
template_name = " sessao/pauta/ordem.html "
model = SessaoPlenaria
def get ( self , request , * args , * * kwargs ) :
pk = self . kwargs [ ' pk ' ]
@ -2073,11 +2092,11 @@ def retira_materias_ja_adicionadas(id_sessao, model):
return lista_id_materias
class AdicionarVariasMateriasExpediente ( PermissionRequiredMixin ,
class AdicionarVariasMateriasExpediente ( PermissionRequiredForAppCrud Mixin ,
MateriaLegislativaPesquisaView ) :
filterset_class = AdicionarVariasMateriasFilterSet
template_name = ' sessao/adicionar_varias_materias_expediente.html '
permission_required = permissoes_sessao ( )
app_label = AppConfig . label
def get_filterset_kwargs ( self , filterset_class ) :
super ( AdicionarVariasMateriasExpediente ,
@ -2138,6 +2157,8 @@ class AdicionarVariasMateriasExpediente(PermissionRequiredMixin,
expediente = ExpedienteMateria ( )
expediente . sessao_plenaria_id = self . kwargs [ ' pk ' ]
expediente . materia_id = materia . id
expediente . observacao = MateriaLegislativa . objects . get (
pk = materia . id ) . ementa
if lista_materias_expediente :
posicao = lista_materias_expediente . last ( ) . numero_ordem + 1
expediente . numero_ordem = posicao
@ -2147,13 +2168,15 @@ class AdicionarVariasMateriasExpediente(PermissionRequiredMixin,
expediente . tipo_votacao = request . POST [ ' tipo_votacao_ %s ' % m ]
expediente . save ( )
return self . get ( request , self . kwargs )
pk = self . kwargs [ ' pk ' ]
return HttpResponseRedirect (
reverse ( ' sapl.sessao:expedientemateria_list ' , kwargs = { ' pk ' : pk } ) )
class AdicionarVariasMateriasOrdemDia ( AdicionarVariasMateriasExpediente ) :
filterset_class = AdicionarVariasMateriasFilterSet
template_name = ' sessao/adicionar_varias_materias_ordem.html '
permission_required = permissoes_sessao ( )
def get_filterset_kwargs ( self , filterset_class ) :
super ( AdicionarVariasMateriasExpediente ,
@ -2199,6 +2222,8 @@ class AdicionarVariasMateriasOrdemDia(AdicionarVariasMateriasExpediente):
ordem_dia = OrdemDia ( )
ordem_dia . sessao_plenaria_id = self . kwargs [ ' pk ' ]
ordem_dia . materia_id = materia . id
ordem_dia . observacao = MateriaLegislativa . objects . get (
pk = materia . id ) . ementa
if lista_materias_ordem_dia :
posicao = lista_materias_ordem_dia . last ( ) . numero_ordem + 1
ordem_dia . numero_ordem = posicao