filter all apis of organization city & province - cant remove own user or organoization
This commit is contained in:
@@ -1,8 +1,36 @@
|
||||
from apps.core.serializers import MobileTestSerializer, SystemConfigSerializer
|
||||
from apps.core.models import MobileTest, SystemConfig
|
||||
from rest_framework.response import Response
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user