11from rest_framework import status
22from rest_framework .response import Response
33from rest_framework .views import APIView
4+
45from .models import Diagnosis , Medication , Suggestion
56from .serializers import MedicationSerializer
67# Constants for medication inclusion and exclusion
7- MEDS_INCLUDE = {'suicideHistory' : ['Lithium' ]}
8+ MEDS_INCLUDE = {
9+ 'suicideHistory' : ['Lithium' ]
10+ }
11+
812MED_EXCLUDE = {
913 'kidneyHistory' : ['Lithium' ],
1014 'liverHistory' : ['Valproate' ],
11- 'bloodPressureHistory' : ['Asenapine' , 'Lurasidone' , 'Olanzapine' , 'Paliperidone' , 'Quetiapine' , 'Risperidone' , 'Ziprasidone' , 'Aripiprazole' , 'Cariprazine' ],
15+ 'bloodPressureHistory' : [
16+ 'Asenapine' , 'Lurasidone' , 'Olanzapine' , 'Paliperidone' ,
17+ 'Quetiapine' , 'Risperidone' , 'Ziprasidone' , 'Aripiprazole' , 'Cariprazine'
18+ ],
1219 'weightGainConcern' : ['Quetiapine' , 'Risperidone' , 'Aripiprazole' , 'Olanzapine' ]
1320}
1421
@@ -26,12 +33,13 @@ def post(self, request):
2633 if data .get (condition , False ):
2734 # Remove any medication from include list that is in the exclude list
2835 include_result = [
29- med for med in include_result if med not in MED_EXCLUDE [condition ]]
36+ med for med in include_result if med not in MED_EXCLUDE [condition ]
37+ ]
3038 exclude_result .extend (MED_EXCLUDE [condition ])
31- diag_query = Diagnosis .objects .filter (state = state_query )
32- if diag_query .count () <= 0 :
39+ try :
40+ diagnosis = Diagnosis .objects .get (state = state_query )
41+ except Diagnosis .DoesNotExist :
3342 return Response ({'error' : 'Diagnosis not found' }, status = status .HTTP_404_NOT_FOUND )
34- diagnosis = diag_query [0 ]
3543 meds = {'first' : [], 'second' : [], 'third' : []}
3644
3745 included_set = set (include_result )
@@ -42,15 +50,18 @@ def post(self, request):
4250
4351 for i , tier_label in enumerate (['first' , 'second' , 'third' ]):
4452 suggestions = Suggestion .objects .filter (
45- diagnosis = diagnosis , tier = i + 1 )
53+ diagnosis = diagnosis , tier = i + 1
54+ )
4655 for suggestion in suggestions :
4756 med_name = suggestion .medication .name
4857 if med_name in excluded_set :
4958 continue
5059 if i > 0 and med_name in included_set :
5160 continue
52- meds [tier_label ].append (
53- {'name' : med_name , 'source' : 'diagnosis' })
61+ meds [tier_label ].append ({
62+ 'name' : med_name ,
63+ 'source' : 'diagnosis'
64+ })
5465
5566 return Response (meds )
5667
@@ -71,20 +82,7 @@ def get(self, request):
7182 return Response (serializer .data )
7283
7384 def post (self , request ):
74- # Implement logic for adding new medications (if needed)
75- # If adding medications, you would check if the medication already exists before creating it
76- data = request .data
77- name = data .get ('name' , '' )
78- if not name :
79- return Response ({'error' : 'Medication name is required' }, status = status .HTTP_400_BAD_REQUEST )
80- if Medication .objects .filter (name = name ).exists ():
81- return Response ({'error' : 'Medication already exists' }, status = status .HTTP_400_BAD_REQUEST )
82- # Assuming Medication model has `name`, `benefits`, `risks` as fields
83- serializer = MedicationSerializer (data = data )
84- if serializer .is_valid ():
85- serializer .save ()
86- return Response (serializer .data , status = status .HTTP_201_CREATED )
87- return Response (serializer .errors , status = status .HTTP_400_BAD_REQUEST )
85+ return Response ({'error' : 'Use AddMedication endpoint for creating medications' }, status = status .HTTP_405_METHOD_NOT_ALLOWED )
8886
8987
9088class AddMedication (APIView ):
@@ -97,13 +95,15 @@ def post(self, request):
9795 name = data .get ('name' , '' ).strip ()
9896 benefits = data .get ('benefits' , '' ).strip ()
9997 risks = data .get ('risks' , '' ).strip ()
100- # Validate the inputs
98+
99+ # Validate required fields
101100 if not name :
102101 return Response ({'error' : 'Medication name is required' }, status = status .HTTP_400_BAD_REQUEST )
103102 if not benefits :
104103 return Response ({'error' : 'Medication benefits are required' }, status = status .HTTP_400_BAD_REQUEST )
105104 if not risks :
106105 return Response ({'error' : 'Medication risks are required' }, status = status .HTTP_400_BAD_REQUEST )
106+
107107 # Check if medication already exists
108108 if Medication .objects .filter (name = name ).exists ():
109109 return Response ({'error' : f'Medication "{ name } " already exists' }, status = status .HTTP_400_BAD_REQUEST )
@@ -116,25 +116,22 @@ def post(self, request):
116116
117117
118118class DeleteMedication (APIView ):
119- "API endpoint to delete medication if medication in database"
119+ """
120+ API endpoint to delete medication if medication in database.
121+ """
120122
121123 def delete (self , request ):
122124 data = request .data
123125 name = data .get ('name' , '' ).strip ()
124- print ( "ok vin" )
125- # Validate the inputs
126+
127+ # Validate required fields
126128 if not name :
127129 return Response ({'error' : 'Medication name is required' }, status = status .HTTP_400_BAD_REQUEST )
128- # Check if medication exists
129- if Medication .objects .filter (name = name ).exists ():
130- # return f'Medication "{name}" exists'
131- # Get the medication object
130+
131+ # Check if medication exists and delete
132+ try :
132133 medication = Medication .objects .get (name = name )
133- # Delete the medication
134134 medication .delete ()
135- return Response ({'success' : "medication exists and will now be deleted" }, status = status .HTTP_201_CREATED )
136- else :
137- return Response ({'error' : 'Medication does not exist' }, status = status .HTTP_400_BAD_REQUEST )
138- # ask user if sure to delete?
139- # delete med from database
140- # Medication.objects.filter(name=name)
135+ return Response ({'success' : f'Medication "{ name } " has been deleted' }, status = status .HTTP_200_OK )
136+ except Medication .DoesNotExist :
137+ return Response ({'error' : f'Medication "{ name } " does not exist' }, status = status .HTTP_404_NOT_FOUND )
0 commit comments