Browse Source

ajustes automáticos da IDE no app api

pull/3453/head
Leandro Roberto 3 years ago
parent
commit
ca9ecf00c0
  1. 5
      sapl/api/core/__init__.py
  2. 2
      sapl/api/core/filters.py
  3. 3
      sapl/api/deprecated.py
  4. 10
      sapl/api/forms.py
  5. 10
      sapl/api/serializers.py
  6. 30
      sapl/api/urls.py
  7. 9
      sapl/api/views_customize.py

5
sapl/api/core/__init__.py

@ -27,6 +27,7 @@ from sapl.api.serializers import ChoiceSerializer, ParlamentarSerializer,\
class BusinessRulesNotImplementedMixin: class BusinessRulesNotImplementedMixin:
def create(self, request, *args, **kwargs): def create(self, request, *args, **kwargs):
raise Exception(_("POST Create não implementado")) raise Exception(_("POST Create não implementado"))
@ -126,6 +127,7 @@ class SaplApiViewSetConstrutor():
# 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(_filterset_class): class SaplFilterSet(_filterset_class):
class Meta(_meta_filterset): class Meta(_meta_filterset):
if not hasattr(_meta_filterset, 'model'): if not hasattr(_meta_filterset, 'model'):
model = _model model = _model
@ -159,7 +161,6 @@ class SaplApiViewSetConstrutor():
return cls return cls
""" """
1. Constroi uma rest_framework.viewsets.ModelViewSet para 1. Constroi uma rest_framework.viewsets.ModelViewSet para
todos os models de todas as apps do sapl todos os models de todas as apps do sapl
@ -232,6 +233,7 @@ class SaplApiViewSetConstrutor():
class wrapper_queryset_response_for_drf_action(object): class wrapper_queryset_response_for_drf_action(object):
def __init__(self, model): def __init__(self, model):
self.model = model self.model = model
@ -268,6 +270,7 @@ class wrapper_queryset_response_for_drf_action(object):
# decorator para recuperar e transformar o default # decorator para recuperar e transformar o default
class customize(object): class customize(object):
def __init__(self, model): def __init__(self, model):
self.model = model self.model = model

2
sapl/api/core/filters.py

@ -3,11 +3,11 @@ from collections import OrderedDict
from django.db.models.fields.files import FileField from django.db.models.fields.files import FileField
from django.template.defaultfilters import capfirst from django.template.defaultfilters import capfirst
import django_filters
from django_filters.constants import ALL_FIELDS from django_filters.constants import ALL_FIELDS
from django_filters.filters import CharFilter from django_filters.filters import CharFilter
from django_filters.filterset import FilterSet from django_filters.filterset import FilterSet
from django_filters.utils import resolve_field, get_all_model_fields from django_filters.utils import resolve_field, get_all_model_fields
import django_filters
class SaplFilterSetMixin(FilterSet): class SaplFilterSetMixin(FilterSet):

3
sapl/api/deprecated.py

