diff --git a/drfautoapi/drfautoapi.py b/drfautoapi/drfautoapi.py index ff9153da7..03c8059d1 100644 --- a/drfautoapi/drfautoapi.py +++ b/drfautoapi/drfautoapi.py @@ -16,8 +16,10 @@ from django_filters.rest_framework.backends import DjangoFilterBackend from django_filters.utils import resolve_field, get_all_model_fields from rest_framework import serializers as rest_serializers from rest_framework.response import Response +from rest_framework.routers import DefaultRouter from rest_framework.viewsets import ModelViewSet + logger = logging.getLogger(__name__) @@ -156,10 +158,15 @@ class ApiViewSetConstrutor(): def import_modules(cls, modules): for m in modules: importlib.import_module(m) - #components = inspect.getmembers(components_ref) - # for key, vclass in components: - # if type(vclass) == cls: - # cls.update(vclass) + + @classmethod + def router(cls, router_class=DefaultRouter): + router = router_class() + for app, built_sets in cls._built_sets.items(): + for model, viewset in built_sets.items(): + router.register( + f'{app.label}/{model._meta.model_name}', viewset) + return router @classmethod def build_class(cls, apps): diff --git a/sapl/api/serializers.py b/sapl/api/serializers.py index 82fbdbe3f..1a779770e 100644 --- a/sapl/api/serializers.py +++ b/sapl/api/serializers.py @@ -7,7 +7,6 @@ from django.db.models import Q from image_cropping.utils import get_backend from rest_framework import serializers from rest_framework.fields import SerializerMethodField -from rest_framework.relations import StringRelatedField from sapl.base.models import Autor, CasaLegislativa, Metadata from sapl.parlamentares.models import Parlamentar, Mandato, Legislatura diff --git a/sapl/api/urls.py b/sapl/api/urls.py index f3a2380b6..50d7241a7 100644 --- a/sapl/api/urls.py +++ b/sapl/api/urls.py @@ -15,14 +15,11 @@ from .apps import AppConfig app_name = AppConfig.name -router = DefaultRouter() +router = SaplApiViewSetConstrutor.router() + router.register(r'materia$', MateriaLegislativaViewSet) router.register(r'sessao-plenaria', SessaoPlenariaViewSet) -for app, built_sets in SaplApiViewSetConstrutor._built_sets.items(): - for view_prefix, viewset in built_sets.items(): - router.register(f'{app.label}/{view_prefix._meta.model_name}', viewset) - urlpatterns_router = router.urls urlpatterns_api_doc = [