From 259642ff52bd145907f9544232148e4e4fff7791 Mon Sep 17 00:00:00 2001
From: Edward Betts <edward@4angle.com>
Date: Sun, 11 Aug 2024 12:50:58 +0200
Subject: [PATCH] Show current trip on home page

---
 templates/event_list.html | 14 ++++++++++++++
 web_view.py               | 14 ++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/templates/event_list.html b/templates/event_list.html
index b9769b5..a769bed 100644
--- a/templates/event_list.html
+++ b/templates/event_list.html
@@ -39,6 +39,7 @@
 } %}
 
 
+{% from "macros.html" import trip_link, display_date_no_year with context %}
 
 {% from "navbar.html" import navbar with context %}
 <body>
@@ -79,6 +80,19 @@
     <p>{{ market }}</p>
   {% endfor %}
 
+  {% if current_trip %}
+    {% set end = current_trip.end %}
+    <div>
+      <div>Current trip: {{ trip_link(current_trip) }}</div>
+      {% if end %}
+        <div>Dates: {{ display_date_no_year(current_trip.start) }} to {{ display_date_no_year(end) }}</div>
+      {% else %}
+        <div>Start: {{ display_date_no_year(current_trip.start) }} (end date missing)</div>
+      {% endif %}
+
+    </div>
+  {% endif %}
+
   <h3>Agenda</h3>
 
   <div>
diff --git a/web_view.py b/web_view.py
index 96c3457..296c670 100755
--- a/web_view.py
+++ b/web_view.py
@@ -68,6 +68,19 @@ def exception_handler(e: werkzeug.exceptions.InternalServerError) -> tuple[str,
     )
 
 
+def get_current_trip(today: date) -> Trip | None:
+    """Get current trip."""
+    trip_list = get_trip_list(route_distances=None)
+
+    current = [
+        item
+        for item in trip_list
+        if item.start <= today and (item.end or item.start) >= today
+    ]
+    assert len(current) < 2
+    return current[0] if current else None
+
+
 @app.route("/")
 async def index() -> str:
     """Index page."""
@@ -87,6 +100,7 @@ async def index() -> str:
         "event_list.html",
         today=now.date(),
         events=events,
+        current_trip=get_current_trip(now.date()),
         fullcalendar_events=calendar.build_events(events),
         start_event_list=date.today() - timedelta(days=1),
         end_event_list=date.today() + timedelta(days=365 * 2),