Better pass-midnight display

This commit is contained in:
Emmy D'Anello 2024-01-27 12:14:30 +01:00
parent 8619553a30
commit 56bd1765df
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 48 additions and 14 deletions

View File

@ -1,6 +1,6 @@
from datetime import date, datetime, time, timedelta 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 django.views.generic import TemplateView
from sncfgtfs.models import Stop, StopTime, CalendarDate, Calendar from sncfgtfs.models import Stop, StopTime, CalendarDate, Calendar
@ -30,20 +30,54 @@ class GareView(TemplateView):
query_time = query_time.isoformat(timespec='seconds') 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:])) 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(): 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 = StopTime.objects.filter(stop=child, pickup_type=0)
qs_child = qs_child.filter(Q(trip__service_id__in=CalendarDate.objects.filter( 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')) date=query_date, exception_type=1).values_list('service_id'))
| Q(trip__service_id__in=Calendar.objects.filter( | Q(trip__service_id__in=Calendar.objects.filter(
start_date__lte=query_date, start_date__lte=query_date,
end_date__gte=query_date, end_date__gte=query_date,
**{f"{query_date:%A}".lower(): True}) **{f"{query_date:%A}".lower(): True})
.filter(~Q(id__in=CalendarDate.objects.filter( .filter(~Q(id__in=CalendarDate.objects.filter(
date=query_date, exception_type=2).values_list('service_id'))) date=query_date, exception_type=2)
.values_list('id'))) .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) qs = qs.union(qs_child)
next_departures += qs.order_by("departure_time")[:20].all() next_departures += qs.order_by("departure_time_24h")[:20].all()
print(len(next_departures))
context['next_departures'] = next_departures context['next_departures'] = next_departures
return context return context