Add departure_date and departure_time_24h (idem for arrivals) in next trains API results

This commit is contained in:
Emmy D'Anello 2024-01-29 22:10:50 +01:00
parent 1e7dd4c517
commit 90036a0d3f
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 12 additions and 1 deletions

View File

@ -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__'

View File

@ -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()