accounts.serializers

  1from drf_spectacular.utils import extend_schema_serializer, OpenApiExample
  2from math import floor
  3from rest_framework import serializers
  4from accounts.models import User, Hospital
  5
  6@extend_schema_serializer(
  7    examples=[
  8        OpenApiExample(
  9            'Hospital Example',
 10            value={
 11                "id": 1,
 12                "name": "General Hospital",
 13                "geolocation_point": "POINT(77.5946 12.9716)",
 14                "radius": 500,
 15                "address": "123 Main St"
 16            }
 17        )
 18    ]
 19)
 20class HospitalSerializer(serializers.ModelSerializer):
 21    """
 22    Serializer for hospital details.
 23
 24    Fields:
 25        - id: Hospital ID.
 26        - name: Hospital name.
 27        - geolocation_point: Geolocation as POINT string.
 28        - radius: Coverage radius.
 29        - address: Hospital address.
 30    """
 31    class Meta:
 32        model = Hospital
 33        fields = ['id', 'name', 'geolocation_point', 'radius', 'address']
 34        read_only_fields = ['id']
 35
 36@extend_schema_serializer(
 37    examples=[
 38        OpenApiExample(
 39            'Auth Token Request Example',
 40            value={
 41                "phone": "9876543210",
 42                "password": "securepassword",
 43                "fcm_token": "fcm_token_example"
 44            }
 45        )
 46    ]
 47)
 48class AuthTokenRequestSerializer(serializers.Serializer):
 49    """
 50    Serializer for authentication token request.
 51
 52    Fields:
 53        - phone: User's phone number.
 54        - password: User's password.
 55        - fcm_token: Optional FCM token for push notifications.
 56    """
 57    phone = serializers.CharField()
 58    password = serializers.CharField()
 59    fcm_token = serializers.CharField(required=False, allow_blank=True)
 60
 61@extend_schema_serializer(
 62    examples=[
 63        OpenApiExample(
 64            'User Example',
 65            value={
 66                "id": 1,
 67                "institution_id": "EMP123",
 68                "role": "Doctor",
 69                "phone_number": "9876543210",
 70                "fcm_token": "fcm_token_example",
 71                "fcm_token_updated_at": "2025-09-20T12:00:00Z",
 72                "current_block_name": "Block A",
 73                "current_block_id": 1,
 74                "current_ward_id": 3,
 75                "current_ward_name": "Ward 3",
 76                "floor": 2,
 77                "hospital_id": 1,
 78                "is_approver": True,
 79                "is_superuser": False,
 80                "is_responder": False,
 81                "is_creator": True,
 82                "is_staff": True,
 83                "role_id": 2
 84            }
 85        )
 86    ]
 87)
 88class UserSerializer(serializers.ModelSerializer):
 89    """
 90    Serializer for user details.
 91
 92    Fields:
 93        - id: User ID.
 94        - institution_id: Institution ID.
 95        - role: Role name.
 96        - phone_number: User's phone number.
 97        - fcm_token: FCM token.
 98        - fcm_token_updated_at: Timestamp of last FCM token update.
 99        - current_block_name: Name of current block.
100        - current_block_id: ID of current block.
101        - current_ward_id: ID of current ward.
102        - current_ward_name: Name of current ward.
103        - floor: Floor number of current ward.
104        - hospital_id: Hospital ID.
105        - is_approver: Is user an approver.
106        - is_superuser: Is user a superuser.
107        - is_responder: Is user a responder.
108        - is_creator: Is user a creator.
109        - is_staff: Is user staff.
110        - role_id: Role ID.
111    """
112    current_block_name = serializers.CharField(source='current_block.name', read_only=True)
113    current_block_id = serializers.IntegerField(source='current_block.id', read_only=True)
114    current_ward_id = serializers.IntegerField(source='current_ward.id', read_only=True)
115    current_ward_name = serializers.CharField(source='current_ward.ward_name', read_only=True)
116    floor = serializers.IntegerField(source='current_ward.floor', read_only=True)
117    role = serializers.CharField(source='role.name', read_only=True)
118    role_id = serializers.IntegerField(source='role.id', read_only=True)
119    hospital_id = serializers.IntegerField(source='hospital.id', read_only=True)
120    is_approver = serializers.BooleanField(source='role.is_approver', read_only=True)
121    is_responder = serializers.BooleanField(source='role.is_responder', read_only=True)
122    is_creator = serializers.BooleanField(source='role.is_creator', read_only=True)
123    class Meta:
124        model = User
125        fields = ['id', 'institution_id', 'role', 'phone_number', 'fcm_token','fcm_token_updated_at', 'current_block_name','current_block_id','current_ward_id','current_ward_name','floor','hospital_id','is_approver','is_superuser','is_responder','is_creator','is_staff','role_id']
126        read_only_fields = ['id', 'phone_number','institution_id', 'role', 'fcm_token','fcm_token_updated_at', 'current_block_name', 'current_block_id','hospital_id','is_staff']
127
128@extend_schema_serializer(
129    examples=[
130        OpenApiExample(
131            'Auth Token Response Example',
132            value={
133                "token": "abcdef1234567890",
134                "user": {
135                    "id": 1,
136                    "institution_id": "EMP123",
137                    "role": "Doctor",
138                    "phone_number": "9876543210",
139                    "fcm_token": "fcm_token_example",
140                    "fcm_token_updated_at": "2025-09-20T12:00:00Z",
141                    "current_block_name": "Block A",
142                    "current_block_id": 1,
143                    "current_ward_id": 3,
144                    "current_ward_name": "Ward 3",
145                    "floor": 2,
146                    "hospital_id": 1,
147                    "is_approver": True,
148                    "is_superuser": False,
149                    "is_responder": False,
150                    "is_creator": True,
151                    "is_staff": True,
152                    "role_id": 2
153                },
154                "hospital": {
155                    "id": 1,
156                    "name": "General Hospital",
157                    "geolocation_point": "POINT(77.5946 12.9716)",
158                    "radius": 500,
159                    "address": "123 Main St"
160                }
161            }
162        )
163    ]
164)
165class AuthTokenResponseSerializer(serializers.Serializer):
166    """
167    Serializer for authentication token response.
168
169    Fields:
170        - token: Authentication token string.
171        - user: User details.
172        - hospital: Hospital details.
173    """
174    token = serializers.CharField()
175    user = UserSerializer()
176    hospital = HospitalSerializer()
@extend_schema_serializer(examples=[OpenApiExample('Hospital Example', value={'id': 1, 'name': 'General Hospital', 'geolocation_point': 'POINT(77.5946 12.9716)', 'radius': 500, 'address': '123 Main St'})])
class HospitalSerializer(rest_framework.serializers.ModelSerializer):
 7@extend_schema_serializer(
 8    examples=[
 9        OpenApiExample(
10            'Hospital Example',
11            value={
12                "id": 1,
13                "name": "General Hospital",
14                "geolocation_point": "POINT(77.5946 12.9716)",
15                "radius": 500,
16                "address": "123 Main St"
17            }
18        )
19    ]
20)
21class HospitalSerializer(serializers.ModelSerializer):
22    """
23    Serializer for hospital details.
24
25    Fields:
26        - id: Hospital ID.
27        - name: Hospital name.
28        - geolocation_point: Geolocation as POINT string.
29        - radius: Coverage radius.
30        - address: Hospital address.
31    """
32    class Meta:
33        model = Hospital
34        fields = ['id', 'name', 'geolocation_point', 'radius', 'address']
35        read_only_fields = ['id']

