Coverage for apps/patients/views.py: 65%
61 statements
« prev ^ index » next coverage.py v6.4.4, created at 2024-04-30 13:30 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2024-04-30 13:30 -0600
1from django.db.models import ProtectedError
2from rest_framework import status
3from rest_framework.mixins import CreateModelMixin as Create
4from rest_framework.mixins import DestroyModelMixin as Delete
5from rest_framework.mixins import ListModelMixin as List
6from rest_framework.mixins import RetrieveModelMixin as Detail
7from rest_framework.mixins import UpdateModelMixin as Update
8from rest_framework.response import Response
9from rest_framework.viewsets import GenericViewSet, ModelViewSet
11from apps.users.mixins import AdminMixin, UserMixin
12from services.pusher import PusherClient
14from .serializers import AddressSerializer, EmailSerializer, PatientDetailSerializer, PatientSerializer, PhoneSerializer
17class AdminPatientViewSet(AdminMixin, ModelViewSet):
18 """
19 Model ViewSet for Patients
20 """
22 serializer_class = PatientSerializer
25class UserPatientViewSet(UserMixin, GenericViewSet, Create, List, Detail, Update, Delete):
26 """
27 Patient ViewSet for User
28 """
30 serializer_class = PatientSerializer
31 search_fields = ["full_name"]
32 filterset_fields = ["organization"]
34 def _push_notification(self, channel: str, event: str = "patient-update", data: dict = {}):
35 pusher = PusherClient()
36 pusher.trigger(channel, event, data)
38 def retrieve(self, request, *args, **kwargs):
39 self.serializer_class = PatientDetailSerializer
40 return super().retrieve(request, *args, **kwargs)
42 def perform_create(self, serializer):
43 patient = serializer.save()
44 try:
45 self._push_notification(
46 patient.organization.random_slug,
47 "patient-creation",
48 {
49 "user": getattr(self.request.user, "pk", None),
50 },
51 )
52 except Exception as e:
53 print(e)
54 return patient
56 def update(self, request, *args, **kwargs):
57 self._push_notification(
58 self.get_object().organization.random_slug,
59 "patient-update",
60 {
61 "user": getattr(request.user, "pk", None),
62 },
63 )
64 return super().update(request, *args, **kwargs)
66 def partial_update(self, request, *args, **kwargs):
67 self._push_notification(
68 self.get_object().organization.random_slug,
69 "patient-update",
70 {
71 "user": getattr(request.user, "pk", None),
72 },
73 )
74 return super().partial_update(request, *args, **kwargs)
76 def destroy(self, request, *args, **kwargs):
77 try:
78 self._push_notification(
79 self.get_object().organization.random_slug,
80 "patient-destroy",
81 {
82 "user": getattr(request.user, "pk", None),
83 },
84 )
85 return super().destroy(request, *args, **kwargs)
86 except ProtectedError as protected_error:
87 protected_elements = [
88 {
89 "id": protected_object.pk,
90 "type": str(protected_object._meta.verbose_name),
91 "label": str(protected_object),
92 }
93 for protected_object in protected_error.protected_objects
94 ]
95 response_data = {"protected_elements": protected_elements}
96 return Response(data=response_data, status=status.HTTP_400_BAD_REQUEST)
98 def get_queryset(self):
99 if getattr(self.user, "practitioner", None):
100 return super().get_queryset().filter(practitioner=self.user.practitioner)
101 return super().get_queryset().filter(organization__in=self.user.affiliations.values_list("organization"))
104class AdminEmailViewSet(AdminMixin, ModelViewSet):
105 """
106 Model ViewSet for Emails
107 """
109 serializer_class = EmailSerializer
112class AdminPhoneViewSet(AdminMixin, ModelViewSet):
113 """
114 Model ViewSet for Phones
115 """
117 serializer_class = PhoneSerializer
120class AdminAddressViewSet(AdminMixin, ModelViewSet):
121 """
122 Model ViewSet for Addresses
123 """
125 serializer_class = AddressSerializer
128class UserEmailViewSet(UserMixin, GenericViewSet, Create, Detail, Update, Delete):
129 """
130 Model ViewSet for Emails
131 """
133 serializer_class = EmailSerializer
136class UserPhoneViewSet(UserMixin, GenericViewSet, Create, Detail, Update, Delete):
137 """
138 Model ViewSet for Phones
139 """
141 serializer_class = PhoneSerializer
144class UserAddressViewSet(UserMixin, GenericViewSet, Create, Detail, Update, Delete):
145 """
146 Model ViewSet for Addresses
147 """
149 serializer_class = AddressSerializer