From 90036a0d3f7694d186eb440c73391940b8448afa Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Mon, 29 Jan 2024 22:10:50 +0100 Subject: [PATCH] Add departure_date and departure_time_24h (idem for arrivals) in next trains API results --- sncf/api/serializers.py | 5 +++++ sncf/api/views.py | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/sncf/api/serializers.py b/sncf/api/serializers.py index 9dfd9c2..c763cb5 100644 --- a/sncf/api/serializers.py +++ b/sncf/api/serializers.py @@ -29,6 +29,11 @@ class TripSerializer(serializers.ModelSerializer): class StopTimeSerializer(serializers.ModelSerializer): + arrival_date = serializers.DateField(required=False) + departure_date = serializers.DateField(required=False) + arrival_time_24h = serializers.DurationField(required=False) + departure_time_24h = serializers.DurationField(required=False) + class Meta: model = StopTime fields = '__all__' diff --git a/sncf/api/views.py b/sncf/api/views.py index 9306265..4e40115 100644 --- a/sncf/api/views.py +++ b/sncf/api/views.py @@ -1,6 +1,6 @@ from datetime import datetime, timedelta, date -from django.db.models import F, Q +from django.db.models import F, Q, Value from django_filters.rest_framework import DjangoFilterBackend from rest_framework import viewsets from rest_framework.filters import OrderingFilter @@ -121,14 +121,17 @@ class NextDeparturesViewSet(viewsets.ReadOnlyModelViewSet): qs_today = StopTime.objects.filter(stop_filter) \ .filter(Q(departure_time__gte=query_time, pickup_type=0), calendar_filter(query_date)) \ + .annotate(departure_date=Value(query_date)) \ .annotate(departure_time_24h=F('departure_time')) qs_yesterday = StopTime.objects.filter(stop_filter) \ .filter(Q(departure_time__gte=time_yesterday, pickup_type=0), calendar_filter(yesterday)) \ + .annotate(departure_date=Value(yesterday)) \ .annotate(departure_time_24h=F('departure_time') - timedelta(days=1)) qs_tomorrow = StopTime.objects.filter(stop_filter) \ .filter(Q(departure_time__gte=timedelta(0), pickup_type=0), calendar_filter(tomorrow)) \ + .annotate(departure_date=Value(tomorrow)) \ .annotate(departure_time_24h=F('departure_time') + timedelta(days=1)) return qs_today.union(qs_yesterday).union(qs_tomorrow).order_by("departure_time_24h").all() @@ -179,14 +182,17 @@ class NextArrivalsViewSet(viewsets.ReadOnlyModelViewSet): qs_today = StopTime.objects.filter(stop_filter) \ .filter(Q(arrival_time__gte=query_time, drop_off_type=0), calendar_filter(query_date)) \ + .annotate(arrival_date=Value(query_date)) \ .annotate(arrival_time_24h=F('arrival_time')) qs_yesterday = StopTime.objects.filter(stop_filter) \ .filter(Q(arrival_time__gte=time_yesterday, drop_off_type=0), calendar_filter(yesterday)) \ + .annotate(arrival_date=Value(yesterday)) \ .annotate(arrival_time_24h=F('arrival_time') - timedelta(days=1)) qs_tomorrow = StopTime.objects.filter(stop_filter) \ .filter(Q(arrival_time__gte=timedelta(0), drop_off_type=0), calendar_filter(tomorrow)) \ + .annotate(arrival_date=Value(tomorrow)) \ .annotate(arrival_time_24h=F('arrival_time') + timedelta(days=1)) return qs_today.union(qs_yesterday).union(qs_tomorrow).order_by("arrival_time_24h").all()