From 56bd1765df1880587dd629b7b2be019ac9c5276e Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sat, 27 Jan 2024 12:14:30 +0100 Subject: [PATCH] Better pass-midnight display --- sncfgtfs/views.py | 62 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/sncfgtfs/views.py b/sncfgtfs/views.py index 1d90fd9..7b95de3 100644 --- a/sncfgtfs/views.py +++ b/sncfgtfs/views.py @@ -1,6 +1,6 @@ from datetime import date, datetime, time, timedelta -from django.db.models import Q +from django.db.models import Q, F from django.views.generic import TemplateView from sncfgtfs.models import Stop, StopTime, CalendarDate, Calendar @@ -30,20 +30,54 @@ class GareView(TemplateView): query_time = query_time.isoformat(timespec='seconds') query_time = timedelta(seconds=int(query_time[:2]) * 3600 + int(query_time[3:5]) * 60 + int(query_time[6:])) - qs = StopTime.objects.none() + yesterday = query_date - timedelta(days=1) + time_yesterday = query_time + timedelta(days=1) + + tomorrow = query_date + timedelta(days=1) + + qs = StopTime.objects.annotate(departure_time_24h=F('departure_time')).none() for child in Stop.objects.filter(name__iexact=kwargs["gare"], location_type=0).all(): - qs_child = StopTime.objects.filter(stop=child, pickup_type=0, departure_time__gte=query_time) - qs_child = qs_child.filter(Q(trip__service_id__in=CalendarDate.objects.filter( - date=query_date, exception_type=1).values_list('service_id')) - | Q(trip__service_id__in=Calendar.objects.filter( - start_date__lte=query_date, - end_date__gte=query_date, - **{f"{query_date:%A}".lower(): True}) - .filter(~Q(id__in=CalendarDate.objects.filter( - date=query_date, exception_type=2).values_list('service_id'))) - .values_list('id'))) + qs_child = StopTime.objects.filter(stop=child, pickup_type=0) + qs_child_tod = qs_child.filter(Q(departure_time__gte=query_time) & + (Q(trip__service_id__in=CalendarDate.objects.filter( + date=query_date, exception_type=1).values_list('service_id')) + | Q(trip__service_id__in=Calendar.objects.filter( + start_date__lte=query_date, + end_date__gte=query_date, + **{f"{query_date:%A}".lower(): True}) + .filter(~Q(id__in=CalendarDate.objects.filter( + date=query_date, exception_type=2) + .values_list('service_id'))) + .values_list('id')))) \ + .annotate(departure_time_24h=F('departure_time')) + + qs_child_yes = qs_child.filter(Q(departure_time__gte=time_yesterday) & + (Q(trip__service_id__in=CalendarDate.objects.filter( + date=yesterday, exception_type=1).values_list('service_id')) + | Q(trip__service_id__in=Calendar.objects.filter( + start_date__lte=yesterday, + end_date__gte=yesterday, + **{f"{yesterday:%A}".lower(): True}) + .filter(~Q(id__in=CalendarDate.objects.filter( + date=yesterday, exception_type=2) + .values_list('service_id'))) + .values_list('id')))) \ + .annotate(departure_time_24h=F('departure_time') - timedelta(days=1)) + + qs_child_tom = qs_child.filter(Q(departure_time__gte=timedelta(0)) & + (Q(trip__service_id__in=CalendarDate.objects.filter( + date=tomorrow, exception_type=1).values_list('service_id')) + | Q(trip__service_id__in=Calendar.objects.filter( + start_date__lte=tomorrow, + end_date__gte=tomorrow, + **{f"{tomorrow:%A}".lower(): True}) + .filter(~Q(id__in=CalendarDate.objects.filter( + date=tomorrow, exception_type=2) + .values_list('service_id'))) + .values_list('id')))) \ + .annotate(departure_time_24h=F('departure_time') + timedelta(days=1)) + qs_child = qs_child_tod.union(qs_child_yes).union(qs_child_tom) qs = qs.union(qs_child) - next_departures += qs.order_by("departure_time")[:20].all() - print(len(next_departures)) + next_departures += qs.order_by("departure_time_24h")[:20].all() context['next_departures'] = next_departures return context