From f4fc8399260227b8058fee9481893d9ed5f3a6b1 Mon Sep 17 00:00:00 2001
From: Edward Betts <edward@4angle.com>
Date: Wed, 13 Nov 2024 15:52:02 +0100
Subject: [PATCH] Move code around

---
 agenda/holidays.py | 21 ++++++++++++++++++++-
 web_view.py        | 19 ++-----------------
 2 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/agenda/holidays.py b/agenda/holidays.py
index 620b9ab..a93ec5d 100644
--- a/agenda/holidays.py
+++ b/agenda/holidays.py
@@ -3,11 +3,30 @@
 import collections
 from datetime import date, timedelta
 
+import flask
+
 import agenda.uk_holiday
 import holidays
 
 from .event import Event
-from .types import Holiday
+from .types import Holiday, Trip
+
+
+def get_trip_holidays(trip: Trip) -> list[Holiday]:
+    """Get holidays happening during trip."""
+    if not trip.end:
+        return []
+    countries = {c.alpha_2 for c in trip.countries}
+    return sorted(
+        (
+            hol
+            for hol in get_all(
+                trip.start, trip.end, flask.current_app.config["DATA_DIR"]
+            )
+            if hol.country.upper() in countries
+        ),
+        key=lambda item: (item.date, item.country),
+    )
 
 
 def us_holidays(start_date: date, end_date: date) -> list[Holiday]:
diff --git a/web_view.py b/web_view.py
index 7f6f555..c485657 100755
--- a/web_view.py
+++ b/web_view.py
@@ -507,8 +507,6 @@ def trip_page(start: str) -> str:
     if not trip:
         flask.abort(404)
 
-    today = date.today()
-
     coordinates = agenda.trip.collect_trip_coordinates(trip)
     routes = agenda.trip.get_trip_routes(trip, app.config["PERSONAL_DATA"])
 
@@ -520,30 +518,17 @@ def trip_page(start: str) -> str:
                 app.config["PERSONAL_DATA"], route.pop("geojson_filename")
             )
 
-    if trip.end:
-        countries = {c.alpha_2 for c in trip.countries}
-        holidays = [
-            hol
-            for hol in agenda.holidays.get_all(
-                trip.start, trip.end, app.config["DATA_DIR"]
-            )
-            if hol.country.upper() in countries
-        ]
-        holidays.sort(key=lambda item: (item.date, item.country))
-    else:
-        holidays = []
-
     return flask.render_template(
         "trip_page.html",
         trip=trip,
         prev_trip=prev_trip,
         next_trip=next_trip,
-        today=today,
+        today=date.today(),
         coordinates=coordinates,
         routes=routes,
         get_country=agenda.get_country,
         format_list_with_ampersand=format_list_with_ampersand,
-        holidays=holidays,
+        holidays=agenda.holidays.get_trip_holidays(trip),
         human_readable_delta=agenda.utils.human_readable_delta,
     )