Adjust travel page to show flights grouped by booking with booking reference and price

Closes: #152
This commit is contained in:
Edward Betts 2024-04-20 14:17:32 +01:00
parent 19732a3ef1
commit 6d6e416df3
4 changed files with 65 additions and 15 deletions

View file

@ -65,12 +65,11 @@ def depart_datetime(item: StrDict) -> datetime:
return datetime.combine(depart, time.min).replace(tzinfo=ZoneInfo("UTC"))
def load_flights(data_dir: str) -> list[StrDict]:
"""Load flights."""
def load_flight_bookings(data_dir: str) -> list[StrDict]:
"""Load flight bookings."""
bookings = load_travel("flight", data_dir)
airlines = yaml.safe_load(open(os.path.join(data_dir, "airlines.yaml")))
airports = travel.parse_yaml("airports", data_dir)
flights = []
for booking in bookings:
for flight in booking["flights"]:
if flight["from"] in airports:
@ -81,10 +80,17 @@ def load_flights(data_dir: str) -> list[StrDict]:
flight["airline_name"] = airlines.get(flight["airline"], "[unknown]")
flight["distance"] = travel.flight_distance(flight)
flight["type"] = booking["type"]
flight["trip"] = booking["trip"]
if "booking_reference" in booking:
flight["booking_reference"] = booking["booking_reference"]
return bookings
def load_flights(data_dir: str) -> list[StrDict]:
"""Load flights."""
flights = []
for booking in load_flight_bookings(data_dir):
for flight in booking["flights"]:
for f in "type", "trip", "booking_reference":
if f in booking:
flight[f] = booking[f]
flights.append(flight)
return flights

View file

@ -90,6 +90,49 @@
</div>
{% endmacro %}
{% macro flight_booking_row(booking) %}
<div class="grid-item">{{ booking.booking_reference or "reference missing" }}</div>
<div class="grid-item text-end">
{% if g.user.is_authenticated and booking.price and booking.currency %}
<span class="badge bg-info text-nowrap">{{ "{:,f}".format(booking.price) }} {{ booking.currency }}</span>
{% if booking.currency != "GBP" %}
<span class="badge bg-info text-nowrap">{{ "{:,.2f}".format(booking.price / fx_rate[booking.currency]) }} GBP</span>
{% endif %}
{% endif %}
</div>
{% for i in range(8) %}
<div class="grid-item"></div>
{% endfor %}
{% for item in booking.flights %}
{% set full_flight_number = item.airline + item.flight_number %}
{% set radarbox_url = "https://www.radarbox.com/data/flights/" + full_flight_number %}
<div class="grid-item"></div>
<div class="grid-item"></div>
<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.arrive.date() != item.depart.date() %}+1 day{% endif %}
{% endif %}
</div>
<div class="grid-item">{{ item.duration }}</div>
<div class="grid-item">{{ full_flight_number }}</div>
<div class="grid-item">
<a href="https://www.flightradar24.com/data/flights/{{ full_flight_number | lower }}">flightradar24</a>
| <a href="https://uk.flightaware.com/live/flight/{{ full_flight_number | replace("U2", "EZY") }}">FlightAware</a>
| <a href="{{ radarbox_url }}">radarbox</a>
</div>
<div class="grid-item text-end">
{% if item.distance %}
{{ "{:,.0f} km / {:,.0f} miles".format(item.distance, item.distance / 1.60934) }}
{% endif %}
</div>
{% endfor %}
{% endmacro %}
{% macro flight_row(item) %}
{% set full_flight_number = item.airline + item.flight_number %}
{% set radarbox_url = "https://www.radarbox.com/data/flights/" + full_flight_number %}

View file

@ -1,15 +1,16 @@
{% extends "base.html" %}
{% from "macros.html" import flight_row, train_row with context %}
{% from "macros.html" import flight_booking_row, train_row with context %}
{% block title %}Travel - Edward Betts{% endblock %}
{% set flight_column_count = 10 %}
{% set column_count = 10 %}
{% block style %}
<style>
.grid-container {
display: grid;
grid-template-columns: repeat({{ column_count }}, auto);
grid-template-columns: repeat({{ flight_column_count }}, auto);
gap: 10px;
justify-content: start;
}
@ -36,19 +37,19 @@
<h3>flights</h3>
<div class="grid-container">
<div class="grid-item">reference</div>
<div class="grid-item">price</div>
<div class="grid-item text-end">date</div>
<div class="grid-item">route</div>
<div class="grid-item">take-off</div>
<div class="grid-item">land</div>
<div class="grid-item">duration</div>
<div class="grid-item">flight</div>
<div class="grid-item">reference</div>
<div class="grid-item">tracking</div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item">distance</div>
{% for item in flights | sort(attribute="depart") %}
{{ flight_row(item) }}
{% for item in flights %}
{{ flight_booking_row(item) }}
{% endfor %}
</div>

View file

@ -111,7 +111,7 @@ async def weekends() -> str:
def travel_list() -> str:
"""Page showing a list of upcoming travel."""
data_dir = app.config["PERSONAL_DATA"]
flights = agenda.trip.load_flights(data_dir)
flights = agenda.trip.load_flight_bookings(data_dir)
trains = [
item
for item in travel.parse_yaml("trains", data_dir)