Coverage for apis_core/apis_entities/api_views.py: 58%

26 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-22 07:51 +0000

1from django.db.models import Q 

2from django.shortcuts import redirect 

3from rest_framework.exceptions import NotFound 

4from rest_framework.generics import ListAPIView 

5from rest_framework.views import APIView 

6 

7from apis_core.apis_entities.serializers import MinimalEntitySerializer 

8from apis_core.apis_entities.utils import get_entity_classes 

9from apis_core.apis_metainfo.models import RootObject 

10from apis_core.utils.filters import CustomSearchFilter 

11 

12 

13class GetEntityGeneric(APIView): 

14 def get(self, request, pk): 

15 try: 

16 obj = RootObject.objects_inheritance.get_subclass(id=pk) 

17 return redirect(obj.get_api_detail_endpoint()) 

18 except RootObject.DoesNotExist: 

19 raise NotFound 

20 

21 

22class ListEntityGeneric(ListAPIView): 

23 serializer_class = MinimalEntitySerializer 

24 filter_backends = [CustomSearchFilter] 

25 

26 def get_queryset(self): 

27 entities = get_entity_classes() 

28 entities = [entity._meta.model_name for entity in entities] 

29 q = Q() 

30 for entity in entities: 

31 q |= Q(**{f"{entity}__isnull": False}) 

32 return RootObject.objects_inheritance.select_subclasses().filter(q)