list of devices by admin filter mixin
This commit is contained in:
14
apps/core/mixins/admin_mixin.py
Normal file
14
apps/core/mixins/admin_mixin.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from rest_framework.viewsets import GenericViewSet
|
||||
from common.helpers import get_organization_by_user
|
||||
|
||||
|
||||
class AdminFilterMixin:
|
||||
admin_param_name = 'admin'
|
||||
|
||||
def get_query(self, queryset):
|
||||
request = self.request # noqa
|
||||
|
||||
if request.query_params.get(self.admin_param_name):
|
||||
return queryset
|
||||
|
||||
return queryset.filter(organization=get_organization_by_user(request.user))
|
||||
@@ -17,7 +17,10 @@ class DeviceSerializer(ModelSerializer):
|
||||
""" custom output of serializer """
|
||||
representation = super().to_representation(instance)
|
||||
|
||||
representation['organization'] = {'name': instance.organization.name}
|
||||
representation['organization'] = {
|
||||
'name': instance.organization.name,
|
||||
'id': instance.organization.id
|
||||
}
|
||||
|
||||
return representation
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ from apps.authentication.api.v1.api import (
|
||||
OrganizationSerializer
|
||||
)
|
||||
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||
from apps.core.mixins.admin_mixin import AdminFilterMixin
|
||||
from apps.authorization.models import UserRelations
|
||||
from common.helpers import get_organization_by_user
|
||||
from rest_framework.exceptions import APIException
|
||||
@@ -57,7 +58,7 @@ class ProviderCompanyViewSet(viewsets.ModelViewSet): # noqa
|
||||
return self.get_paginated_response(serializer.data)
|
||||
|
||||
|
||||
class DeviceViewSet(viewsets.ModelViewSet):
|
||||
class DeviceViewSet(viewsets.ModelViewSet, AdminFilterMixin):
|
||||
queryset = pos_models.Device.objects.all()
|
||||
serializer_class = device_serializer.DeviceSerializer
|
||||
|
||||
@@ -87,9 +88,8 @@ class DeviceViewSet(viewsets.ModelViewSet):
|
||||
def my_devices(self, request):
|
||||
""" list of company devices """
|
||||
|
||||
devices = self.queryset.filter(
|
||||
organization=get_organization_by_user(request.user)
|
||||
)
|
||||
# using admin filter mixin to get query
|
||||
devices = self.get_query(self.queryset)
|
||||
|
||||
# paginate devices
|
||||
page = self.paginate_queryset(devices)
|
||||
|
||||
Reference in New Issue
Block a user