Fix location tracking for trips ending in UK

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 <noreply@anthropic.com>
This commit is contained in:
Edward Betts 2025-07-16 04:18:56 +02:00
parent a92debea5b
commit 6533165bef
2 changed files with 21 additions and 2 deletions

View file

@ -260,6 +260,20 @@ def get_location_for_date(
get_country(acc.get("country", "gb")), 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 # Return most recent location or default to Bristol
if most_recent_location: if most_recent_location:
return most_recent_location return most_recent_location

View file

@ -13,9 +13,9 @@ def test_get_location_for_date() -> None:
trips = [ trips = [
t t
for t in agenda.trip.build_trip_list() 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"] 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 date(2023, 12, 2), trips, bookings, accommodations, airports
) )
assert l2[0] == "Bristol" assert l2[0] == "Bristol"
l2 = agenda.busy.get_location_for_date(
date(2023, 10, 7), trips, bookings, accommodations, airports
)
assert l2[0] == "Bristol"