Skip to content

Commit

Permalink
feat(apis_entities): add endpoint that lists all entities
Browse files Browse the repository at this point in the history
Closes: #850
  • Loading branch information
b1rger committed May 16, 2024
1 parent 8da1372 commit 41424b3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
16 changes: 16 additions & 0 deletions apis_core/apis_entities/api_views.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)
10 changes: 10 additions & 0 deletions apis_core/apis_entities/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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)
6 changes: 6 additions & 0 deletions apis_core/apis_entities/utils.py
Original file line number Diff line number Diff line change
@@ -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()))
3 changes: 2 additions & 1 deletion apis_core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")),
]
Expand Down

0 comments on commit 41424b3

Please sign in to comment.