diff --git a/sapl/painel/views.py b/sapl/painel/views.py index 65acfec9e..4f10135d1 100644 --- a/sapl/painel/views.py +++ b/sapl/painel/views.py @@ -82,21 +82,13 @@ def get_cronometro_status(request, name): def get_materia_aberta(pk): - try: - materia = OrdemDia.objects.filter( - sessao_plenaria_id=pk, votacao_aberta=True).last() - return materia - except ObjectDoesNotExist: - return None + return OrdemDia.objects.filter( + sessao_plenaria_id=pk, votacao_aberta=True).last() def get_last_materia(pk): - try: - materia = OrdemDia.objects.filter( - sessao_plenaria_id=pk).last() - return materia - except ObjectDoesNotExist: - return None + return OrdemDia.objects.filter( + sessao_plenaria_id=pk).last() def get_presentes(pk, response, materia): @@ -136,11 +128,14 @@ def get_presentes(pk, response, materia): num_presentes_ordem_dia = len(presentes_ordem_dia) if materia.tipo_votacao == 1: - tipo_votacao = 'Simbólica' + tipo_votacao = str(_('Simbólica')) + response = get_votos(response, materia) elif materia.tipo_votacao == 2: tipo_votacao = 'Nominal' + response = get_votos_nominal(response, materia) elif materia.tipo_votacao == 3: tipo_votacao = 'Secreta' + response = get_votos(response, materia) response.update({ 'presentes_ordem_dia': presentes_ordem_dia, @@ -149,10 +144,6 @@ def get_presentes(pk, response, materia): 'num_presentes_sessao_plenaria': num_presentes_sessao_plen, 'status_painel': 'ABERTO', 'msg_painel': str(_('Votação aberta!')), - 'numero_votos_sim': 0, - 'numero_votos_nao': 0, - 'numero_abstencoes': 0, - 'total_votos': 0, 'tipo_resultado': tipo_votacao, 'observacao_materia': materia.observacao, 'materia_legislativa_texto': str(materia.materia)}) @@ -164,21 +155,13 @@ def get_presentes(pk, response, materia): def get_materia_expediente_aberta(pk): - try: - materia = ExpedienteMateria.objects.filter( - sessao_plenaria_id=pk, votacao_aberta=True).last() - return materia - except ObjectDoesNotExist: - return None + return ExpedienteMateria.objects.filter( + sessao_plenaria_id=pk, votacao_aberta=True).last() def get_last_materia_expediente(pk): - try: - materia = ExpedienteMateria.objects.filter( - sessao_plenaria_id=pk).last() - return materia - except ObjectDoesNotExist: - return None + return ExpedienteMateria.objects.filter( + sessao_plenaria_id=pk).last() def get_presentes_expediente(pk, response, materia): @@ -219,10 +202,14 @@ def get_presentes_expediente(pk, response, materia): if materia.tipo_votacao == 1: tipo_votacao = 'Simbólica' + response = get_votos(response, materia) elif materia.tipo_votacao == 2: tipo_votacao = 'Nominal' + response = get_votos_nominal(response, materia) elif materia.tipo_votacao == 3: tipo_votacao = 'Secreta' + response = get_votos(response, materia) + response.update({ 'presentes_expediente': presentes_expediente, 'num_presentes_expediente': num_presentes_expediente, @@ -230,10 +217,6 @@ def get_presentes_expediente(pk, response, materia): 'num_presentes_sessao_plenaria': num_presentes_sessao_plen, 'status_painel': str(_('ABERTO')), 'msg_painel': str(_('Votação aberta!')), - 'numero_votos_sim': 0, - 'numero_votos_nao': 0, - 'numero_abstencoes': 0, - 'total_votos': 0, 'tipo_resultado': tipo_votacao, 'observacao_materia': materia.observacao, 'materia_legislativa_texto': str(materia.materia)}) @@ -262,17 +245,28 @@ def get_votos(response, materia): registro = RegistroVotacao.objects.filter( ordem=materia, materia=materia.materia).last() - total = (registro.numero_votos_sim + - registro.numero_votos_nao + - registro.numero_abstencoes) - response.update({ - 'numero_votos_sim': registro.numero_votos_sim, - 'numero_votos_nao': registro.numero_votos_nao, - 'numero_abstencoes': registro.numero_abstencoes, - 'total_votos': total, - 'tipo_votacao': tipo_votacao, - 'tipo_resultado': registro.tipo_resultado_votacao.nome, - }) + + if registro: + total = (registro.numero_votos_sim + + registro.numero_votos_nao + + registro.numero_abstencoes) + response.update({ + 'numero_votos_sim': registro.numero_votos_sim, + 'numero_votos_nao': registro.numero_votos_nao, + 'numero_abstencoes': registro.numero_abstencoes, + 'total_votos': total, + 'tipo_votacao': tipo_votacao, + 'tipo_resultado': registro.tipo_resultado_votacao.nome, + }) + else: + response.update({ + 'numero_votos_sim': 0, + 'numero_votos_nao': 0, + 'numero_abstencoes': 0, + 'total_votos': 0, + 'tipo_votacao': tipo_votacao, + 'tipo_resultado': 'Ainda não foi votada.', + }) return response @@ -286,48 +280,61 @@ def get_votos_nominal(response, materia): elif materia.tipo_votacao == 3: tipo_votacao = 'Secreta' - registro = RegistroVotacao.objects.get( - ordem=materia, materia=materia.materia) - - votos_parlamentares = VotoParlamentar.objects.filter( - votacao_id=registro.id) - - filiacao = Filiacao.objects.filter( - data_desfiliacao__isnull=True, parlamentar__ativo=True) - parlamentar_partido = {} - for f in filiacao: - parlamentar_partido[ - f.parlamentar.nome_parlamentar] = f.partido.sigla - - for v in votos_parlamentares: - try: - parlamentar_partido[v.parlamentar.nome_parlamentar] - except KeyError: - votos.update({v.parlamentar.id: { - 'parlamentar': v.parlamentar.nome_parlamentar, - 'voto': str(v.voto), - 'partido': str(_('Sem Registro')) - }}) - else: - votos.update({v.parlamentar.id: { - 'parlamentar': v.parlamentar.nome_parlamentar, - 'voto': str(v.voto), - 'partido': parlamentar_partido[v.parlamentar.nome_parlamentar] - }}) - - total = (registro.numero_votos_sim + - registro.numero_votos_nao + - registro.numero_abstencoes) + try: + registro = RegistroVotacao.objects.get( + ordem=materia, materia=materia.materia) + except ObjectDoesNotExist: + response.update({ + 'numero_votos_sim': 0, + 'numero_votos_nao': 0, + 'numero_abstencoes': 0, + 'total_votos': 0, + 'tipo_votacao': tipo_votacao, + 'tipo_resultado': 'Não foi votado ainda', + 'votos': None + }) - response.update({ - 'numero_votos_sim': registro.numero_votos_sim, - 'numero_votos_nao': registro.numero_votos_nao, - 'numero_abstencoes': registro.numero_abstencoes, - 'total_votos': total, - 'tipo_votacao': tipo_votacao, - 'tipo_resultado': registro.tipo_resultado_votacao.nome, - 'votos': votos - }) + else: + votos_parlamentares = VotoParlamentar.objects.filter( + votacao_id=registro.id) + + filiacao = Filiacao.objects.filter( + data_desfiliacao__isnull=True, parlamentar__ativo=True) + parlamentar_partido = {} + for f in filiacao: + parlamentar_partido[ + f.parlamentar.nome_parlamentar] = f.partido.sigla + + for v in votos_parlamentares: + try: + parlamentar_partido[v.parlamentar.nome_parlamentar] + except KeyError: + votos.update({v.parlamentar.id: { + 'parlamentar': v.parlamentar.nome_parlamentar, + 'voto': str(v.voto), + 'partido': str(_('Sem Registro')) + }}) + else: + votos.update({v.parlamentar.id: { + 'parlamentar': v.parlamentar.nome_parlamentar, + 'voto': str(v.voto), + 'partido': parlamentar_partido[ + v.parlamentar.nome_parlamentar] + }}) + + total = (registro.numero_votos_sim + + registro.numero_votos_nao + + registro.numero_abstencoes) + + response.update({ + 'numero_votos_sim': registro.numero_votos_sim, + 'numero_votos_nao': registro.numero_votos_nao, + 'numero_abstencoes': registro.numero_abstencoes, + 'total_votos': total, + 'tipo_votacao': tipo_votacao, + 'tipo_resultado': registro.tipo_resultado_votacao.nome, + 'votos': votos + }) return response diff --git a/sapl/sessao/views.py b/sapl/sessao/views.py index cac20bece..cda3229a2 100644 --- a/sapl/sessao/views.py +++ b/sapl/sessao/views.py @@ -2,7 +2,8 @@ from datetime import datetime from re import sub from django.contrib import messages -from django.core.exceptions import ObjectDoesNotExist, ValidationError +from django.core.exceptions import (ObjectDoesNotExist, MultipleObjectsReturned, + ValidationError) from django.core.urlresolvers import reverse from django.forms.utils import ErrorList from django.http import JsonResponse @@ -1855,9 +1856,14 @@ class VotacaoExpedienteEditView(SessaoPermissionMixin): 'ementa': expediente.observacao} context.update({'materia': materia}) - votacao = RegistroVotacao.objects.get( - materia_id=materia_id, - expediente_id=expediente_id) + try: + votacao = RegistroVotacao.objects.get( + materia_id=materia_id, + expediente_id=expediente_id) + except MultipleObjectsReturned: + votacao = RegistroVotacao.objects.get( + materia_id=materia_id, + expediente_id=expediente_id).last() votacao_existente = {'observacao': sub( ' ', ' ', strip_tags(votacao.observacao)), 'tipo_resultado':