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],
|
bookings: list[dict],
|
||||||
accommodations: list[dict],
|
accommodations: list[dict],
|
||||||
airports: 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."""
|
"""Get location (city, country) for a specific date using travel history."""
|
||||||
# UK airports that indicate being home
|
# UK airports that indicate being home
|
||||||
uk_airports = {"LHR", "LGW", "STN", "LTN", "BRS", "BHX", "MAN", "EDI", "GLA"}
|
uk_airports = {"LHR", "LGW", "STN", "LTN", "BRS", "BHX", "MAN", "EDI", "GLA"}
|
||||||
|
@ -135,10 +135,22 @@ def get_location_for_date(
|
||||||
destination_airport = flight["to"]
|
destination_airport = flight["to"]
|
||||||
|
|
||||||
if destination_airport in uk_airports:
|
if destination_airport in uk_airports:
|
||||||
trip_most_recent_location = (
|
# When on a trip, show the actual location even for UK airports
|
||||||
"Bristol",
|
airport_info = airports.get(destination_airport)
|
||||||
get_country("gb"),
|
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:
|
else:
|
||||||
airport_info = airports.get(destination_airport)
|
airport_info = airports.get(destination_airport)
|
||||||
if airport_info:
|
if airport_info:
|
||||||
|
@ -177,8 +189,9 @@ def get_location_for_date(
|
||||||
):
|
):
|
||||||
trip_most_recent_date = acc_date
|
trip_most_recent_date = acc_date
|
||||||
if acc.get("country") == "gb":
|
if acc.get("country") == "gb":
|
||||||
|
# When on a trip, show the actual location even for UK accommodations
|
||||||
trip_most_recent_location = (
|
trip_most_recent_location = (
|
||||||
"Bristol",
|
acc.get("location", "London"),
|
||||||
get_country("gb"),
|
get_country("gb"),
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
@ -187,14 +200,28 @@ def get_location_for_date(
|
||||||
get_country(acc.get("country", "gb")),
|
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:
|
if trip_most_recent_location:
|
||||||
return 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()
|
locations = trip.locations()
|
||||||
if 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)
|
return (city, country)
|
||||||
|
|
||||||
# Find most recent flight or accommodation before this date
|
# 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 arriving at UK airport, assume back home in Bristol
|
||||||
if destination_airport in uk_airports:
|
if destination_airport in uk_airports:
|
||||||
most_recent_location = ("Bristol", get_country("gb"))
|
most_recent_location = (None, get_country("gb"))
|
||||||
else:
|
else:
|
||||||
# Get destination airport location for non-UK arrivals
|
# Get destination airport location for non-UK arrivals
|
||||||
airport_info = airports.get(destination_airport)
|
airport_info = airports.get(destination_airport)
|
||||||
|
@ -267,7 +294,7 @@ def get_location_for_date(
|
||||||
most_recent_date = acc_date
|
most_recent_date = acc_date
|
||||||
# For UK accommodation, use Bristol as location
|
# For UK accommodation, use Bristol as location
|
||||||
if acc.get("country") == "gb":
|
if acc.get("country") == "gb":
|
||||||
most_recent_location = ("Bristol", get_country("gb"))
|
most_recent_location = (None, get_country("gb"))
|
||||||
else:
|
else:
|
||||||
most_recent_location = (
|
most_recent_location = (
|
||||||
acc.get("location", "Unknown"),
|
acc.get("location", "Unknown"),
|
||||||
|
@ -285,7 +312,7 @@ def get_location_for_date(
|
||||||
|
|
||||||
# If trip ended in UK, you should be home now
|
# If trip ended in UK, you should be home now
|
||||||
if hasattr(final_country, "alpha_2") and final_country.alpha_2 == "GB":
|
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
|
# For short trips to nearby countries or international trips
|
||||||
# (ended >=1 day ago), assume returned home if no subsequent travel data
|
# (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"}
|
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
|
# Return most recent location or default to Bristol
|
||||||
if most_recent_location:
|
if most_recent_location:
|
||||||
return most_recent_location
|
return most_recent_location
|
||||||
|
|
||||||
return ("Bristol", get_country("gb"))
|
return (None, get_country("gb"))
|
||||||
|
|
||||||
|
|
||||||
def weekends(
|
def weekends(
|
||||||
|
|
|
@ -47,9 +47,7 @@
|
||||||
</td>
|
</td>
|
||||||
{% if extra_class %}<td class="{{ extra_class|trim }}">{% else %}<td>{% endif %}
|
{% if extra_class %}<td class="{{ extra_class|trim }}">{% else %}<td>{% endif %}
|
||||||
{% set city, country = weekend[day + '_location'] %}
|
{% set city, country = weekend[day + '_location'] %}
|
||||||
{% if city == "Bristol" and country.alpha_2 | upper == "GB" %}
|
{% if city %}
|
||||||
{# <strong>home</strong> #}
|
|
||||||
{% else %}
|
|
||||||
{{ city }}, {{ country.flag }} {{ country.name }}
|
{{ city }}, {{ country.flag }} {{ country.name }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from datetime import date, datetime
|
from datetime import date, datetime, timedelta
|
||||||
|
|
||||||
import agenda.busy
|
import agenda.busy
|
||||||
import agenda.travel as travel
|
import agenda.travel as travel
|
||||||
|
@ -26,54 +26,43 @@ def test_get_location_for_date() -> None:
|
||||||
|
|
||||||
for weekend in weekends:
|
for weekend in weekends:
|
||||||
for day in "saturday", "sunday":
|
for day in "saturday", "sunday":
|
||||||
assert weekend[day + "_location"][0] == "Bristol" or bool(
|
# When free (no events), should be home (None)
|
||||||
weekend[day]
|
# 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(
|
april_29_location = agenda.busy.get_location_for_date(
|
||||||
date(2023, 4, 29), trips, bookings, accommodations, airports
|
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
|
l = agenda.busy.get_location_for_date(
|
||||||
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(
|
|
||||||
date(2025, 2, 15), trips, bookings, accommodations, airports
|
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
|
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
|
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
|
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
|
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
|
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