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"