|
|
|
from itertools import chain
|
|
|
|
|
|
|
|
from django.contrib.auth.mixins import PermissionRequiredMixin
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from django.db.models import Count, Q
|
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from django.views.generic.base import TemplateView
|
|
|
|
from django_filters.views import FilterView
|
|
|
|
|
|
|
|
from sapl.crud.base import (Crud, CrudBaseMixin, CrudCreateView,
|
|
|
|
CrudDetailView, CrudUpdateView)
|
|
|
|
from sapl.materia.models import MateriaLegislativa, TipoMateriaLegislativa
|
|
|
|
from sapl.parlamentares.models import Parlamentar
|
|
|
|
from sapl.sessao.models import (OrdemDia, PresencaOrdemDia, SessaoPlenaria,
|
|
|
|
SessaoPlenariaPresenca)
|
|
|
|
from sapl.utils import permissao_tb_aux
|
|
|
|
|
|
|
|
from .forms import (CasaLegislativaForm, RelatorioHistoricoTramitacaoFilterSet,
|
|
|
|
RelatorioMateriasPorAnoAutorTipoFilterSet,
|
|
|
|
RelatorioMateriasPorAutorFilterSet,
|
|
|
|
RelatorioMateriasTramitacaoilterSet,
|
|
|
|
RelatorioPresencaSessaoFilterSet)
|
|
|
|
from .models import CasaLegislativa
|
|
|
|
|
|
|
|
|
|
|
|
def get_casalegislativa():
|
|
|
|
return CasaLegislativa.objects.first()
|
|
|
|
|
|
|
|
|
|
|
|
class RelatorioPresencaSessaoView(FilterView):
|
|
|
|
model = SessaoPlenaria
|
|
|
|
filterset_class = RelatorioPresencaSessaoFilterSet
|
|
|
|
template_name = 'base/RelatorioPresencaSessao_filter.html'
|
|
|
|
|
|
|
|
def calcular_porcentagem_presenca(self,
|
|
|
|
parlamentares,
|
|
|
|
total_sessao,
|
|
|
|
total_ordemdia):
|
|
|
|
for p in parlamentares:
|
|
|
|
p.sessao_porc = round(p.sessao_count * 100 / total_sessao, 1)
|
|
|
|
p.ordemdia_porc = round(p.ordemdia_count * 100 / total_ordemdia, 1)
|
|
|
|
return parlamentares
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(RelatorioPresencaSessaoView,
|
|
|
|
self).get_context_data(**kwargs)
|
|
|
|
context['title'] = _('Presença dos parlamentares nas sessões')
|
|
|
|
# =====================================================================
|
|
|
|
if 'salvar' in self.request.GET:
|
|
|
|
where = context['object_list'].query.where
|
|
|
|
_range = where.children[0].rhs
|
|
|
|
|
|
|
|
sufixo = 'sessao_plenaria__data_inicio__range'
|
|
|
|
param0 = {'%s' % sufixo: _range}
|
|
|
|
param1 = {'presencaordemdia__%s' % sufixo: _range}
|
|
|
|
param2 = {'sessaoplenariapresenca__%s' % sufixo: _range}
|
|
|
|
|
|
|
|
pls = Parlamentar.objects.filter(
|
|
|
|
Q(**param1) & Q(**param2)).annotate(
|
|
|
|
sessao_count=Count(
|
|
|
|
'sessaoplenariapresenca__sessao_plenaria__data_inicio',
|
|
|
|
distinct=True),
|
|
|
|
ordemdia_count=Count(
|
|
|
|
'presencaordemdia__sessao_plenaria__data_inicio',
|
|
|
|
distinct=True),
|
|
|
|
sessao_porc=Count(0),
|
|
|
|
ordemdia_porc=Count(0))
|
|
|
|
|
|
|
|
total_ordemdia = OrdemDia.objects.order_by(
|
|
|
|
'sessao_plenaria').filter(**param0).distinct(
|
|
|
|
'sessao_plenaria').count()
|
|
|
|
|
|
|
|
self.calcular_porcentagem_presenca(pls,
|
|
|
|
context['object_list'].count(),
|
|
|
|
total_ordemdia)
|
|
|
|
|
|
|
|
context['total_ordemdia'] = total_ordemdia
|
|
|
|
context['total_sessao'] = context['object_list'].count()
|
|
|
|
context['parlamentares'] = pls
|
|
|
|
context['periodo'] = (self.request.GET['data_inicio_0'] +
|
|
|
|
' - ' + self.request.GET['data_inicio_1'])
|
|
|
|
# =====================================================================
|
|
|
|
qr = self.request.GET.copy()
|
|
|
|
context['filter_url'] = ('&' + qr.urlencode()) if len(qr) > 0 else ''
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class RelatorioHistoricoTramitacaoView(FilterView):
|
|
|
|
model = MateriaLegislativa
|
|
|
|
filterset_class = RelatorioHistoricoTramitacaoFilterSet
|
|
|
|
template_name = 'base/RelatorioHistoricoTramitacao_filter.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(RelatorioHistoricoTramitacaoView,
|
|
|
|
self).get_context_data(**kwargs)
|
|
|
|
context['title'] = _('Histórico de Tramitações')
|
|
|
|
qr = self.request.GET.copy()
|
|
|
|
context['filter_url'] = ('&' + qr.urlencode()) if len(qr) > 0 else ''
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class RelatorioMateriasTramitacaoView(FilterView):
|
|
|
|
model = MateriaLegislativa
|
|
|
|
filterset_class = RelatorioMateriasTramitacaoilterSet
|
|
|
|
template_name = 'base/RelatorioMateriasPorTramitacao_filter.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(RelatorioMateriasTramitacaoView,
|
|
|
|
self).get_context_data(**kwargs)
|
|
|
|
|
|
|
|
context['title'] = _('Matérias por Ano, Autor e Tipo')
|
|
|
|
|
|
|
|
qs = context['object_list']
|
|
|
|
qs = qs.filter(em_tramitacao=True)
|
|
|
|
context['object_list'] = qs
|
|
|
|
|
|
|
|
qtdes = {}
|
|
|
|
for tipo in TipoMateriaLegislativa.objects.all():
|
|
|
|
qs = context['object_list']
|
|
|
|
qtde = len(qs.filter(tipo_id=tipo.id))
|
|
|
|
if qtde > 0:
|
|
|
|
qtdes[tipo] = qtde
|
|
|
|
context['qtdes'] = qtdes
|
|
|
|
|
|
|
|
qr = self.request.GET.copy()
|
|
|
|
context['filter_url'] = ('&' + qr.urlencode()) if len(qr) > 0 else ''
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class RelatorioMateriasPorAnoAutorTipoView(FilterView):
|
|
|
|
model = MateriaLegislativa
|
|
|
|
filterset_class = RelatorioMateriasPorAnoAutorTipoFilterSet
|
|
|
|
template_name = 'base/RelatorioMateriasPorAnoAutorTipo_filter.html'
|
|
|
|
|
|
|
|
def get_filterset_kwargs(self, filterset_class):
|
|
|
|
super(RelatorioMateriasPorAnoAutorTipoView,
|
|
|
|
self).get_filterset_kwargs(filterset_class)
|
|
|
|
|
|
|
|
kwargs = {'data': self.request.GET or None}
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(RelatorioMateriasPorAnoAutorTipoView,
|
|
|
|
self).get_context_data(**kwargs)
|
|
|
|
|
|
|
|
context['title'] = _('Matérias por Ano, Autor e Tipo')
|
|
|
|
|
|
|
|
qtdes = {}
|
|
|
|
for tipo in TipoMateriaLegislativa.objects.all():
|
|
|
|
qs = kwargs['object_list']
|
|
|
|
qtde = len(qs.filter(tipo_id=tipo.id))
|
|
|
|
if qtde > 0:
|
|
|
|
qtdes[tipo] = qtde
|
|
|
|
context['qtdes'] = qtdes
|
|
|
|
|
|
|
|
qr = self.request.GET.copy()
|
|
|
|
context['filter_url'] = ('&' + qr.urlencode()) if len(qr) > 0 else ''
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class RelatorioMateriasPorAutorView(FilterView):
|
|
|
|
model = MateriaLegislativa
|
|
|
|
filterset_class = RelatorioMateriasPorAutorFilterSet
|
|
|
|
template_name = 'base/RelatorioMateriasPorAutor_filter.html'
|
|
|
|
|
|
|
|
def get_filterset_kwargs(self, filterset_class):
|
|
|
|
super(RelatorioMateriasPorAutorView,
|
|
|
|
self).get_filterset_kwargs(filterset_class)
|
|
|
|
|
|
|
|
kwargs = {'data': self.request.GET or None}
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(RelatorioMateriasPorAutorView,
|
|
|
|
self).get_context_data(**kwargs)
|
|
|
|
|
|
|
|
context['title'] = _('Matérias por Autor')
|
|
|
|
|
|
|
|
qtdes = {}
|
|
|
|
for tipo in TipoMateriaLegislativa.objects.all():
|
|
|
|
qs = kwargs['object_list']
|
|
|
|
qtde = len(qs.filter(tipo_id=tipo.id))
|
|
|
|
if qtde > 0:
|
|
|
|
qtdes[tipo] = qtde
|
|
|
|
context['qtdes'] = qtdes
|
|
|
|
|
|
|
|
qr = self.request.GET.copy()
|
|
|
|
context['filter_url'] = ('&' + qr.urlencode()) if len(qr) > 0 else ''
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class CasaLegislativaCrud(Crud):
|
|
|
|
model = CasaLegislativa
|
|
|
|
help_path = ''
|
|
|
|
|
|
|
|
class BaseMixin(PermissionRequiredMixin, CrudBaseMixin):
|
|
|
|
list_field_names = ['codigo', 'nome', 'sigla']
|
|
|
|
|
|
|
|
def has_permission(self):
|
|
|
|
if self.request.user.is_superuser:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
class CreateView(PermissionRequiredMixin, CrudCreateView):
|
|
|
|
permission_required = {'base.add_casa_legislativa'}
|
|
|
|
form_class = CasaLegislativaForm
|
|
|
|
|
|
|
|
class UpdateView(PermissionRequiredMixin, CrudUpdateView):
|
|
|
|
permission_required = {'base.change_casalegislativa'}
|
|
|
|
form_class = CasaLegislativaForm
|
|
|
|
|
|
|
|
class DetailView(CrudDetailView):
|
|
|
|
form_class = CasaLegislativaForm
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse('sapl.base:casalegislativa_update',
|
|
|
|
kwargs={'pk': self.kwargs['pk']}))
|
|
|
|
|
|
|
|
|
|
|
|
class HelpView(PermissionRequiredMixin, TemplateView):
|
|
|
|
# XXX treat non existing template as a 404!!!!
|
|
|
|
|
|
|
|
def get_template_names(self):
|
|
|
|
return ['ajuda/%s.html' % self.kwargs['topic']]
|
|
|
|
|
|
|
|
|
|
|
|
class SistemaView(PermissionRequiredMixin, TemplateView):
|
|
|
|
template_name = 'sistema.html'
|
|
|
|
permission_required = ''
|
|
|
|
|
|
|
|
def has_permission(self):
|
|
|
|
return permissao_tb_aux(self)
|