Files
RasadDam_Backend/apps/core/api.py

43 lines
1.4 KiB
Python
Raw Normal View History

from rest_framework import viewsets
from apps.authentication.mixins.region_filter import RegionFilterMixin
from apps.core.models import MobileTest, SystemConfig
from apps.core.serializers import MobileTestSerializer, SystemConfigSerializer
class BaseViewSet(RegionFilterMixin, viewsets.ModelViewSet):
"""
All view sets in the project should inherit from this class.
It applies region-based filtering automatically to GET (list) requests.
"""
def get_queryset(self):
queryset = super().get_queryset()
request = self.request
user = request.user
user_relation = user.user_relation.all()
if self.request.method.lower() == 'get' and not self.kwargs.get('pk'):
queryset = self.filter_by_region(queryset, org=True)
if not user_relation.first().role.type.key == 'ADM':
model_name = queryset.model.__name__.lower()
if model_name == 'user':
queryset = queryset.exclude(id=user.id)
elif model_name == 'organization':
queryset = queryset.exclude(id=user_relation.first().organization.id)
return queryset
class MobileTestViewSet(viewsets.ModelViewSet):
queryset = MobileTest.objects.all()
serializer_class = MobileTestSerializer
class SystemConfigViewSet(viewsets.ModelViewSet):
queryset = SystemConfig.objects.all()
serializer_class = SystemConfigSerializer