-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement user management features with authentication, profile…
… handling, and error logging
- Loading branch information
1 parent
c915b13
commit 36ce8cb
Showing
67 changed files
with
3,386 additions
and
1,038 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,155 @@ | ||
# apps/trains/api/serializers.py | ||
|
||
from rest_framework import serializers | ||
from ..models import Train, Schedule, TrainCar, CrowdMeasurement | ||
|
||
from ..models import CrowdMeasurement, Schedule, Train | ||
|
||
class CrowdLevelSerializer(serializers.ModelSerializer): | ||
"""Serializer for crowd level information.""" | ||
|
||
class TrainSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Train | ||
fields = "__all__" | ||
model = CrowdMeasurement | ||
fields = [ | ||
'id', | ||
'passenger_count', | ||
'crowd_percentage', | ||
'confidence_score', | ||
'measurement_method', | ||
'timestamp' | ||
] | ||
|
||
def to_representation(self, instance): | ||
data = super().to_representation(instance) | ||
data["line_name"] = instance.line.name | ||
data["current_station_name"] = instance.current_station.name if instance.current_station else None | ||
return data | ||
|
||
class TrainCarSerializer(serializers.ModelSerializer): | ||
"""Serializer for train car information.""" | ||
|
||
crowd_level = serializers.SerializerMethodField() | ||
|
||
class Meta: | ||
model = TrainCar | ||
fields = [ | ||
'car_number', | ||
'capacity', | ||
'current_load', | ||
'crowd_status', | ||
'load_percentage', | ||
'is_operational', | ||
'crowd_level' | ||
] | ||
|
||
def get_crowd_level(self, obj): | ||
return { | ||
'level': obj.crowd_status, | ||
'percentage': obj.load_percentage, | ||
'available_seats': max(0, obj.capacity - obj.current_load) | ||
} | ||
|
||
|
||
class ScheduleSerializer(serializers.ModelSerializer): | ||
"""Serializer for train schedules with computed fields.""" | ||
|
||
station_name = serializers.CharField(source='station.name', read_only=True) | ||
train_id = serializers.CharField(source='train.train_id', read_only=True) | ||
delay_duration = serializers.IntegerField(read_only=True) | ||
line_name = serializers.CharField(source='train.line.name', read_only=True) | ||
|
||
class Meta: | ||
model = Schedule | ||
fields = "__all__" | ||
fields = [ | ||
'id', | ||
'train_id', | ||
'station_name', | ||
'line_name', | ||
'arrival_time', | ||
'status', | ||
'expected_crowd_level', | ||
'is_active', | ||
'delay_duration', | ||
] | ||
read_only_fields = ['delay_duration', 'is_active'] | ||
|
||
def to_representation(self, instance): | ||
"""Add computed fields to the representation""" | ||
data = super().to_representation(instance) | ||
data["station_name"] = instance.station.name | ||
data["train_id"] = instance.train.train_id | ||
data['is_delayed'] = instance.is_delayed | ||
data['is_cancelled'] = instance.is_cancelled | ||
return data | ||
|
||
|
||
class TrainStatusSerializer(serializers.Serializer): | ||
train_id = serializers.CharField() | ||
current_location = serializers.DictField() | ||
speed = serializers.FloatField() | ||
status = serializers.CharField() | ||
next_station = serializers.CharField() | ||
estimated_arrival = serializers.DateTimeField() | ||
crowd_level = serializers.CharField() | ||
class TrainSerializer(serializers.ModelSerializer): | ||
"""Serializer for train information.""" | ||
|
||
cars = TrainCarSerializer(many=True, read_only=True) | ||
line_name = serializers.CharField(source='line.name', read_only=True) | ||
current_station_name = serializers.CharField(source='current_station.name', read_only=True) | ||
next_station_name = serializers.CharField(source='next_station.name', read_only=True) | ||
|
||
class CrowdLevelSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = CrowdMeasurement | ||
fields = ["timestamp", "passenger_count", "crowd_percentage", "confidence_score"] | ||
model = Train | ||
fields = [ | ||
'id', | ||
'train_id', | ||
'line_name', | ||
'status', | ||
'has_air_conditioning', | ||
'current_station_name', | ||
'next_station_name', | ||
'direction', | ||
'speed', | ||
'cars', | ||
'last_updated' | ||
] | ||
|
||
|
||
class TrainDetailSerializer(serializers.ModelSerializer): | ||
schedule = ScheduleSerializer(many=True, read_only=True, source="schedule_set") | ||
crowd_measurements = CrowdLevelSerializer(many=True, read_only=True, source="crowdmeasurement_set") | ||
"""Detailed train serializer including schedules and crowd information.""" | ||
|
||
cars = TrainCarSerializer(many=True, read_only=True) | ||
schedules = ScheduleSerializer(many=True, read_only=True) | ||
line_name = serializers.CharField(source='line.name', read_only=True) | ||
current_station_name = serializers.CharField(source='current_station.name', read_only=True) | ||
next_station_name = serializers.CharField(source='next_station.name', read_only=True) | ||
crowd_measurements = CrowdLevelSerializer(many=True, read_only=True) | ||
|
||
class Meta: | ||
model = Train | ||
fields = [ | ||
"train_id", | ||
"line", | ||
"status", | ||
"current_station", | ||
"next_station", | ||
"last_updated", | ||
"schedule", | ||
"crowd_measurements", | ||
'id', | ||
'train_id', | ||
'line_name', | ||
'status', | ||
'has_air_conditioning', | ||
'current_station_name', | ||
'next_station_name', | ||
'direction', | ||
'speed', | ||
'cars', | ||
'schedules', | ||
'crowd_measurements', | ||
'last_updated' | ||
] | ||
|
||
def to_representation(self, instance): | ||
"""Add additional computed fields to the representation.""" | ||
data = super().to_representation(instance) | ||
data["line_name"] = instance.line.name | ||
data["current_station_name"] = instance.current_station.name if instance.current_station else None | ||
data["next_station_name"] = instance.next_station.name if instance.next_station else None | ||
|
||
# Add real-time information | ||
data['is_delayed'] = instance.status == 'DELAYED' | ||
data['is_operational'] = instance.status in ['IN_SERVICE', 'DELAYED'] | ||
|
||
# Add crowd summary | ||
cars_data = data.get('cars', []) | ||
if cars_data: | ||
total_load = sum(car['current_load'] for car in cars_data) | ||
total_capacity = sum(car['capacity'] for car in cars_data) | ||
data['total_passengers'] = total_load | ||
data['occupancy_percentage'] = round((total_load / total_capacity) * 100, 1) if total_capacity else 0 | ||
|
||
return data | ||
|
||
|
||
class CrowdSummarySerializer(serializers.Serializer): | ||
"""Serializer for crowd summary information.""" | ||
|
||
train_id = serializers.IntegerField() | ||
crowd_data = serializers.ListField(child=serializers.DictField()) | ||
is_ac = serializers.BooleanField() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.