Serializer for hospital details.

Fields: - id: Hospital ID. - name: Hospital name. - geolocation_point: Geolocation as POINT string. - radius: Coverage radius. - address: Hospital address.

class HospitalSerializer.Meta:
32    class Meta:
33        model = Hospital
34        fields = ['id', 'name', 'geolocation_point', 'radius', 'address']
35        read_only_fields = ['id']
model = <class 'accounts.models.Hospital'>
fields = ['id', 'name', 'geolocation_point', 'radius', 'address']
read_only_fields = ['id']
@extend_schema_serializer(examples=[OpenApiExample('Auth Token Request Example', value={'phone': '9876543210', 'password': 'securepassword', 'fcm_token': 'fcm_token_example'})])
class AuthTokenRequestSerializer(rest_framework.serializers.Serializer):
37@extend_schema_serializer(
38    examples=[
39        OpenApiExample(
40            'Auth Token Request Example',
41            value={
42                "phone": "9876543210",
43                "password": "securepassword",
44                "fcm_token": "fcm_token_example"
45            }
46        )
47    ]
48)
49class AuthTokenRequestSerializer(serializers.Serializer):
50    """
51    Serializer for authentication token request.
52
53    Fields:
54        - phone: User's phone number.
55        - password: User's password.
56        - fcm_token: Optional FCM token for push notifications.
57    """
58    phone = serializers.CharField()
59    password = serializers.CharField()
60    fcm_token = serializers.CharField(required=False, allow_blank=True)

