Skip to content

Commit

Permalink
feat: optimize query on sector, queue and agents external endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
helllllllder committed Oct 21, 2024
1 parent 6b405a6 commit 30c2249
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 28 deletions.
4 changes: 2 additions & 2 deletions chats/apps/api/v1/external/agents/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Meta:
)

def filter_queue(self, queryset, name, value):
return queryset.filter(queue_authorizations__queue__uuid=value)
return queryset.filter(queue_authorizations__queue=value)

def filter_sector(self, queryset, name, value):
return queryset.filter(sector_authorizations__sector__uuid=value)
return queryset.filter(sector_authorizations__sector=value)
16 changes: 3 additions & 13 deletions chats/apps/api/v1/external/agents/viewsets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated

from chats.apps.accounts.authentication.drf.authorization import (
ProjectAdminAuthentication,
Expand All @@ -10,27 +9,18 @@
from chats.apps.projects.models import ProjectPermission


def get_permission_token_from_request(request):
auth_header = request.META.get("HTTP_AUTHORIZATION")
return auth_header.split()[1]


class AgentFlowViewset(viewsets.ReadOnlyModelViewSet):
model = ProjectPermission
queryset = ProjectPermission.objects.all()
serializer_class = AgentFlowSerializer
filter_backends = [DjangoFilterBackend]
filterset_class = AgentFlowFilter
permission_classes = [
IsAuthenticated,
]
lookup_field = "uuid"
authentication_classes = [ProjectAdminAuthentication]

def get_queryset(self):
# permission = get_permission_token_from_request(self.request)
permission = self.request.auth
qs = super().get_queryset()
return qs.filter(
project__permissions=permission.pk, project__permissions__role=1
)
if permission is None or permission.role != 1:
return qs.none()
return qs.filter(project=permission.project)
3 changes: 1 addition & 2 deletions chats/apps/api/v1/external/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def has_permission(self, request, view): # pragma: no cover
return validation.is_valid
except (AttributeError, IndexError, ProjectPermission.DoesNotExist):
return False

return super().has_permission(request, view)

def has_object_permission(self, request, view, obj):
Expand All @@ -30,7 +29,7 @@ def has_object_permission(self, request, view, obj):
project = obj.project
except ProjectPermission.DoesNotExist:
return False
return permission.project == project
return permission.project == str(project.uuid)


LEVEL_NAME_MAPPING = {
Expand Down
10 changes: 4 additions & 6 deletions chats/apps/api/v1/external/queues/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from chats.apps.accounts.authentication.drf.authorization import (
ProjectAdminAuthentication,
)
from chats.apps.api.v1.external.permissions import IsAdminPermission
from chats.apps.api.v1.external.queues.filters import QueueFlowFilter
from chats.apps.api.v1.external.queues.serializers import QueueFlowSerializer
from chats.apps.queues.models import Queue
Expand All @@ -16,14 +15,13 @@ class QueueFlowViewset(viewsets.ReadOnlyModelViewSet):
serializer_class = QueueFlowSerializer
filter_backends = [DjangoFilterBackend]
filterset_class = QueueFlowFilter
permission_classes = [
IsAdminPermission,
]

lookup_field = "uuid"
authentication_classes = [ProjectAdminAuthentication]

def get_queryset(self):
permission = self.request.auth
qs = super().get_queryset()

return qs.filter(sector__project__permissions__uuid=permission.pk)
if permission is None or permission.role != 1:
return qs.none()
return qs.filter(sector__project=permission.project)
8 changes: 3 additions & 5 deletions chats/apps/api/v1/external/sectors/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from chats.apps.accounts.authentication.drf.authorization import (
ProjectAdminAuthentication,
)
from chats.apps.api.v1.external.permissions import IsAdminPermission
from chats.apps.api.v1.external.sectors.serializers import SectorFlowSerializer
from chats.apps.sectors.models import Sector

Expand All @@ -17,13 +16,12 @@ class SectorFlowViewset(viewsets.ReadOnlyModelViewSet):
filterset_fields = [
"name",
]
permission_classes = [
IsAdminPermission,
]
lookup_field = "uuid"
authentication_classes = [ProjectAdminAuthentication]

def get_queryset(self):
permission = self.request.auth
qs = super().get_queryset()
return qs.filter(project__permissions__uuid=permission.pk)
if permission is None or permission.role != 1:
return qs.none()
return qs.filter(project=permission.project)

0 comments on commit 30c2249

Please sign in to comment.