@ -1,5 +1,4 @@
import logging
import logging import logging
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
@ -10,12 +9,10 @@ from django.forms.widgets import MultiWidget, TextInput
from django.http import Http404 from django.http import Http404
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ugettext_lazy as _
from django_filters.filters import CharFilter, ModelChoiceFilter, DateFilter from django_filters.filters import CharFilter, ModelChoiceFilter, DateFilter
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 rest_framework import serializers from rest_framework import serializers
from rest_framework import serializers
from rest_framework.generics import ListAPIView from rest_framework.generics import ListAPIView
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
from rest_framework.permissions import (IsAuthenticated, from rest_framework.permissions import (IsAuthenticated,

10
sapl/api/forms.py

@ -2,7 +2,17 @@
from sapl.api.core.filters import SaplFilterSetMixin from sapl.api.core.filters import SaplFilterSetMixin
from sapl.sessao.models import SessaoPlenaria from sapl.sessao.models import SessaoPlenaria
# esta classe não é necessária
# a api construiría uma igual
# mas está demonstrar que caso queira customizar um filter_set
# que a api consiga recuperá-lo, para os endpoints básicos
# deve seguir os critérios de nomenclatura e herança
# class [Model]FilterSet(SaplFilterSetMixin):
# class Meta(SaplFilterSetMixin.Meta):
class SessaoPlenariaFilterSet(SaplFilterSetMixin): class SessaoPlenariaFilterSet(SaplFilterSetMixin):
class Meta(SaplFilterSetMixin.Meta): class Meta(SaplFilterSetMixin.Meta):
model = SessaoPlenaria model = SessaoPlenaria

10
sapl/api/serializers.py

@ -1,16 +1,18 @@
import logging import logging
from django.conf import settings from django.conf import settings
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from django.db.models import F, Q from django.db.models import Q
from image_cropping.utils import get_backend
from rest_framework import serializers from rest_framework import serializers
from rest_framework.relations import StringRelatedField from rest_framework.relations import StringRelatedField
from sapl.parlamentares.models import Parlamentar, Mandato, Filiacao, Legislatura
from sapl.base.models import Autor, CasaLegislativa from sapl.base.models import Autor, CasaLegislativa
from sapl.utils import filiacao_data from sapl.parlamentares.models import Parlamentar, Mandato, Legislatura
from image_cropping.utils import get_backend
class IntRelatedField(StringRelatedField): class IntRelatedField(StringRelatedField):
def to_representation(self, value): def to_representation(self, value):
return int(value) return int(value)

30
sapl/api/urls.py

@ -12,21 +12,17 @@ from sapl.api.views_customize import SaplApiViewSetConstrutor
from .apps import AppConfig from .apps import AppConfig
app_name = AppConfig.name app_name = AppConfig.name
router = DefaultRouter() router = DefaultRouter()
router.register(r'materia$', MateriaLegislativaViewSet) router.register(r'materia$', MateriaLegislativaViewSet)
router.register(r'sessao-plenaria', SessaoPlenariaViewSet) router.register(r'sessao-plenaria', SessaoPlenariaViewSet)
for app, built_sets in SaplApiViewSetConstrutor._built_sets.items(): for app, built_sets in SaplApiViewSetConstrutor._built_sets.items():
for view_prefix, viewset in built_sets.items(): for view_prefix, viewset in built_sets.items():
router.register(app.label + '/' + router.register(app.label + '/' +
view_prefix._meta.model_name, viewset) view_prefix._meta.model_name, viewset)
urlpatterns_router = router.urls urlpatterns_router = router.urls
urlpatterns_api_doc = [ urlpatterns_api_doc = [
@ -38,28 +34,6 @@ urlpatterns_api_doc = [
# YOUR PATTERNS # YOUR PATTERNS
url('^schema/', SpectacularAPIView.as_view(), name='schema_api'), url('^schema/', SpectacularAPIView.as_view(), name='schema_api'),
] ]
"""if 'drf_yasg' in settings.INSTALLED_APPS:
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
schema_view = get_schema_view(
openapi.Info(
title="Sapl API - docs",
default_version='v1',
description="Sapl API - Docs - Configuração Básica",
),
url=settings.SITE_URL,
public=True,
permission_classes=(permissions.AllowAny,),
)
urlpatterns_api_doc = [
url(r'^docs/swagger(?P<format>\.json|\.yaml)$',
schema_view.without_ui(cache_timeout=0), name='schema-json'),
url(r'^docs/swagger/$',
schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
url(r'^docs/redoc/$',
schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
]"""
# TODO: refatorar para customização da api automática # TODO: refatorar para customização da api automática
deprecated_urlpatterns_api = [ deprecated_urlpatterns_api = [
@ -67,13 +41,9 @@ deprecated_urlpatterns_api = [
AutoresProvaveisListView.as_view(), name='autores_provaveis_list'), AutoresProvaveisListView.as_view(), name='autores_provaveis_list'),
url(r'^autor/possiveis', url(r'^autor/possiveis',
AutoresPossiveisListView.as_view(), name='autores_possiveis_list'), AutoresPossiveisListView.as_view(), name='autores_possiveis_list'),
url(r'^autor', AutorListView.as_view(), name='autor_list'), url(r'^autor', AutorListView.as_view(), name='autor_list'),
url(r'^model/(?P<content_type>\d+)/(?P<pk>\d*)$', url(r'^model/(?P<content_type>\d+)/(?P<pk>\d*)$',
ModelChoiceView.as_view(), name='model_list'), ModelChoiceView.as_view(), name='model_list'),
] ]
urlpatterns = [ urlpatterns = [

9
sapl/api/views_customize.py

@ -25,7 +25,6 @@ from sapl.protocoloadm.models import DocumentoAdministrativo,\
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
SaplApiViewSetConstrutor = SaplApiViewSetConstrutor.build_class() SaplApiViewSetConstrutor = SaplApiViewSetConstrutor.build_class()
@ -33,7 +32,7 @@ SaplApiViewSetConstrutor = SaplApiViewSetConstrutor.build_class()
class _AutorViewSet: class _AutorViewSet:
# Customização para AutorViewSet com implementação de actions específicas # Customização para AutorViewSet com implementação de actions específicas
""" """
Neste exemplo de customização do que foi criado em Nesta customização do que foi criado em
SaplApiViewSetConstrutor além do ofertado por SaplApiViewSetConstrutor além do ofertado por
rest_framework.viewsets.ModelViewSet, dentre outras customizações rest_framework.viewsets.ModelViewSet, dentre outras customizações
possíveis, foi adicionado as rotas referentes aos relacionamentos genéricos possíveis, foi adicionado as rotas referentes aos relacionamentos genéricos
@ -100,7 +99,9 @@ class _AutorViewSet:
@customize(Parlamentar) @customize(Parlamentar)
class _ParlamentarViewSet: class _ParlamentarViewSet:
class ParlamentarPermission(SaplModelPermissions): class ParlamentarPermission(SaplModelPermissions):
def has_permission(self, request, view): def has_permission(self, request, view):
if request.method == 'GET': if request.method == 'GET':
return True return True
@ -219,7 +220,9 @@ class _ProposicaoViewSet:
* Pode recuperar qualquer das proposições incorporadas * Pode recuperar qualquer das proposições incorporadas
""" """
class ProposicaoPermission(SaplModelPermissions): class ProposicaoPermission(SaplModelPermissions):
def has_permission(self, request, view): def has_permission(self, request, view):
if request.method == 'GET': if request.method == 'GET':
return True return True
@ -259,6 +262,7 @@ class _ProposicaoViewSet:
@customize(MateriaLegislativa) @customize(MateriaLegislativa)
class _MateriaLegislativaViewSet: class _MateriaLegislativaViewSet:
class Meta: class Meta:
ordering = ['-ano', 'tipo', 'numero'] ordering = ['-ano', 'tipo', 'numero']
@ -305,6 +309,7 @@ class _TipoMateriaLegislativaViewSet:
class _DocumentoAdministrativoViewSet: class _DocumentoAdministrativoViewSet:
class DocumentoAdministrativoPermission(SaplModelPermissions): class DocumentoAdministrativoPermission(SaplModelPermissions):
def has_permission(self, request, view): def has_permission(self, request, view):
if request.method == 'GET': if request.method == 'GET':
comportamento = AppConfig.attr('documentos_administrativos') comportamento = AppConfig.attr('documentos_administrativos')

Loading…
Cancel
Save