From f4557d14e85fb435f8aa84dd3689d03313707eaa Mon Sep 17 00:00:00 2001
From: Edward Betts <edward@4angle.com>
Date: Thu, 20 Jun 2024 12:40:26 +0100
Subject: [PATCH] Show flight prices on individual trip pages

Closes: #153
---
 agenda/trip.py           | 11 +++++++----
 agenda/types.py          |  6 ++++++
 templates/trip_page.html | 31 +++++++++++++++++++++++--------
 3 files changed, 36 insertions(+), 12 deletions(-)

diff --git a/agenda/trip.py b/agenda/trip.py
index 3bdf492..cbb72fc 100644
--- a/agenda/trip.py
+++ b/agenda/trip.py
@@ -107,12 +107,12 @@ def load_flight_bookings(data_dir: str) -> list[StrDict]:
     return bookings
 
 
-def load_flights(data_dir: str) -> list[StrDict]:
+def load_flights(flight_bookings: list[StrDict]) -> list[StrDict]:
     """Load flights."""
     flights = []
-    for booking in load_flight_bookings(data_dir):
+    for booking in flight_bookings:
         for flight in booking["flights"]:
-            for f in "type", "trip", "booking_reference":
+            for f in "type", "trip", "booking_reference", "price", "currency":
                 if f in booking:
                     flight[f] = booking[f]
             flights.append(flight)
@@ -133,14 +133,17 @@ def build_trip_list(
 
     yaml_trip_lookup = {item["trip"]: item for item in yaml_trip_list}
 
+    flight_bookings = load_flight_bookings(data_dir)
+
     travel_items = sorted(
-        load_flights(data_dir)
+        load_flights(flight_bookings)
         + load_trains(data_dir, route_distances=route_distances)
         + load_ferries(data_dir, route_distances=route_distances),
         key=depart_datetime,
     )
 
     data = {
+        "flight_bookings": flight_bookings,
         "travel": travel_items,
         "accommodation": travel.parse_yaml("accommodation", data_dir),
         "conferences": travel.parse_yaml("conferences", data_dir),
diff --git a/agenda/types.py b/agenda/types.py
index d86ae3d..6d18909 100644
--- a/agenda/types.py
+++ b/agenda/types.py
@@ -77,6 +77,7 @@ class Trip:
     accommodation: list[StrDict] = field(default_factory=list)
     conferences: list[StrDict] = field(default_factory=list)
     events: list[StrDict] = field(default_factory=list)
+    flight_bookings: list[StrDict] = field(default_factory=list)
     name: str | None = None
     private: bool = False
 
@@ -207,6 +208,11 @@ class Trip:
             else None
         )
 
+    @property
+    def flights(self) -> list[StrDict]:
+        """Flights."""
+        return [item for item in self.travel if item["type"] == "flight"]
+
     def distances_by_transport_type(self) -> list[tuple[str, float]]:
         """Calculate the total distance travelled for each type of transport.
 
diff --git a/templates/trip_page.html b/templates/trip_page.html
index e1effc6..9bf4d37 100644
--- a/templates/trip_page.html
+++ b/templates/trip_page.html
@@ -114,9 +114,9 @@
             </small>
           </h5>
           <p class="card-text">
-            Topic: {{ item.topic }}
-            | Venue: {{ item.venue }}
-            | Location: {{ item.location }}
+            <strong>Topic:</strong> {{ item.topic }}
+            <strong>Venue:</strong> {{ item.venue }}
+            <strong>Location:</strong>  {{ item.location }}
             {% if country %}
               {{ country.flag }}
             {% elif item.online %}
@@ -127,9 +127,9 @@
               </span>
             {% endif %}
             {% if item.free %}
-              | <span class="badge bg-success text-nowrap">free to attend</span>
+              <span class="badge bg-success text-nowrap">free to attend</span>
             {% elif item.price and item.currency %}
-              | <span class="badge bg-info text-nowrap">price: {{ item.price }} {{ item.currency }}</span>
+              <span class="badge bg-info text-nowrap">price: {{ item.price }} {{ item.currency }}</span>
             {% endif %}
           </p>
         </div>
@@ -150,8 +150,8 @@
             </small>
           </h5>
           <p class="card-text">
-            Address: {{ item.address }}
-            | Location: {{ item.location }}
+            <strong>Address:</strong> {{ item.address }}
+            <strong>Location:</strong> {{ item.location }}
             {% if country %}
               {{ country.flag }}
             {% else %}
@@ -160,13 +160,28 @@
               </span>
             {% endif %}
             {% if g.user.is_authenticated and item.price and item.currency %}
-              | <span class="badge bg-info text-nowrap">price: {{ item.price }} {{ item.currency }}</span>
+              <span class="badge bg-info text-nowrap">price: {{ item.price }} {{ item.currency }}</span>
             {% endif %}
           </p>
         </div>
       </div>
     {% endfor %}
 
+    {% if trip.flight_bookings %}
+    <h3>Flight bookings</h3>
+    {% for item in trip.flight_bookings %}
+      <div>
+        {{ item.flights | map(attribute="airline_name") | unique | join(" + ") }}
+        {% if g.user.is_authenticated and item.booking_reference %}
+          <strong>booking reference:</strong> {{ item.booking_reference }}
+        {% endif %}
+        {% if g.user.is_authenticated and item.price and item.currency %}
+          <span class="badge bg-info text-nowrap">price: {{ item.price }} {{ item.currency }}</span>
+        {% endif %}
+      </div>
+    {% endfor %}
+    {% endif %}
+
     {% for item in trip.events %}
       {% set country = get_country(item.country) if item.country else None %}
       <div class="card my-1">