Include route distances on trips page

This commit is contained in:
Edward Betts 2024-04-05 20:17:19 +02:00
parent b5c1e16901
commit fe4bde32ba
2 changed files with 20 additions and 5 deletions

View file

@ -23,7 +23,9 @@ def load_travel(travel_type: str, data_dir: str) -> list[StrDict]:
return items return items
def load_trains(data_dir: str) -> list[StrDict]: def load_trains(
data_dir: str, route_distances: travel.RouteDistances | None = None
) -> list[StrDict]:
"""Load trains.""" """Load trains."""
trains = load_travel("train", data_dir) trains = load_travel("train", data_dir)
stations = travel.parse_yaml("stations", data_dir) stations = travel.parse_yaml("stations", data_dir)
@ -43,6 +45,12 @@ def load_trains(data_dir: str) -> list[StrDict]:
leg["from_station"] = by_name[leg["from"]] leg["from_station"] = by_name[leg["from"]]
leg["to_station"] = by_name[leg["to"]] leg["to_station"] = by_name[leg["to"]]
if route_distances:
travel.add_leg_route_distance(leg, route_distances)
if all("distance" in leg for leg in train["legs"]):
train["distance"] = sum(leg["distance"] for leg in train["legs"])
return trains return trains
@ -68,7 +76,10 @@ def load_flights(data_dir: str) -> list[StrDict]:
return flights return flights
def build_trip_list(data_dir: str | None = None) -> list[Trip]: def build_trip_list(
data_dir: str | None = None,
route_distances: travel.RouteDistances | None = None,
) -> list[Trip]:
"""Generate list of trips.""" """Generate list of trips."""
trips: dict[date, Trip] = {} trips: dict[date, Trip] = {}
@ -80,7 +91,8 @@ def build_trip_list(data_dir: str | None = None) -> list[Trip]:
yaml_trip_lookup = {item["trip"]: item for item in yaml_trip_list} yaml_trip_lookup = {item["trip"]: item for item in yaml_trip_list}
travel_items = sorted( travel_items = sorted(
load_flights(data_dir) + load_trains(data_dir), key=depart_datetime load_flights(data_dir) + load_trains(data_dir, route_distances=route_distances),
key=depart_datetime,
) )
data = { data = {

View file

@ -218,9 +218,11 @@ def accommodation_list() -> str:
@app.route("/trip") @app.route("/trip")
def trip_list() -> str: def trip_list() -> str:
"""Page showing a list of trips.""" """Page showing a list of trips."""
route_distances = agenda.travel.load_route_distances(app.config["DATA_DIR"])
trip_list = [ trip_list = [
trip trip
for trip in agenda.trip.build_trip_list() for trip in agenda.trip.build_trip_list(route_distances=route_distances)
if flask.g.user.is_authenticated or not trip.private if flask.g.user.is_authenticated or not trip.private
] ]
@ -309,9 +311,10 @@ def human_readable_delta(future_date: date) -> str | None:
@app.route("/trip/<start>") @app.route("/trip/<start>")
def trip_page(start: str) -> str: def trip_page(start: str) -> str:
"""Individual trip page.""" """Individual trip page."""
route_distances = agenda.travel.load_route_distances(app.config["DATA_DIR"])
trip_list = [ trip_list = [
trip trip
for trip in agenda.trip.build_trip_list() for trip in agenda.trip.build_trip_list(route_distances=route_distances)
if flask.g.user.is_authenticated or not trip.private if flask.g.user.is_authenticated or not trip.private
] ]