Skip to content

Commit 3be027e

Browse files
committed
move permissions logic to separate permission class for toggle privacy
1 parent 1440fdb commit 3be027e

File tree

1 file changed

+35
-36
lines changed
  • care/facility/api/viewsets

1 file changed

+35
-36
lines changed

care/facility/api/viewsets/bed.py

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from django.db.models import OuterRef, Subquery
44
from django_filters import rest_framework as filters
55
from drf_spectacular.utils import extend_schema, extend_schema_view
6-
from dry_rest_permissions.generics import DRYPermissions
76
from rest_framework import filters as drf_filters
87
from rest_framework import status
98
from rest_framework.decorators import action
@@ -17,7 +16,7 @@
1716
RetrieveModelMixin,
1817
UpdateModelMixin,
1918
)
20-
from rest_framework.permissions import IsAuthenticated
19+
from rest_framework.permissions import BasePermission, IsAuthenticated
2120
from rest_framework.response import Response
2221
from rest_framework.viewsets import GenericViewSet
2322

@@ -211,6 +210,32 @@ class ConsultationBedFilter(filters.FilterSet):
211210
bed = filters.UUIDFilter(field_name="bed__external_id")
212211

213212

213+
class TogglePatientPrivacyPermission(BasePermission):
214+
def has_permission(self, request, view):
215+
user = request.user
216+
instance = view.get_object()
217+
218+
if (
219+
user.user_type == User.TYPE_VALUE_MAP["WardAdmin"]
220+
or user.user_type == User.TYPE_VALUE_MAP["LocalBodyAdmin"]
221+
or user.user_type == User.TYPE_VALUE_MAP["DistrictAdmin"]
222+
or user.user_type == User.TYPE_VALUE_MAP["StateAdmin"]
223+
or (
224+
user.user_type == User.TYPE_VALUE_MAP["Doctor"]
225+
and user.home_facility.external_id == instance.bed.facility.external_id
226+
)
227+
or (
228+
user.user_type == User.TYPE_VALUE_MAP["Staff"]
229+
and user.home_facility.external_id == instance.bed.facility.external_id
230+
)
231+
):
232+
return True
233+
return False
234+
235+
def has_object_permission(self, request, view, obj) -> bool:
236+
return self.has_permission(request, view)
237+
238+
214239
class ConsultationBedViewSet(
215240
ListModelMixin,
216241
RetrieveModelMixin,
@@ -228,10 +253,7 @@ class ConsultationBedViewSet(
228253
filter_backends = (filters.DjangoFilterBackend,)
229254
filterset_class = ConsultationBedFilter
230255
lookup_field = "external_id"
231-
permission_classes = (
232-
IsAuthenticated,
233-
DRYPermissions,
234-
)
256+
permission_classes = [TogglePatientPrivacyPermission]
235257

236258
def get_queryset(self):
237259
user = self.request.user
@@ -254,34 +276,11 @@ def get_queryset(self):
254276
tags=["consultationbed"],
255277
)
256278
@action(detail=True, methods=["PATCH"])
257-
def toggle_patient_privacy(self, request, external_id):
258-
user: User = request.user
259-
consultation_bed: ConsultationBed = self.get_queryset().get(
260-
external_id=external_id
261-
)
262-
263-
if consultation_bed and (
264-
user.user_type == User.TYPE_VALUE_MAP["WardAdmin"]
265-
or user.user_type == User.TYPE_VALUE_MAP["LocalBodyAdmin"]
266-
or user.user_type == User.TYPE_VALUE_MAP["DistrictAdmin"]
267-
or user.user_type == User.TYPE_VALUE_MAP["StateAdmin"]
268-
or (
269-
user.user_type == User.TYPE_VALUE_MAP["Doctor"]
270-
and user.home_facility.external_id
271-
== consultation_bed.bed.facility.external_id
272-
)
273-
or (
274-
user.user_type == User.TYPE_VALUE_MAP["Staff"]
275-
and user.home_facility.external_id
276-
== consultation_bed.bed.facility.external_id
277-
)
278-
):
279-
consultation_bed.privacy = not consultation_bed.privacy
280-
consultation_bed.save()
281-
return Response(
282-
{"status": "success", "privacy": consultation_bed.privacy},
283-
status=status.HTTP_200_OK,
284-
)
285-
raise PermissionDenied(
286-
detail="You do not have permission to perform this action"
279+
def toggle_patient_privacy(self, request):
280+
instance = self.get_object()
281+
instance.privacy = not instance.privacy
282+
instance.save()
283+
return Response(
284+
{"status": "success", "privacy": instance.privacy},
285+
status=status.HTTP_200_OK,
287286
)

0 commit comments

Comments
 (0)