Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(apis_entities): add endpoint that lists all entities #851

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
12 changes: 12 additions & 0 deletions apis_core/apis_entities/serializers.py
Original file line number Diff line number Diff line change
@@ -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)
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
Loading