Spaces:
Sleeping
Sleeping
Update backend/api/views.py
Browse files- backend/api/views.py +80 -6
backend/api/views.py
CHANGED
|
@@ -6,8 +6,10 @@ import json
|
|
| 6 |
import os
|
| 7 |
from rest_framework.response import Response
|
| 8 |
|
| 9 |
-
|
| 10 |
-
from .
|
|
|
|
|
|
|
| 11 |
from .ml_engine import predict_xray
|
| 12 |
from .storage import upload_to_supabase
|
| 13 |
from .pdf_generator import generate_medical_pdf
|
|
@@ -29,7 +31,7 @@ class UserProfileView(views.APIView):
|
|
| 29 |
else:
|
| 30 |
if requested_role == 'doctor':
|
| 31 |
provided_code = data.get('access_code')
|
| 32 |
-
secure_code = "e63ecb7857b348c5a79645c92578f5260defd2bde982bf823b8f66f5133fea52"
|
| 33 |
|
| 34 |
# Check if provided_code exists, convert to string, strip whitespace, and compare
|
| 35 |
if provided_code and str(provided_code).strip() == secure_code:
|
|
@@ -84,7 +86,6 @@ class PredictionView(views.APIView):
|
|
| 84 |
image_file.seek(0)
|
| 85 |
result, confidence, risk_level = predict_xray(image_file)
|
| 86 |
|
| 87 |
-
# --- REVERTED: Just create the record, no email logic here ---
|
| 88 |
test_record = TestResult.objects.create(
|
| 89 |
patient=user_profile,
|
| 90 |
xray_image_url=image_url,
|
|
@@ -198,7 +199,6 @@ class DownloadReportView(views.APIView):
|
|
| 198 |
return response
|
| 199 |
|
| 200 |
except Exception as e:
|
| 201 |
-
print(f"Error generating PDF: {e}")
|
| 202 |
print(f"Error generating PDF: {e}")
|
| 203 |
return Response({"error": "Failed to generate report"}, status=500)
|
| 204 |
|
|
@@ -207,4 +207,78 @@ class PublicStatsView(views.APIView):
|
|
| 207 |
|
| 208 |
def get(self, request):
|
| 209 |
total_count = TestResult.objects.count()
|
| 210 |
-
return response.Response({"total_tests": total_count})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import os
|
| 7 |
from rest_framework.response import Response
|
| 8 |
|
| 9 |
+
# Added Appointment to imports
|
| 10 |
+
from .models import UserProfile, TestResult, Appointment
|
| 11 |
+
# Added AppointmentSerializer to imports
|
| 12 |
+
from .serializers import UserProfileSerializer, TestResultSerializer, AppointmentSerializer
|
| 13 |
from .ml_engine import predict_xray
|
| 14 |
from .storage import upload_to_supabase
|
| 15 |
from .pdf_generator import generate_medical_pdf
|
|
|
|
| 31 |
else:
|
| 32 |
if requested_role == 'doctor':
|
| 33 |
provided_code = data.get('access_code')
|
| 34 |
+
secure_code = "e63ecb7857b348c5a79645c92578f5260defd2bde982bf823b8f66f5133fea52"
|
| 35 |
|
| 36 |
# Check if provided_code exists, convert to string, strip whitespace, and compare
|
| 37 |
if provided_code and str(provided_code).strip() == secure_code:
|
|
|
|
| 86 |
image_file.seek(0)
|
| 87 |
result, confidence, risk_level = predict_xray(image_file)
|
| 88 |
|
|
|
|
| 89 |
test_record = TestResult.objects.create(
|
| 90 |
patient=user_profile,
|
| 91 |
xray_image_url=image_url,
|
|
|
|
| 199 |
return response
|
| 200 |
|
| 201 |
except Exception as e:
|
|
|
|
| 202 |
print(f"Error generating PDF: {e}")
|
| 203 |
return Response({"error": "Failed to generate report"}, status=500)
|
| 204 |
|
|
|
|
| 207 |
|
| 208 |
def get(self, request):
|
| 209 |
total_count = TestResult.objects.count()
|
| 210 |
+
return response.Response({"total_tests": total_count})
|
| 211 |
+
|
| 212 |
+
# --- NEW APPOINTMENT SYSTEM VIEWS ---
|
| 213 |
+
|
| 214 |
+
class DoctorListView(views.APIView):
|
| 215 |
+
permission_classes = [IsAuthenticated]
|
| 216 |
+
|
| 217 |
+
def get(self, request):
|
| 218 |
+
# Filter profiles where role is 'doctor'
|
| 219 |
+
doctors = UserProfile.objects.filter(role='doctor')
|
| 220 |
+
return response.Response(UserProfileSerializer(doctors, many=True).data)
|
| 221 |
+
|
| 222 |
+
class AppointmentView(views.APIView):
|
| 223 |
+
permission_classes = [IsAuthenticated]
|
| 224 |
+
|
| 225 |
+
def get(self, request):
|
| 226 |
+
try:
|
| 227 |
+
profile = request.user.profile
|
| 228 |
+
# If Doctor, show appointments assigned to them
|
| 229 |
+
if profile.role == 'doctor':
|
| 230 |
+
appointments = Appointment.objects.filter(doctor=profile).order_by('date_time')
|
| 231 |
+
# If Patient, show their own bookings
|
| 232 |
+
else:
|
| 233 |
+
appointments = Appointment.objects.filter(patient=profile).order_by('date_time')
|
| 234 |
+
|
| 235 |
+
return response.Response(AppointmentSerializer(appointments, many=True).data)
|
| 236 |
+
except UserProfile.DoesNotExist:
|
| 237 |
+
return response.Response({"error": "Profile not found"}, status=404)
|
| 238 |
+
|
| 239 |
+
def post(self, request):
|
| 240 |
+
try:
|
| 241 |
+
patient_profile = request.user.profile
|
| 242 |
+
doctor_id = request.data.get('doctor_id')
|
| 243 |
+
date_time = request.data.get('date_time')
|
| 244 |
+
reason = request.data.get('reason', '')
|
| 245 |
+
|
| 246 |
+
if not doctor_id or not date_time:
|
| 247 |
+
return response.Response({"error": "Doctor and Date are required"}, status=400)
|
| 248 |
+
|
| 249 |
+
try:
|
| 250 |
+
doctor_profile = UserProfile.objects.get(id=doctor_id, role='doctor')
|
| 251 |
+
except UserProfile.DoesNotExist:
|
| 252 |
+
return response.Response({"error": "Selected doctor not found or invalid"}, status=404)
|
| 253 |
+
|
| 254 |
+
appointment = Appointment.objects.create(
|
| 255 |
+
patient=patient_profile,
|
| 256 |
+
doctor=doctor_profile,
|
| 257 |
+
date_time=date_time,
|
| 258 |
+
reason=reason,
|
| 259 |
+
status='pending'
|
| 260 |
+
)
|
| 261 |
+
return response.Response(AppointmentSerializer(appointment).data, status=status.HTTP_201_CREATED)
|
| 262 |
+
|
| 263 |
+
except Exception as e:
|
| 264 |
+
return response.Response({"error": str(e)}, status=400)
|
| 265 |
+
|
| 266 |
+
class AppointmentStatusView(views.APIView):
|
| 267 |
+
permission_classes = [IsAuthenticated]
|
| 268 |
+
|
| 269 |
+
def patch(self, request, pk):
|
| 270 |
+
try:
|
| 271 |
+
appointment = Appointment.objects.get(pk=pk)
|
| 272 |
+
# Permission check: User must be either the doctor or patient involved
|
| 273 |
+
if request.user.profile != appointment.doctor and request.user.profile != appointment.patient:
|
| 274 |
+
return response.Response({"error": "Unauthorized"}, status=403)
|
| 275 |
+
|
| 276 |
+
new_status = request.data.get('status')
|
| 277 |
+
if new_status in dict(Appointment.STATUS_CHOICES):
|
| 278 |
+
appointment.status = new_status
|
| 279 |
+
appointment.save()
|
| 280 |
+
return response.Response(AppointmentSerializer(appointment).data)
|
| 281 |
+
return response.Response({"error": "Invalid status"}, status=400)
|
| 282 |
+
|
| 283 |
+
except Appointment.DoesNotExist:
|
| 284 |
+
return response.Response({"error": "Appointment not found"}, status=404)
|