From 6533165befcd69a9d4ebfc634c3f56d5284ce102 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Wed, 16 Jul 2025 04:18:56 +0200 Subject: [PATCH] Fix location tracking for trips ending in UK MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a trip ended in the UK but had foreign accommodations during the trip, location tracking would incorrectly show the foreign location even after returning home. Added logic to detect when the most recent travel occurred within a completed trip that ended in the UK, and override foreign accommodations to show "Bristol" (home) for dates after the trip. Example: Trip Sep 8-17 with accommodation in Kochi, India on Sep 10, but trip ended at Gatwick Airport, UK. Oct 7 now correctly shows "Bristol" instead of "Kochi". 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- agenda/busy.py | 14 ++++++++++++++ tests/test_busy.py | 9 +++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/agenda/busy.py b/agenda/busy.py index 2def1de..3f38d40 100644 --- a/agenda/busy.py +++ b/agenda/busy.py @@ -260,6 +260,20 @@ def get_location_for_date( get_country(acc.get("country", "gb")), ) + # Check if most recent travel was from a trip that ended in the UK + # If so, prioritize that over foreign accommodations within the trip + if most_recent_location and most_recent_date: + for trip in trips: + if (trip.end and trip.end < target_date and + trip.start <= most_recent_date <= trip.end): + # The most recent travel was within a trip that has since ended + locations = trip.locations() + if locations: + final_city, final_country = locations[-1] + # If trip ended in UK, you should be home now + if hasattr(final_country, 'alpha_2') and final_country.alpha_2 == 'GB': + return ("Bristol", get_country("gb")) + # Return most recent location or default to Bristol if most_recent_location: return most_recent_location diff --git a/tests/test_busy.py b/tests/test_busy.py index fd8e9a1..93c3349 100644 --- a/tests/test_busy.py +++ b/tests/test_busy.py @@ -13,9 +13,9 @@ def test_get_location_for_date() -> None: trips = [ t for t in agenda.trip.build_trip_list() - if t.start in [date(2023, 11, 14), date(2025, 2, 9)] + if t.start in [date(2023, 9, 8), date(2023, 11, 14), date(2025, 2, 9)] ] - assert len(trips) == 2 + assert len(trips) == 3 data_dir = app.config["PERSONAL_DATA"] @@ -38,3 +38,8 @@ def test_get_location_for_date() -> None: date(2023, 12, 2), trips, bookings, accommodations, airports ) assert l2[0] == "Bristol" + + l2 = agenda.busy.get_location_for_date( + date(2023, 10, 7), trips, bookings, accommodations, airports + ) + assert l2[0] == "Bristol"