Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue#3396 Obter Integrantes da mesa pela API #3415

Open
wants to merge 15 commits into
base: 3.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sapl/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sapl.api.deprecated import MateriaLegislativaViewSet, SessaoPlenariaViewSet,\
AutoresProvaveisListView, AutoresPossiveisListView, AutorListView,\
ModelChoiceView
from sapl.api.views import SaplApiViewSetConstrutor, AppVersionView, recria_token
from sapl.api.views import SaplApiViewSetConstrutor, AppVersionView, recria_token, get_mesa_diretora

from .apps import AppConfig

Expand Down Expand Up @@ -72,6 +72,8 @@
url(r'^api/', include(urlpatterns_router)),
url(r'^api/version', AppVersionView.as_view()),
url(r'^api/recriar-token/(?P<pk>\d*)$', recria_token, name="recria_token"),
url(r'^api/parlamentares/mesa-diretora',
get_mesa_diretora, name='get_mesa_diretora')

# implementar caminho para autenticação
# https://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/
Expand Down
48 changes: 46 additions & 2 deletions sapl/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.utils.decorators import classonlymethod
from django.utils.translation import ugettext_lazy as _
from django_filters.rest_framework.backends import DjangoFilterBackend
from django.http import JsonResponse
from rest_framework import serializers as rest_serializers
from rest_framework.authtoken.models import Token
from rest_framework.decorators import action, api_view, permission_classes
Expand All @@ -33,7 +34,9 @@
from sapl.protocoloadm.models import DocumentoAdministrativo,\
DocumentoAcessorioAdministrativo, TramitacaoAdministrativo, Anexado
from sapl.sessao.models import SessaoPlenaria, ExpedienteSessao
from sapl.utils import models_with_gr_for_model, choice_anos_com_sessaoplenaria
from sapl.utils import models_with_gr_for_model, choice_anos_com_sessaoplenaria, get_base_url
from sapl.parlamentares.models import (ComposicaoMesa, SessaoLegislativa)
from sapl.parlamentares.views import (partido_parlamentar_sessao_legislativa)


@receiver(post_save, sender=settings.AUTH_USER_MODEL)
Expand All @@ -50,6 +53,47 @@ def recria_token(request, pk):

return Response({"message": "Token recriado com sucesso!", "token": token.key})

def get_mesa_diretora(request):
logger = logging.getLogger(__name__)
edwardoliveira marked this conversation as resolved.
Show resolved Hide resolved

kwargs = {}

legislatura = request.GET.get('legislatura')
if legislatura:
kwargs['legislatura_id'] = legislatura
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coloca um else aqui: se não tiver legislatura informada pega a última ("-data_inicio")


sessao = request.GET.get('sessao')
if sessao:
kwargs['id'] = sessao

sessao_legislativa = SessaoLegislativa.objects.select_related('legislatura').filter(**kwargs).order_by('-data_inicio').first()

if sessao_legislativa is None:
logger.error("Sessão ou legislatura não encontrada!")
return JsonResponse({})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retorna uma mensagem de erro no json. Algo como:

{"error": "Sessão ou legislatura não encontrada!"}


composicao_mesa = ComposicaoMesa.objects.select_related('parlamentar', 'cargo').all().filter(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Não precisa colocar o all() e o filter() ao mesmo tempo. É desnecessário. Ou um ou outro. No caso fica o filter().

sessao_legislativa=sessao_legislativa).order_by('cargo_id')

if composicao_mesa is None:
logger.error("Nenhuma mesa não encontrada!")
return JsonResponse({})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mesma coisa aqui: retorna uma mensagem de erro.




mesa_diretora = [{'legislatura_id':sessao_legislativa.legislatura.id,'legislatura':str(sessao_legislativa.legislatura),
'sessao_legislativa_id':sessao_legislativa.id,'sessao_legislativa':str(sessao_legislativa),
'parlamentar_id':i[0], 'parlamentar_nome':i[1], 'cargo_id':i[2], 'cargo_descricao':i[3]}
for i in composicao_mesa.values_list('parlamentar_id', 'parlamentar__nome_parlamentar',
'cargo_id', 'cargo__descricao')]

for i, c in enumerate(composicao_mesa):
mesa_diretora[i]['fotografia'] = get_base_url(request) + c.parlamentar.fotografia.url

return JsonResponse({
'mesa_diretora':mesa_diretora,
})


class BusinessRulesNotImplementedMixin:
def create(self, request, *args, **kwargs):
Expand Down Expand Up @@ -703,4 +747,4 @@ def get(self, request):
'user': request.user.username,
'is_authenticated': request.user.is_authenticated,
}
return Response(content)
return Response(content)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pq vc tá modificando essa linha? Não precisa. Reverter essa mudança.