Manage trip additions and cancels
This commit is contained in:
@ -265,7 +265,7 @@ class Command(BaseCommand):
|
||||
dep_time = stop_time_dict['departure_time']
|
||||
dep_time = int(dep_time[:2]) * 3600 + int(dep_time[3:5]) * 60 + int(dep_time[6:])
|
||||
st = StopTime(
|
||||
id=f"{stop_time_dict['trip_id']}-{stop_time_dict['stop_sequence']}",
|
||||
id=f"{stop_time_dict['trip_id']}-{stop_time_dict['stop_id']}",
|
||||
trip_id=stop_time_dict['trip_id'],
|
||||
arrival_time=timedelta(seconds=arr_time),
|
||||
departure_time=timedelta(seconds=dep_time),
|
||||
|
@ -3,6 +3,7 @@ from zoneinfo import ZoneInfo
|
||||
|
||||
import requests
|
||||
from django.core.management import BaseCommand
|
||||
from django.db.models import Q, Max
|
||||
|
||||
from sncfgtfs.gtfs_realtime_pb2 import FeedMessage
|
||||
from sncfgtfs.models import Calendar, CalendarDate, StopTime, StopTimeUpdate, Trip, TripUpdate, Stop
|
||||
@ -41,6 +42,11 @@ class Command(BaseCommand):
|
||||
headsign = trip_id[5:-1]
|
||||
trip_qs = Trip.objects.all()
|
||||
trip_ids = trip_qs.values_list('id', flat=True)
|
||||
|
||||
last_stop_queryset = StopTime.objects.filter(
|
||||
stop__parent_station_id=trip_update.stop_time_update[-1].stop_id,
|
||||
).values('trip_id')
|
||||
trip_ids = trip_ids.intersection(last_stop_queryset)
|
||||
for stop_sequence, stop_time_update in enumerate(trip_update.stop_time_update):
|
||||
stop_id = stop_time_update.stop_id
|
||||
st_queryset = StopTime.objects.filter(stop__parent_station_id=stop_id)
|
||||
@ -49,14 +55,19 @@ class Command(BaseCommand):
|
||||
trip_ids_restrict = trip_ids.intersection(st_queryset.values('trip_id'))
|
||||
if trip_ids_restrict:
|
||||
trip_ids = trip_ids_restrict
|
||||
else:
|
||||
stop = Stop.objects.get(id=stop_id)
|
||||
self.stdout.write(self.style.WARNING(f"Warning: No trip is found passing by stop "
|
||||
f"{stop.name} ({stop_id})"))
|
||||
trip_ids = set(trip_ids)
|
||||
route_ids = set(Trip.objects.filter(id__in=trip_ids).values_list('route_id', flat=True))
|
||||
self.stdout.write(f"{len(route_ids)} routes found on trip for train {headsign}")
|
||||
self.stdout.write(f"{len(route_ids)} routes found on trip for new train {headsign}")
|
||||
if not route_ids:
|
||||
self.stdout.write(f"Route not found for trip {trip_id}.")
|
||||
continue
|
||||
elif len(route_ids) > 1:
|
||||
self.stdout.write(f"Multiple routes found for trip {trip_id}.")
|
||||
continue
|
||||
self.stderr.write(", ".join(route_ids))
|
||||
route_id = route_ids.pop()
|
||||
|
||||
Calendar.objects.update_or_create(
|
||||
@ -98,15 +109,19 @@ class Command(BaseCommand):
|
||||
stop_id = stop_time_update.stop_id
|
||||
stop = Stop.objects.get(id=stop_id)
|
||||
if stop.location_type == 1:
|
||||
if stop_sequence == 0:
|
||||
stop = sample_trip.stop_times.get(stop_sequence=0).stop
|
||||
if not StopTime.objects.filter(trip_id=trip_id).exists():
|
||||
stop = StopTime.objects.get(trip_id=sample_trip.id,
|
||||
stop__parent_station_id=stop_id).stop
|
||||
elif StopTime.objects.filter(trip_id=trip_id, stop__parent_station_id=stop_id).exists():
|
||||
stop = StopTime.objects.get(trip_id=trip_id, stop__parent_station_id=stop_id).stop
|
||||
else:
|
||||
previous_stop = sample_trip.stop_times.get(stop_sequence=stop_sequence - 1).stop
|
||||
stop = next(s for s in stop.children.all() \
|
||||
if s.location_type == 0 and s.stop_type == previous_stop.stop_type)
|
||||
stop = next(s for s in Stop.objects.filter(parent_station_id=stop_id).all()
|
||||
for s2 in StopTime.objects.filter(trip_id=trip_id).all()
|
||||
if s.stop_type in s2.stop.stop_type
|
||||
or s2.stop.stop_type in s.stop_type)
|
||||
stop_id = stop.id
|
||||
StopTime.objects.update_or_create(
|
||||
id=f"{trip_id}-{stop_sequence}",
|
||||
id=f"{trip_id}-{stop_id}",
|
||||
trip_id=trip_id,
|
||||
defaults={
|
||||
"stop_id": stop_id,
|
||||
@ -130,12 +145,46 @@ class Command(BaseCommand):
|
||||
start_time=trip_update.trip.start_time,
|
||||
schedule_relationship=trip_update.trip.schedule_relationship,
|
||||
)
|
||||
for stop_sequence, stop_time_update in enumerate(trip_update.stop_time_update):
|
||||
if not StopTime.objects.filter(trip_id=trip_id, stop_sequence=stop_sequence).exists():
|
||||
self.stdout.write(f"Stop {stop_sequence} does not exist in GTFS feed for trip {trip_id}.")
|
||||
continue
|
||||
|
||||
st = StopTime.objects.get(trip_id=trip_id, stop_sequence=stop_sequence)
|
||||
for stop_sequence, stop_time_update in enumerate(trip_update.stop_time_update):
|
||||
stop_id = stop_time_update.stop_id
|
||||
if stop_id.startswith('StopArea:'):
|
||||
if StopTime.objects.filter(trip_id=trip_id, stop__parent_station_id=stop_id).exists():
|
||||
stop = StopTime.objects.get(trip_id=trip_id, stop__parent_station_id=stop_id).stop
|
||||
else:
|
||||
stop = next(s for s in Stop.objects.filter(parent_station_id=stop_id).all()
|
||||
for s2 in StopTime.objects.filter(trip_id=trip_id).all()
|
||||
if s.stop_type in s2.stop.stop_type
|
||||
or s2.stop.stop_type in s.stop_type)
|
||||
|
||||
st, _created = StopTime.objects.update_or_create(
|
||||
id=f"{trip_id}-{stop.id}",
|
||||
trip_id=trip_id,
|
||||
stop_id=stop.id,
|
||||
defaults={
|
||||
"stop_sequence": stop_sequence,
|
||||
"arrival_time": datetime.fromtimestamp(stop_time_update.arrival.time,
|
||||
tz=ZoneInfo("Europe/Paris")) - start_dt,
|
||||
"departure_time": datetime.fromtimestamp(stop_time_update.departure.time,
|
||||
tz=ZoneInfo("Europe/Paris")) - start_dt,
|
||||
"pickup_type": 0 if stop_time_update.departure.time else 1,
|
||||
"drop_off_type": 0 if stop_time_update.arrival.time else 1,
|
||||
}
|
||||
)
|
||||
elif stop_time_update.schedule_relationship == 1:
|
||||
st = StopTime.objects.get(Q(stop=stop_id) | Q(stop__parent_station_id=stop_id),
|
||||
trip_id=trip_id)
|
||||
if st.pickup_type != 0 or st.drop_off_type != 0:
|
||||
st.pickup_type = 0
|
||||
st.drop_off_type = 0
|
||||
st.save()
|
||||
else:
|
||||
st = StopTime.objects.get(Q(stop=stop_id) | Q(stop__parent_station_id=stop_id),
|
||||
trip_id=trip_id)
|
||||
if st.stop_sequence != stop_sequence:
|
||||
st.stop_sequence = stop_sequence
|
||||
st.save()
|
||||
|
||||
st_update = StopTimeUpdate(
|
||||
trip_update=tu,
|
||||
stop_time=st,
|
||||
|
Reference in New Issue
Block a user