Skip to content

Commit

Permalink
Merge pull request #358 from codescalers/development_1.1_office_suppe…
Browse files Browse the repository at this point in the history
…rvisor_endpoint

Enhancements: Implement an endpoint to get all supervisors users on an exact office.
  • Loading branch information
xmonader authored Feb 8, 2024
2 parents f770a3f + ec287d6 commit 4b5c0e7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
3 changes: 2 additions & 1 deletion server/cshr/routes/office.py
Original file line number Diff line number Diff line change
@@ -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("<str:id>/", OfficeApiView.as_view()),
path("<str:id>/supervisors/", OfficeSupervisorsApiView.as_view()),
]
8 changes: 8 additions & 0 deletions server/cshr/services/office.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
2 changes: 0 additions & 2 deletions server/cshr/views/company_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 19 additions & 1 deletion server/cshr/views/office.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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):
Expand Down Expand Up @@ -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

0 comments on commit 4b5c0e7

Please sign in to comment.