Serializer for authentication token request.

Fields: - phone: User's phone number. - password: User's password. - fcm_token: Optional FCM token for push notifications.

phone
password
fcm_token
@extend_schema_serializer(examples=[OpenApiExample('User Example', value={'id': 1, 'institution_id': 'EMP123', 'role': 'Doctor', 'phone_number': '9876543210', 'fcm_token': 'fcm_token_example', 'fcm_token_updated_at': '2025-09-20T12:00:00Z', 'current_block_name': 'Block A', 'current_block_id': 1, 'current_ward_id': 3, 'current_ward_name': 'Ward 3', 'floor': 2, 'hospital_id': 1, 'is_approver': True, 'is_superuser': False, 'is_responder': False, 'is_creator': True, 'is_staff': True, 'role_id': 2})])
class UserSerializer(rest_framework.serializers.ModelSerializer):
 62@extend_schema_serializer(
 63    examples=[
 64        OpenApiExample(
 65            'User Example',
 66            value={
 67                "id": 1,
 68                "institution_id": "EMP123",
 69                "role": "Doctor",
 70                "phone_number": "9876543210",
 71                "fcm_token": "fcm_token_example",
 72                "fcm_token_updated_at": "2025-09-20T12:00:00Z",
 73                "current_block_name": "Block A",
 74                "current_block_id": 1,
 75                "current_ward_id": 3,
 76                "current_ward_name": "Ward 3",
 77                "floor": 2,
 78                "hospital_id": 1,
 79                "is_approver": True,
 80                "is_superuser": False,
 81                "is_responder": False,
 82                "is_creator": True,
 83                "is_staff": True,
 84                "role_id": 2
 85            }
 86        )
 87    ]
 88)
 89class UserSerializer(serializers.ModelSerializer):
 90    """
 91    Serializer for user details.
 92
 93    Fields:
 94        - id: User ID.
 95        - institution_id: Institution ID.
 96        - role: Role name.
 97        - phone_number: User's phone number.
 98        - fcm_token: FCM token.
 99        - fcm_token_updated_at: Timestamp of last FCM token update.
100        - current_block_name: Name of current block.
101        - current_block_id: ID of current block.
102        - current_ward_id: ID of current ward.
103        - current_ward_name: Name of current ward.
104        - floor: Floor number of current ward.
105        - hospital_id: Hospital ID.
106        - is_approver: Is user an approver.
107        - is_superuser: Is user a superuser.
108        - is_responder: Is user a responder.
109        - is_creator: Is user a creator.
110        - is_staff: Is user staff.
111        - role_id: Role ID.
112    """
113    current_block_name = serializers.CharField(source='current_block.name', read_only=True)
114    current_block_id = serializers.IntegerField(source='current_block.id', read_only=True)
115    current_ward_id = serializers.IntegerField(source='current_ward.id', read_only=True)
116    current_ward_name = serializers.CharField(source='current_ward.ward_name', read_only=True)
117    floor = serializers.IntegerField(source='current_ward.floor', read_only=True)
118    role = serializers.CharField(source='role.name', read_only=True)
119    role_id = serializers.IntegerField(source='role.id', read_only=True)
120    hospital_id = serializers.IntegerField(source='hospital.id', read_only=True)
121    is_approver = serializers.BooleanField(source='role.is_approver', read_only=True)
122    is_responder = serializers.BooleanField(source='role.is_responder', read_only=True)
123    is_creator = serializers.BooleanField(source='role.is_creator', read_only=True)
124    class Meta:
125        model = User
126        fields = ['id', 'institution_id', 'role', 'phone_number', 'fcm_token','fcm_token_updated_at', 'current_block_name','current_block_id','current_ward_id','current_ward_name','floor','hospital_id','is_approver','is_superuser','is_responder','is_creator','is_staff','role_id']
127        read_only_fields = ['id', 'phone_number','institution_id', 'role', 'fcm_token','fcm_token_updated_at', 'current_block_name', 'current_block_id','hospital_id','is_staff']

Serializer for user details.

