Source code for apis_core.apis_entities.api_views
from django.db.models import Q
from django.shortcuts import redirect
from rest_framework.exceptions import NotFound
from rest_framework.generics import ListAPIView
from rest_framework.views import APIView
from apis_core.apis_entities.serializers import MinimalEntitySerializer
from apis_core.apis_entities.utils import get_entity_classes
from apis_core.apis_metainfo.models import RootObject
from apis_core.utils.filters import CustomSearchFilter
[docs]
class GetEntityGeneric(APIView):
[docs]
def get(self, request, pk):
try:
obj = RootObject.objects_inheritance.get_subclass(id=pk)
return redirect(obj.get_api_detail_endpoint())
except RootObject.DoesNotExist:
raise NotFound
[docs]
class ListEntityGeneric(ListAPIView):
serializer_class = MinimalEntitySerializer
filter_backends = [CustomSearchFilter]
[docs]
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)