From ec287d65c4cd2e93bda4559aa14421bc75341e8a Mon Sep 17 00:00:00 2001 From: Mahmoud Emad Date: Thu, 8 Feb 2024 18:57:23 +0200 Subject: [PATCH] Enhancements: Implement an endpoint to get all supervisors users on an exact office. --- server/cshr/routes/office.py | 3 ++- server/cshr/services/office.py | 8 ++++++++ server/cshr/views/company_properties.py | 2 -- server/cshr/views/office.py | 20 +++++++++++++++++++- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/server/cshr/routes/office.py b/server/cshr/routes/office.py index 510b3eedc..0da8aec68 100644 --- a/server/cshr/routes/office.py +++ b/server/cshr/routes/office.py @@ -1,9 +1,10 @@ from django.urls import path -from cshr.views.office import OfficeApiView, BaseOfficeApiView +from cshr.views.office import OfficeApiView, BaseOfficeApiView, OfficeSupervisorsApiView urlpatterns = [ path("", BaseOfficeApiView.as_view()), path("/", OfficeApiView.as_view()), + path("/supervisors/", OfficeSupervisorsApiView.as_view()), ] diff --git a/server/cshr/services/office.py b/server/cshr/services/office.py index a9eac8d38..90781cba5 100644 --- a/server/cshr/services/office.py +++ b/server/cshr/services/office.py @@ -2,9 +2,14 @@ methods that will serve office endpoints """ from cshr.models.office import Office +from django.db.models.query import QuerySet + +from cshr.models.users import User, USER_TYPE def get_office_by_id(id: str) -> Office: + if not id.isdigit(): + return None try: return Office.objects.get(id=int(id)) except Office.DoesNotExist: @@ -16,3 +21,6 @@ def get_office_by_name(name: str) -> Office: return Office.objects.get(name=name) except Office.DoesNotExist: return None + +def get_office_supervisors(office: Office) -> QuerySet[User]: + return User.objects.filter(location__id=office.id, user_type=USER_TYPE.SUPERVISOR) \ No newline at end of file diff --git a/server/cshr/views/company_properties.py b/server/cshr/views/company_properties.py index a208cf1b3..dde16bb1e 100644 --- a/server/cshr/views/company_properties.py +++ b/server/cshr/views/company_properties.py @@ -23,8 +23,6 @@ def get_queryset(self) -> Response: return query_set - """method to create a new Company properties""" - def post(self, request: Request) -> Response: """Method to create a new Company properties""" serializer = self.get_serializer(data=request.data) diff --git a/server/cshr/views/office.py b/server/cshr/views/office.py index 975ff47e0..b35fdc569 100644 --- a/server/cshr/views/office.py +++ b/server/cshr/views/office.py @@ -2,6 +2,7 @@ from rest_framework.request import Request from rest_framework.response import Response from cshr.models.office import Office +from cshr.serializers.users import GeneralUserSerializer from ..serializers.office import OfficeSerializer from ..api.response import CustomResponse from cshr.api.permission import ( @@ -11,7 +12,7 @@ UserIsAuthenticated, CustomPermissions, ) -from cshr.services.office import get_office_by_id +from cshr.services.office import get_office_by_id, get_office_supervisors class BaseOfficeApiView(ListAPIView, GenericAPIView): @@ -82,3 +83,20 @@ def put(self, request: Request, id: str, format=None) -> Response: error=serializer.errors, message="Office failed to update" ) return CustomResponse.not_found(message="Office not found to update") + + +class OfficeSupervisorsApiView(ListAPIView, GenericAPIView): + """method to get all Company properties""" + + serializer_class = GeneralUserSerializer + permission_class = [IsAdmin | IsSupervisor] + + def get_queryset(self) -> Response: + office_id = self.kwargs.get("id") + office = get_office_by_id(office_id) + + if office is None: + return [] + + query_set = get_office_supervisors(office) + return query_set