|
@ -3,11 +3,13 @@ import logging |
|
|
from django import apps |
|
|
from django import apps |
|
|
from django.conf import settings |
|
|
from django.conf import settings |
|
|
from django.contrib.contenttypes.models import ContentType |
|
|
from django.contrib.contenttypes.models import ContentType |
|
|
from django.urls import reverse_lazy |
|
|
from django.core.exceptions import ObjectDoesNotExist |
|
|
from django.db.models import Q |
|
|
from django.db.models import Q |
|
|
from django.db.models.fields.files import FileField |
|
|
from django.db.models.fields.files import FileField |
|
|
from django.db.models.signals import post_save |
|
|
from django.db.models.signals import post_save |
|
|
from django.dispatch import receiver |
|
|
from django.dispatch import receiver |
|
|
|
|
|
from django.urls import reverse_lazy |
|
|
|
|
|
from django.utils import timezone |
|
|
from django.utils.decorators import classonlymethod |
|
|
from django.utils.decorators import classonlymethod |
|
|
from django.utils.text import capfirst |
|
|
from django.utils.text import capfirst |
|
|
from django.utils.translation import ugettext_lazy as _ |
|
|
from django.utils.translation import ugettext_lazy as _ |
|
@ -16,17 +18,14 @@ from django_filters.filters import CharFilter |
|
|
from django_filters.rest_framework.backends import DjangoFilterBackend |
|
|
from django_filters.rest_framework.backends import DjangoFilterBackend |
|
|
from django_filters.rest_framework.filterset import FilterSet |
|
|
from django_filters.rest_framework.filterset import FilterSet |
|
|
from django_filters.utils import resolve_field |
|
|
from django_filters.utils import resolve_field |
|
|
from django.utils import timezone |
|
|
|
|
|
from django.core.exceptions import ObjectDoesNotExist |
|
|
|
|
|
from rest_framework import serializers as rest_serializers |
|
|
from rest_framework import serializers as rest_serializers |
|
|
from rest_framework.authtoken.models import Token |
|
|
from rest_framework.authtoken.models import Token |
|
|
from rest_framework.decorators import action, api_view, permission_classes |
|
|
from rest_framework.decorators import action, api_view, permission_classes |
|
|
from rest_framework.fields import SerializerMethodField |
|
|
from rest_framework.fields import SerializerMethodField |
|
|
from rest_framework.response import Response |
|
|
|
|
|
from rest_framework.viewsets import ModelViewSet |
|
|
|
|
|
from rest_framework.permissions import IsAuthenticated, IsAdminUser |
|
|
from rest_framework.permissions import IsAuthenticated, IsAdminUser |
|
|
|
|
|
from rest_framework.response import Response |
|
|
from rest_framework.views import APIView |
|
|
from rest_framework.views import APIView |
|
|
|
|
|
from rest_framework.viewsets import ModelViewSet |
|
|
|
|
|
|
|
|
from sapl.api.forms import SaplFilterSetMixin |
|
|
from sapl.api.forms import SaplFilterSetMixin |
|
|
from sapl.api.permissions import SaplModelPermissions |
|
|
from sapl.api.permissions import SaplModelPermissions |
|
@ -35,12 +34,12 @@ from sapl.base.models import Autor, AppConfig, DOC_ADM_OSTENSIVO |
|
|
from sapl.materia.models import Proposicao, TipoMateriaLegislativa,\ |
|
|
from sapl.materia.models import Proposicao, TipoMateriaLegislativa,\ |
|
|
MateriaLegislativa, Tramitacao |
|
|
MateriaLegislativa, Tramitacao |
|
|
from sapl.norma.models import NormaJuridica |
|
|
from sapl.norma.models import NormaJuridica |
|
|
|
|
|
from sapl.parlamentares.models import Mandato, Parlamentar, Legislatura |
|
|
from sapl.parlamentares.models import Parlamentar |
|
|
from sapl.parlamentares.models import Parlamentar |
|
|
from sapl.protocoloadm.models import DocumentoAdministrativo,\ |
|
|
from sapl.protocoloadm.models import DocumentoAdministrativo,\ |
|
|
DocumentoAcessorioAdministrativo, TramitacaoAdministrativo, Anexado |
|
|
DocumentoAcessorioAdministrativo, TramitacaoAdministrativo, Anexado |
|
|
from sapl.sessao.models import SessaoPlenaria, ExpedienteSessao |
|
|
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 |
|
|
from sapl.parlamentares.models import Mandato, Parlamentar, Legislatura |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=settings.AUTH_USER_MODEL) |
|
|
@receiver(post_save, sender=settings.AUTH_USER_MODEL) |
|
@ -109,31 +108,51 @@ class SaplApiViewSetConstrutor(): |
|
|
object_name = _model._meta.object_name |
|
|
object_name = _model._meta.object_name |
|
|
|
|
|
|
|
|
# Caso Exista, pega a classe sapl.api.serializers.{model}Serializer |
|
|
# Caso Exista, pega a classe sapl.api.serializers.{model}Serializer |
|
|
serializer_name = '{model}Serializer'.format(model=object_name) |
|
|
# ou utiliza a base do drf para gerar uma automática para o model |
|
|
_serializer_class = serializers_classes.get(serializer_name, None) |
|
|
serializer_name = f'{object_name}Serializer' |
|
|
|
|
|
_serializer_class = serializers_classes.get( |
|
|
|
|
|
serializer_name, rest_serializers.ModelSerializer) |
|
|
|
|
|
|
|
|
# Caso Exista, pega a classe sapl.api.forms.{model}FilterSet |
|
|
# Caso Exista, pega a classe sapl.api.forms.{model}FilterSet |
|
|
filter_name = '{model}FilterSet'.format(model=object_name) |
|
|
# ou utiliza a base definida em sapl.forms.SaplFilterSetMixin |
|
|
_filter_class = filters_classes.get(filter_name, None) |
|
|
filter_name = f'{object_name}FilterSet' |
|
|
|
|
|
_filter_class = filters_classes.get( |
|
|
|
|
|
filter_name, SaplFilterSetMixin) |
|
|
|
|
|
|
|
|
def create_class(): |
|
|
def create_class(): |
|
|
|
|
|
|
|
|
|
|
|
_meta_serializer = object if not hasattr( |
|
|
|
|
|
_serializer_class, 'Meta') else _serializer_class.Meta |
|
|
|
|
|
|
|
|
# Define uma classe padrão para serializer caso não tenha sido |
|
|
# Define uma classe padrão para serializer caso não tenha sido |
|
|
# criada a classe sapl.api.serializers.{model}Serializer |
|
|
# criada a classe sapl.api.serializers.{model}Serializer |
|
|
class SaplSerializer(rest_serializers.ModelSerializer): |
|
|
class SaplSerializer(_serializer_class): |
|
|
__str__ = SerializerMethodField() |
|
|
__str__ = SerializerMethodField() |
|
|
|
|
|
|
|
|
class Meta: |
|
|
class Meta(_meta_serializer): |
|
|
model = _model |
|
|
if not hasattr(_meta_serializer, 'model'): |
|
|
fields = '__all__' |
|
|
model = _model |
|
|
|
|
|
|
|
|
|
|
|
if not hasattr(_meta_serializer, 'fields'): |
|
|
|
|
|
fields = '__all__' |
|
|
|
|
|
elif _meta_serializer.fields != '__all__': |
|
|
|
|
|
fields = list( |
|
|
|
|
|
_meta_serializer.fields) + ['__str__', ] |
|
|
|
|
|
else: |
|
|
|
|
|
fields = _meta_serializer.fields |
|
|
|
|
|
|
|
|
def get___str__(self, obj): |
|
|
def get___str__(self, obj): |
|
|
return str(obj) |
|
|
return str(obj) |
|
|
|
|
|
|
|
|
|
|
|
_meta_filterset = object if not hasattr( |
|
|
|
|
|
_filter_class, 'Meta') else _filter_class.Meta |
|
|
|
|
|
|
|
|
# Define uma classe padrão para filtro caso não tenha sido |
|
|
# Define uma classe padrão para filtro caso não tenha sido |
|
|
# criada a classe sapl.api.forms.{model}FilterSet |
|
|
# criada a classe sapl.api.forms.{model}FilterSet |
|
|
class SaplFilterSet(SaplFilterSetMixin): |
|
|
class SaplFilterSet(_filter_class): |
|
|
class Meta(SaplFilterSetMixin.Meta): |
|
|
class Meta(_meta_filterset): |
|
|
model = _model |
|
|
if not hasattr(_meta_filterset, 'model'): |
|
|
|
|
|
model = _model |
|
|
|
|
|
|
|
|
# Define uma classe padrão ModelViewSet de DRF |
|
|
# Define uma classe padrão ModelViewSet de DRF |
|
|
class ModelSaplViewSet(SaplApiViewSet): |
|
|
class ModelSaplViewSet(SaplApiViewSet): |
|
@ -142,14 +161,12 @@ class SaplApiViewSetConstrutor(): |
|
|
# Utiliza o filtro customizado pela classe |
|
|
# Utiliza o filtro customizado pela classe |
|
|
# sapl.api.forms.{model}FilterSet |
|
|
# sapl.api.forms.{model}FilterSet |
|
|
# ou utiliza o trivial SaplFilterSet definido acima |
|
|
# ou utiliza o trivial SaplFilterSet definido acima |
|
|
filter_class = _filter_class \ |
|
|
filter_class = SaplFilterSet |
|
|
if _filter_class else SaplFilterSet |
|
|
|
|
|
|
|
|
|
|
|
# Utiliza o serializer customizado pela classe |
|
|
# Utiliza o serializer customizado pela classe |
|
|
# sapl.api.serializers.{model}Serializer |
|
|
# sapl.api.serializers.{model}Serializer |
|
|
# ou utiliza o trivial SaplSerializer definido acima |
|
|
# ou utiliza o trivial SaplSerializer definido acima |
|
|
serializer_class = _serializer_class \ |
|
|
serializer_class = SaplSerializer |
|
|
if _serializer_class else SaplSerializer |
|
|
|
|
|
|
|
|
|
|
|
return ModelSaplViewSet |
|
|
return ModelSaplViewSet |
|
|
|
|
|
|
|
@ -370,7 +387,7 @@ class _ParlamentarViewSet: |
|
|
return Response(serializer.data) |
|
|
return Response(serializer.data) |
|
|
|
|
|
|
|
|
@action(detail=True) |
|
|
@action(detail=True) |
|
|
def parlamentares_by_legislatura(self,request,*args,**kwargs): |
|
|
def parlamentares_by_legislatura(self, request, *args, **kwargs): |
|
|
""" |
|
|
""" |
|
|
Pega lista de parlamentares pelo id da legislatura. |
|
|
Pega lista de parlamentares pelo id da legislatura. |
|
|
""" |
|
|
""" |
|
@ -386,18 +403,22 @@ class _ParlamentarViewSet: |
|
|
'data_fim_mandato__lte': legislatura.data_fim, |
|
|
'data_fim_mandato__lte': legislatura.data_fim, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
mandatos = Mandato.objects.filter(**filter_params).order_by('-data_inicio_mandato') |
|
|
mandatos = Mandato.objects.filter( |
|
|
parlamentares = Parlamentar.objects.filter(mandato__in=mandatos).distinct() |
|
|
**filter_params).order_by('-data_inicio_mandato') |
|
|
|
|
|
parlamentares = Parlamentar.objects.filter( |
|
|
|
|
|
mandato__in=mandatos).distinct() |
|
|
serializer_class = ParlamentarResumeSerializer(parlamentares, many=True, context={ |
|
|
serializer_class = ParlamentarResumeSerializer(parlamentares, many=True, context={ |
|
|
'request': request, 'legislatura': kwargs['pk'] |
|
|
'request': request, 'legislatura': kwargs['pk'] |
|
|
}) |
|
|
}) |
|
|
return Response(serializer_class.data) |
|
|
return Response(serializer_class.data) |
|
|
|
|
|
|
|
|
@action(detail=False,methods=['GET']) |
|
|
@action(detail=False, methods=['GET']) |
|
|
def search_parlamentares(self,request,*args,**kwargs): |
|
|
def search_parlamentares(self, request, *args, **kwargs): |
|
|
nome = request.query_params.get('nome_parlamentar','') |
|
|
nome = request.query_params.get('nome_parlamentar', '') |
|
|
parlamentares = Parlamentar.objects.filter(nome_parlamentar__icontains=nome) |
|
|
parlamentares = Parlamentar.objects.filter( |
|
|
serializer_class= ParlamentarResumeSerializer(parlamentares,many=True,context={'request':request}) |
|
|
nome_parlamentar__icontains=nome) |
|
|
|
|
|
serializer_class = ParlamentarResumeSerializer( |
|
|
|
|
|
parlamentares, many=True, context={'request': request}) |
|
|
return Response(serializer_class.data) |
|
|
return Response(serializer_class.data) |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -467,7 +488,8 @@ class _MateriaLegislativaViewSet: |
|
|
if not materia.tramitacao_set.exists(): |
|
|
if not materia.tramitacao_set.exists(): |
|
|
return Response({}) |
|
|
return Response({}) |
|
|
|
|
|
|
|
|
ultima_tramitacao = materia.tramitacao_set.order_by('-data_tramitacao', '-id').first() |
|
|
ultima_tramitacao = materia.tramitacao_set.order_by( |
|
|
|
|
|
'-data_tramitacao', '-id').first() |
|
|
|
|
|
|
|
|
serializer_class = SaplApiViewSetConstrutor.get_class_for_model( |
|
|
serializer_class = SaplApiViewSetConstrutor.get_class_for_model( |
|
|
Tramitacao).serializer_class(ultima_tramitacao) |
|
|
Tramitacao).serializer_class(ultima_tramitacao) |
|
@ -622,4 +644,3 @@ class AppVersionView(APIView): |
|
|
'is_authenticated': request.user.is_authenticated, |
|
|
'is_authenticated': request.user.is_authenticated, |
|
|
} |
|
|
} |
|
|
return Response(content) |
|
|
return Response(content) |
|
|
|
|
|
|
|
|