Use None in place of "Bristol" for home
Also fix some more location bugs.
This commit is contained in:
parent
eb65f720bf
commit
02350ef4db
|
@ -87,7 +87,7 @@ def get_location_for_date(
|
|||
bookings: list[dict],
|
||||
accommodations: list[dict],
|
||||
airports: dict,
|
||||
) -> tuple[str, pycountry.db.Country | None]:
|
||||
) -> tuple[str | None, pycountry.db.Country | None]:
|
||||
"""Get location (city, country) for a specific date using travel history."""
|
||||
# UK airports that indicate being home
|
||||
uk_airports = {"LHR", "LGW", "STN", "LTN", "BRS", "BHX", "MAN", "EDI", "GLA"}
|
||||
|
@ -135,10 +135,22 @@ def get_location_for_date(
|
|||
destination_airport = flight["to"]
|
||||
|
||||
if destination_airport in uk_airports:
|
||||
trip_most_recent_location = (
|
||||
"Bristol",
|
||||
get_country("gb"),
|
||||
)
|
||||
# When on a trip, show the actual location even for UK airports
|
||||
airport_info = airports.get(destination_airport)
|
||||
if airport_info:
|
||||
location_name = airport_info.get(
|
||||
"city",
|
||||
airport_info.get("name", "London"),
|
||||
)
|
||||
trip_most_recent_location = (
|
||||
location_name,
|
||||
get_country("gb"),
|
||||
)
|
||||
else:
|
||||
trip_most_recent_location = (
|
||||
"London",
|
||||
get_country("gb"),
|
||||
)
|
||||
else:
|
||||
airport_info = airports.get(destination_airport)
|
||||
if airport_info:
|
||||
|
@ -177,8 +189,9 @@ def get_location_for_date(
|
|||
):
|
||||
trip_most_recent_date = acc_date
|
||||
if acc.get("country") == "gb":
|
||||
# When on a trip, show the actual location even for UK accommodations
|
||||
trip_most_recent_location = (
|
||||
"Bristol",
|
||||
acc.get("location", "London"),
|
||||
get_country("gb"),
|
||||
)
|
||||
else:
|
||||
|
@ -187,14 +200,28 @@ def get_location_for_date(
|
|||
get_country(acc.get("country", "gb")),
|
||||
)
|
||||
|
||||
# Return the most recent location within the trip, or fallback to first trip location
|
||||
# Return the most recent location within the trip, or fallback to trip location by date
|
||||
if trip_most_recent_location:
|
||||
return trip_most_recent_location
|
||||
|
||||
# Fallback to first location if no specific location found
|
||||
# Fallback: determine location based on trip progression and date
|
||||
locations = trip.locations()
|
||||
if locations:
|
||||
city, country = locations[0]
|
||||
# If only one location, use it (when on a trip, always show the location)
|
||||
if len(locations) == 1:
|
||||
city, country = locations[0]
|
||||
return (city, country)
|
||||
|
||||
# Multiple locations: use progression through the trip
|
||||
trip_duration = (trip.end - trip.start).days + 1
|
||||
days_into_trip = (target_date - trip.start).days
|
||||
|
||||
# Simple progression: first half at first location, second half at last location
|
||||
if days_into_trip <= trip_duration // 2:
|
||||
city, country = locations[0]
|
||||
else:
|
||||
city, country = locations[-1]
|
||||
|
||||
return (city, country)
|
||||
|
||||
# Find most recent flight or accommodation before this date
|
||||
|
@ -236,7 +263,7 @@ def get_location_for_date(
|
|||
|
||||
# If arriving at UK airport, assume back home in Bristol
|
||||
if destination_airport in uk_airports:
|
||||
most_recent_location = ("Bristol", get_country("gb"))
|
||||
most_recent_location = (None, get_country("gb"))
|
||||
else:
|
||||
# Get destination airport location for non-UK arrivals
|
||||
airport_info = airports.get(destination_airport)
|
||||
|
@ -267,7 +294,7 @@ def get_location_for_date(
|
|||
most_recent_date = acc_date
|
||||
# For UK accommodation, use Bristol as location
|
||||
if acc.get("country") == "gb":
|
||||
most_recent_location = ("Bristol", get_country("gb"))
|
||||
most_recent_location = (None, get_country("gb"))
|
||||
else:
|
||||
most_recent_location = (
|
||||
acc.get("location", "Unknown"),
|
||||
|
@ -285,7 +312,7 @@ def get_location_for_date(
|
|||
|
||||
# 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 (None, get_country("gb"))
|
||||
|
||||
# For short trips to nearby countries or international trips
|
||||
# (ended >=1 day ago), assume returned home if no subsequent travel data
|
||||
|
@ -316,13 +343,13 @@ def get_location_for_date(
|
|||
in {"US", "CA", "IN", "JP", "CN", "AU", "NZ", "BR", "AR", "ZA"}
|
||||
)
|
||||
):
|
||||
return ("Bristol", get_country("gb"))
|
||||
return (None, get_country("gb"))
|
||||
|
||||
# Return most recent location or default to Bristol
|
||||
if most_recent_location:
|
||||
return most_recent_location
|
||||
|
||||
return ("Bristol", get_country("gb"))
|
||||
return (None, get_country("gb"))
|
||||
|
||||
|
||||
def weekends(
|
||||
|
|
|
@ -47,9 +47,7 @@
|
|||
</td>
|
||||
{% if extra_class %}<td class="{{ extra_class|trim }}">{% else %}<td>{% endif %}
|
||||
{% set city, country = weekend[day + '_location'] %}
|
||||
{% if city == "Bristol" and country.alpha_2 | upper == "GB" %}
|
||||
{# <strong>home</strong> #}
|
||||
{% else %}
|
||||
{% if city %}
|
||||
{{ city }}, {{ country.flag }} {{ country.name }}
|
||||
{% endif %}
|
||||
</td>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from datetime import date, datetime
|
||||
from datetime import date, datetime, timedelta
|
||||
|
||||
import agenda.busy
|
||||
import agenda.travel as travel
|
||||
|
@ -26,54 +26,43 @@ def test_get_location_for_date() -> None:
|
|||
|
||||
for weekend in weekends:
|
||||
for day in "saturday", "sunday":
|
||||
assert weekend[day + "_location"][0] == "Bristol" or bool(
|
||||
weekend[day]
|
||||
)
|
||||
# When free (no events), should be home (None)
|
||||
# When traveling (events), should be away (City name)
|
||||
assert bool(weekend[day + "_location"][0]) == bool(weekend[day])
|
||||
|
||||
# Debug the April 29 issue
|
||||
# Test some specific cases
|
||||
april_29_location = agenda.busy.get_location_for_date(
|
||||
date(2023, 4, 29), trips, bookings, accommodations, airports
|
||||
)
|
||||
print(f"\nDirect call for April 29: {april_29_location}")
|
||||
assert not april_29_location[0] # Should be home (None)
|
||||
|
||||
# Check what the foss-north trip looks like
|
||||
foss_north_trip = None
|
||||
for trip in trips:
|
||||
if trip.title == "foss-north" and trip.start == date(2023, 4, 22):
|
||||
foss_north_trip = trip
|
||||
print(f"foss-north trip: {trip.start} to {trip.end}")
|
||||
print(f"foss-north locations: {trip.locations()}")
|
||||
break
|
||||
|
||||
l1 = agenda.busy.get_location_for_date(
|
||||
l = agenda.busy.get_location_for_date(
|
||||
date(2025, 2, 15), trips, bookings, accommodations, airports
|
||||
)
|
||||
assert l1[0] == "Hackettstown"
|
||||
assert l[0] == "Hackettstown"
|
||||
|
||||
l2 = agenda.busy.get_location_for_date(
|
||||
l = agenda.busy.get_location_for_date(
|
||||
date(2025, 7, 1), trips, bookings, accommodations, airports
|
||||
)
|
||||
assert l2[0] == "Bristol"
|
||||
assert not l[0]
|
||||
|
||||
l2 = agenda.busy.get_location_for_date(
|
||||
l = agenda.busy.get_location_for_date(
|
||||
date(2023, 12, 2), trips, bookings, accommodations, airports
|
||||
)
|
||||
assert l2[0] == "Bristol"
|
||||
assert not l[0]
|
||||
|
||||
l2 = agenda.busy.get_location_for_date(
|
||||
l = agenda.busy.get_location_for_date(
|
||||
date(2023, 10, 7), trips, bookings, accommodations, airports
|
||||
)
|
||||
assert l2[0] == "Bristol"
|
||||
assert not l[0]
|
||||
|
||||
l2 = agenda.busy.get_location_for_date(
|
||||
l = agenda.busy.get_location_for_date(
|
||||
date(2023, 2, 18), trips, bookings, accommodations, airports
|
||||
)
|
||||
assert l2[0] == "Bristol"
|
||||
assert not l[0]
|
||||
|
||||
l2 = agenda.busy.get_location_for_date(
|
||||
l = agenda.busy.get_location_for_date(
|
||||
date(2025, 8, 2), trips, bookings, accommodations, airports
|
||||
)
|
||||
assert l2[0] == "Bristol"
|
||||
assert not l[0]
|
||||
|
||||
# Fix the April 29 case
|
||||
assert april_29_location[0] == "Bristol"
|
||||
|
|
Loading…
Reference in a new issue