diff --git a/apis_core/apis_entities/api_views.py b/apis_core/apis_entities/api_views.py index 45d65ca2a..ca57bb44a 100644 --- a/apis_core/apis_entities/api_views.py +++ b/apis_core/apis_entities/api_views.py @@ -1,8 +1,12 @@ from django.shortcuts import redirect +from django.db.models import Q from rest_framework.views import APIView from rest_framework.exceptions import NotFound +from rest_framework.generics import ListAPIView from apis_core.apis_metainfo.models import RootObject +from apis_core.apis_entities.serializers import MinimalEntitySerializer +from apis_core.apis_entities.utils import get_entity_classes class GetEntityGeneric(APIView): @@ -12,3 +16,15 @@ def get(self, request, pk): return redirect(obj.get_api_detail_endpoint()) except RootObject.DoesNotExist: raise NotFound + + +class ListEntityGeneric(ListAPIView): + serializer_class = MinimalEntitySerializer + + def get_queryset(self): + entities = get_entity_classes() + entities = [entity._meta.model_name for entity in entities] + q = Q() + for entity in entities: + q |= Q(**{f"{entity}__isnull": False}) + return RootObject.objects_inheritance.select_subclasses().filter(q) diff --git a/apis_core/apis_entities/serializers.py b/apis_core/apis_entities/serializers.py new file mode 100644 index 000000000..ff72a9629 --- /dev/null +++ b/apis_core/apis_entities/serializers.py @@ -0,0 +1,12 @@ +from rest_framework import serializers +from apis_core.generic.serializers import GenericHyperlinkedIdentityField + + +class MinimalEntitySerializer(serializers.Serializer): + uri = GenericHyperlinkedIdentityField( + view_name="apis_core:generic:genericmodelapi-detail" + ) + name = serializers.SerializerMethodField(method_name="get_name") + + def get_name(self, object): + return str(object) diff --git a/apis_core/apis_entities/utils.py b/apis_core/apis_entities/utils.py new file mode 100644 index 000000000..40a3e3365 --- /dev/null +++ b/apis_core/apis_entities/utils.py @@ -0,0 +1,6 @@ +from django.apps import apps +from apis_core.apis_entities.models import AbstractEntity + + +def get_entity_classes(): + return list(filter(lambda x: issubclass(x, AbstractEntity), apps.get_models())) diff --git a/apis_core/urls.py b/apis_core/urls.py index 10d381faa..6fdef8806 100644 --- a/apis_core/urls.py +++ b/apis_core/urls.py @@ -10,7 +10,7 @@ from apis_core.apis_metainfo.viewsets import UriToObjectViewSet from apis_core.core.views import Dumpdata -from apis_core.apis_entities.api_views import GetEntityGeneric +from apis_core.apis_entities.api_views import GetEntityGeneric, ListEntityGeneric from drf_spectacular.views import ( SpectacularAPIView, @@ -67,6 +67,7 @@ GetEntityGeneric.as_view(), name="GetEntityGeneric", ), + path("entities/", ListEntityGeneric.as_view()), path("api/dumpdata", Dumpdata.as_view()), path("", include("apis_core.generic.urls", namespace="generic")), ]