Show ferry bookings in trip list

This commit is contained in:
Edward Betts 2024-05-01 08:32:15 +02:00
parent a5d1290491
commit b9b849802d
3 changed files with 71 additions and 10 deletions

View file

@ -19,9 +19,9 @@ class UnknownStation(Exception):
pass pass
def load_travel(travel_type: str, data_dir: str) -> list[StrDict]: def load_travel(travel_type: str, plural: str, data_dir: str) -> list[StrDict]:
"""Read flight and train journeys.""" """Read flight and train journeys."""
items = travel.parse_yaml(travel_type + "s", data_dir) items = travel.parse_yaml(plural, data_dir)
for item in items: for item in items:
item["type"] = travel_type item["type"] = travel_type
return items return items
@ -31,7 +31,7 @@ def load_trains(
data_dir: str, route_distances: travel.RouteDistances | None = None data_dir: str, route_distances: travel.RouteDistances | None = None
) -> list[StrDict]: ) -> list[StrDict]:
"""Load trains.""" """Load trains."""
trains = load_travel("train", data_dir) trains = load_travel("train", "trains", data_dir)
stations = travel.parse_yaml("stations", data_dir) stations = travel.parse_yaml("stations", data_dir)
by_name = {station["name"]: station for station in stations} by_name = {station["name"]: station for station in stations}
@ -58,6 +58,20 @@ def load_trains(
return trains return trains
def load_ferries(data_dir: str) -> list[StrDict]:
"""Load ferries."""
ferries = load_travel("ferry", "ferries", data_dir)
terminals = travel.parse_yaml("ferry_terminals", data_dir)
by_name = {terminal["name"]: terminal for terminal in terminals}
for item in ferries:
assert item["from"] in by_name and item["to"] in by_name
item["from_terminal"] = by_name[item["from"]]
item["to_terminal"] = by_name[item["to"]]
return ferries
def depart_datetime(item: StrDict) -> datetime: def depart_datetime(item: StrDict) -> datetime:
depart = item["depart"] depart = item["depart"]
if isinstance(depart, datetime): if isinstance(depart, datetime):
@ -67,7 +81,7 @@ def depart_datetime(item: StrDict) -> datetime:
def load_flight_bookings(data_dir: str) -> list[StrDict]: def load_flight_bookings(data_dir: str) -> list[StrDict]:
"""Load flight bookings.""" """Load flight bookings."""
bookings = load_travel("flight", data_dir) bookings = load_travel("flight", "flights", data_dir)
airlines = yaml.safe_load(open(os.path.join(data_dir, "airlines.yaml"))) airlines = yaml.safe_load(open(os.path.join(data_dir, "airlines.yaml")))
airports = travel.parse_yaml("airports", data_dir) airports = travel.parse_yaml("airports", data_dir)
for booking in bookings: for booking in bookings:
@ -110,7 +124,9 @@ def build_trip_list(
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, route_distances=route_distances), load_flights(data_dir)
+ load_trains(data_dir, route_distances=route_distances)
+ load_ferries(data_dir),
key=depart_datetime, key=depart_datetime,
) )
@ -169,17 +185,22 @@ def collect_trip_coordinates(trip: Trip) -> list[StrDict]:
stations = {} stations = {}
station_list = [] station_list = []
airports = {} airports = {}
ferry_terminals = {}
for t in trip.travel: for t in trip.travel:
if t["type"] == "train": if t["type"] == "train":
station_list += [t["from_station"], t["to_station"]] station_list += [t["from_station"], t["to_station"]]
for leg in t["legs"]: for leg in t["legs"]:
station_list.append(leg["from_station"]) station_list.append(leg["from_station"])
station_list.append(leg["to_station"]) station_list.append(leg["to_station"])
else: elif t["type"] == "flight":
assert t["type"] == "flight"
for field in "from_airport", "to_airport": for field in "from_airport", "to_airport":
if field in t: if field in t:
airports[t[field]["iata"]] = t[field] airports[t[field]["iata"]] = t[field]
else:
assert t["type"] == "ferry"
for field in "from_terminal", "to_terminal":
terminal = t[field]
ferry_terminals[terminal["name"]] = terminal
for s in station_list: for s in station_list:
if s["uic"] in stations: if s["uic"] in stations:
@ -205,7 +226,12 @@ def collect_trip_coordinates(trip: Trip) -> list[StrDict]:
if "latitude" in item and "longitude" in item if "latitude" in item and "longitude" in item
] ]
for coord_type, coord_dict in ("station", stations), ("airport", airports): locations = [
("station", stations),
("airport", airports),
("ferry_terminals", ferry_terminals),
]
for coord_type, coord_dict in locations:
coords += [ coords += [
{ {
"name": s["name"], "name": s["name"],
@ -234,6 +260,8 @@ def get_trip_routes(trip: Trip) -> list[StrDict]:
routes = [] routes = []
seen_geojson = set() seen_geojson = set()
for t in trip.travel: for t in trip.travel:
if t["type"] == "ferry":
continue
if t["type"] == "flight": if t["type"] == "flight":
if "from_airport" not in t or "to_airport" not in t: if "from_airport" not in t or "to_airport" not in t:
continue continue

View file

@ -226,3 +226,36 @@
{% endif %} {% endif %}
</div> </div>
{% endmacro %} {% endmacro %}
{% macro ferry_row(item) %}
<div class="grid-item text-end">{{ item.depart.strftime("%a, %d %b %Y") }}</div>
<div class="grid-item">
{{ item.from }} &rarr; {{ item.to }}
</div>
<div class="grid-item">{{ item.depart.strftime("%H:%M") }}</div>
<div class="grid-item">
{% if item.arrive %}
{{ item.arrive.strftime("%H:%M") }}
{% if item.depart != item.arrive and item.arrive.date() != item.depart.date() %}+1 day{% endif %}
{% endif %}
</div>
<div class="grid-item"></div>
<div class="grid-item">{{ item.operator }}</div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item text-end">
{% if g.user.is_authenticated and item.price and item.currency %}
<span class="badge bg-info text-nowrap">{{ "{:,f}".format(item.price) }} {{ item.currency }}</span>
{% if item.currency != "GBP" %}
<span class="badge bg-info text-nowrap">{{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP</span>
{% endif %}
{% endif %}
</div>
{# <div class="grid-item">{{ item | pprint }}</div> #}
{% endmacro %}

View file

@ -1,8 +1,8 @@
{% extends "base.html" %} {% extends "base.html" %}
{% from "macros.html" import trip_link, display_date_no_year, display_date, conference_row, accommodation_row, flight_row, train_row with context %} {% from "macros.html" import trip_link, display_date_no_year, display_date, conference_row, accommodation_row, flight_row, train_row, ferry_row with context %}
{% set row = { "flight": flight_row, "train": train_row } %} {% set row = { "flight": flight_row, "train": train_row, "ferry": ferry_row } %}
{% block title %}Trips - Edward Betts{% endblock %} {% block title %}Trips - Edward Betts{% endblock %}