3
3
from django .db .models import OuterRef , Subquery
4
4
from django_filters import rest_framework as filters
5
5
from drf_spectacular .utils import extend_schema , extend_schema_view
6
- from dry_rest_permissions .generics import DRYPermissions
7
6
from rest_framework import filters as drf_filters
8
7
from rest_framework import status
9
8
from rest_framework .decorators import action
17
16
RetrieveModelMixin ,
18
17
UpdateModelMixin ,
19
18
)
20
- from rest_framework .permissions import IsAuthenticated
19
+ from rest_framework .permissions import BasePermission , IsAuthenticated
21
20
from rest_framework .response import Response
22
21
from rest_framework .viewsets import GenericViewSet
23
22
@@ -211,6 +210,32 @@ class ConsultationBedFilter(filters.FilterSet):
211
210
bed = filters .UUIDFilter (field_name = "bed__external_id" )
212
211
213
212
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
+
214
239
class ConsultationBedViewSet (
215
240
ListModelMixin ,
216
241
RetrieveModelMixin ,
@@ -228,10 +253,7 @@ class ConsultationBedViewSet(
228
253
filter_backends = (filters .DjangoFilterBackend ,)
229
254
filterset_class = ConsultationBedFilter
230
255
lookup_field = "external_id"
231
- permission_classes = (
232
- IsAuthenticated ,
233
- DRYPermissions ,
234
- )
256
+ permission_classes = [TogglePatientPrivacyPermission ]
235
257
236
258
def get_queryset (self ):
237
259
user = self .request .user
@@ -254,34 +276,11 @@ def get_queryset(self):
254
276
tags = ["consultationbed" ],
255
277
)
256
278
@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 ,
287
286
)
0 commit comments