File size: 2,585 Bytes
718f018
660852a
718f018
 
 
 
 
 
 
 
 
 
6a7f9f2
 
 
 
 
 
 
 
718f018
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from rest_framework import serializers
from .models import UserProfile, TestResult, Appointment
from django.contrib.auth.models import User

class UserProfileSerializer(serializers.ModelSerializer):
    email = serializers.EmailField(source='user.email', read_only=True)
    
    class Meta:
        model = UserProfile
        # Add the new fields to the serializer
        fields = ['id', 'email', 'full_name', 'phone', 'address', 'role', 'state', 'city', 'age', 'gender', 'license_number']

class AppointmentSerializer(serializers.ModelSerializer):
    patient_name = serializers.CharField(source='patient.full_name', read_only=True)
    doctor_name = serializers.CharField(source='doctor.full_name', read_only=True)
    
    class Meta:
        model = Appointment # Ensure you import Appointment from .models
        fields = ['id', 'patient', 'doctor', 'patient_name', 'doctor_name', 'date', 'time', 'reason', 'status', 'created_at']

class TestResultSerializer(serializers.ModelSerializer):
    # Use full_name if available, otherwise fallback to email
    patient_name = serializers.SerializerMethodField()
    
    # Fetch extra patient details
    email = serializers.CharField(source='patient.user.email', read_only=True) 
    
    state = serializers.CharField(source='patient.state', read_only=True)
    city = serializers.CharField(source='patient.city', read_only=True)
    age = serializers.IntegerField(source='patient.age', read_only=True)
    gender = serializers.CharField(source='patient.gender', read_only=True)
    phone = serializers.CharField(source='patient.phone', read_only=True)

    # OVERRIDE the model field to dynamically calculate risk level for ALL records (old and new)
    risk_level = serializers.SerializerMethodField()

    class Meta:
        model = TestResult
        fields = '__all__'
        
    def get_patient_name(self, obj):
        # Return full_name if it exists, else return email
        if obj.patient.full_name:
            return obj.patient.full_name
        return obj.patient.user.email

    def get_risk_level(self, obj):
        """
        Dynamically calculates Risk Level based on Confidence Score.
        This ensures 'Medium' is returned for 50-80% even if the DB says 'Low'.
        """
        if obj.result == 'Negative':
            return 'Low'
        
        # Positive Case Logic
        # obj.confidence_score is stored as 0-100 float
        if obj.confidence_score > 80:
            return 'High'
        elif obj.confidence_score >= 50:
            return 'Medium'
        else:
            return 'Low'