Fields: - id: User ID. - institution_id: Institution ID. - role: Role name. - phone_number: User's phone number. - fcm_token: FCM token. - fcm_token_updated_at: Timestamp of last FCM token update. - current_block_name: Name of current block. - current_block_id: ID of current block. - current_ward_id: ID of current ward. - current_ward_name: Name of current ward. - floor: Floor number of current ward. - hospital_id: Hospital ID. - is_approver: Is user an approver. - is_superuser: Is user a superuser. - is_responder: Is user a responder. - is_creator: Is user a creator. - is_staff: Is user staff. - role_id: Role ID.

current_block_name
current_block_id
current_ward_id
current_ward_name
floor
role
role_id
hospital_id
is_approver
is_responder
is_creator
class UserSerializer.Meta:
124    class Meta:
125        model = User
126        fields = ['id', 'institution_id', 'role', 'phone_number', 'fcm_token','fcm_token_updated_at', 'current_block_name','current_block_id','current_ward_id','current_ward_name','floor','hospital_id','is_approver','is_superuser','is_responder','is_creator','is_staff','role_id']
127        read_only_fields = ['id', 'phone_number','institution_id', 'role', 'fcm_token','fcm_token_updated_at', 'current_block_name', 'current_block_id','hospital_id','is_staff']
model = <class 'accounts.models.User'>
fields = ['id', 'institution_id', 'role', 'phone_number', 'fcm_token', 'fcm_token_updated_at', 'current_block_name', 'current_block_id', 'current_ward_id', 'current_ward_name', 'floor', 'hospital_id', 'is_approver', 'is_superuser', 'is_responder', 'is_creator', 'is_staff', 'role_id']
read_only_fields = ['id', 'phone_number', 'institution_id', 'role', 'fcm_token', 'fcm_token_updated_at', 'current_block_name', 'current_block_id', 'hospital_id', 'is_staff']
@extend_schema_serializer(examples=[OpenApiExample('Auth Token Response Example', value={'token': 'abcdef1234567890', 'user': {'id': 1, 'institution_id': 'EMP123', 'role': 'Doctor', 'phone_number': '9876543210', 'fcm_token': 'fcm_token_example', 'fcm_token_updated_at': '2025-09-20T12:00:00Z', 'current_block_name': 'Block A', 'current_block_id': 1, 'current_ward_id': 3, 'current_ward_name': 'Ward 3', 'floor': 2, 'hospital_id': 1, 'is_approver': True, 'is_superuser': False, 'is_responder': False, 'is_creator': True, 'is_staff': True, 'role_id': 2}, 'hospital': {'id': 1, 'name': 'General Hospital', 'geolocation_point': 'POINT(77.5946 12.9716)', 'radius': 500, 'address': '123 Main St'}})])
class AuthTokenResponseSerializer(rest_framework.serializers.Serializer):
129@extend_schema_serializer(
130    examples=[
131        OpenApiExample(
132            'Auth Token Response Example',
133            value={
134                "token": "abcdef1234567890",
135                "user": {
136                    "id": 1,
137                    "institution_id": "EMP123",
138                    "role": "Doctor",
139                    "phone_number": "9876543210",
140                    "fcm_token": "fcm_token_example",
141                    "fcm_token_updated_at": "2025-09-20T12:00:00Z",
142                    "current_block_name": "Block A",
143                    "current_block_id": 1,
144                    "current_ward_id": 3,
145                    "current_ward_name": "Ward 3",
146                    "floor": 2,
147                    "hospital_id": 1,
148                    "is_approver": True,
149                    "is_superuser": False,
150                    "is_responder": False,
151                    "is_creator": True,
152                    "is_staff": True,
153                    "role_id": 2
154                },
155                "hospital": {
156                    "id": 1,
157                    "name": "General Hospital",
158                    "geolocation_point": "POINT(77.5946 12.9716)",
159                    "radius": 500,
160                    "address": "123 Main St"
161                }
162            }
163        )
164    ]
165)
166class AuthTokenResponseSerializer(serializers.Serializer):
167    """
168    Serializer for authentication token response.
169
170    Fields:
171        - token: Authentication token string.
172        - user: User details.
173        - hospital: Hospital details.
174    """
175    token = serializers.CharField()
176    user = UserSerializer()
177    hospital = HospitalSerializer()

Serializer for authentication token response.

Fields: - token: Authentication token string. - user: User details. - hospital: Hospital details.

token
user
hospital