Passing timetable data to index

This commit is contained in:
Johannes Randerath 2024-06-11 18:56:54 +02:00
parent a310152c4f
commit 90f76c2a30
2 changed files with 28 additions and 9 deletions

View File

@ -0,0 +1,5 @@
from django import forms
class Form1(forms.Form):
title = forms.CharField(max_length=255)
file = forms.FileField()

View File

@ -11,6 +11,7 @@ index(request)
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse
from .models import * from .models import *
from .forms import *
def print_r(r, s): def print_r(r, s):
if not len(r): if not len(r):
@ -22,14 +23,27 @@ def index(request):
route_name = lambda r : r.route_short_name if r.route_short_name else r.route_long_name route_name = lambda r : r.route_short_name if r.route_short_name else r.route_long_name
routes = [{"route_id": r.route_id, "route_type": r.route_type, "route_name": route_name(r), "agency_id": r.agency_id.agency_id} for r in Route.objects.all()] routes = [{"route_id": r.route_id, "route_type": r.route_type, "route_name": route_name(r), "agency_id": r.agency_id.agency_id} for r in Route.objects.all()]
trips = {r["route_id"]: [t for t in Trip.objects.filter(route_id_id=r["route_id"])] for r in routes} trips = {r["route_id"]: [t for t in Trip.objects.filter(route_id_id=r["route_id"])] for r in routes}
#stop_sequence = {} stop_sequences = {}
#for r in routes: for r in routes:
# seq = [] seq = []
# for s in StopTime.objects.filter(trip_id_id__exact=trips[r["route_id"]][0].trip_id) t = trips[r["route_id"]]
# stop_sequence[r["route_id"]] = [s for s in StopTime.objects.filter(trip_id_id__exact=trips[r["route_id"]].get(0).trip_id) if len(trips[r["route_id"]])] for s in StopTime.objects.filter(trip_id_id__exact=t[0].trip_id):
#print(stop_sequences) seq.append(s)
#print(trips[routes[0]["route_id"]][0]) stop_sequences[r["route_id"]] = sorted(seq, key=lambda st : st.stop_sequence)
#timetables = {r["route_id"]: {"stop_sequence": stop_sequences[r["route_id"]], "stop_times": {stop: sorted([st.departure_time for st in [StopTime.objects.filter(trip_id=t.trip_id.trip_id) for t in trips[r["route_id"]]]]) for stop in stop_sequences[r["route_id"]]}} for r in routes} timetables = {}
context = {"stops": stops, "routes": routes, }#"timetables": timetables} for r in routes:
timetable = {"stop_sequence": stop_sequences[r["route_id"]]}
sts = {}
for stop in stop_sequences[r["route_id"]]:
times = []
for t in trips[r["route_id"]]:
for st in StopTime.objects.filter(trip_id=t.trip_id):
times.append(st.departure_time)
sts[stop.stop_id] = times
timetable["stop_times"] = sts
print(timetable)
timetables[r["route_id"]] = timetable
context = {"stops": stops, "routes": routes, "timetables": timetables}
return render(request,"map.html", context) return render(request,"map.html", context)