Compare commits
No commits in common. "02350ef4db94e81fcd6ebbcd73ce7423c2cda4d2" and "3ad11b070ab586925426863e4245cb670180a43f" have entirely different histories.
02350ef4db
...
3ad11b070a
3 changed files with 54 additions and 80 deletions
|
|
@ -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 | None, pycountry.db.Country | None]:
|
) -> tuple[str, 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,22 +135,10 @@ 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:
|
||||||
# When on a trip, show the actual location even for UK airports
|
trip_most_recent_location = (
|
||||||
airport_info = airports.get(destination_airport)
|
"Bristol",
|
||||||
if airport_info:
|
get_country("gb"),
|
||||||
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:
|
||||||
|
|
@ -189,9 +177,8 @@ 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 = (
|
||||||
acc.get("location", "London"),
|
"Bristol",
|
||||||
get_country("gb"),
|
get_country("gb"),
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
|
@ -200,28 +187,14 @@ 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 trip location by date
|
# Return the most recent location within the trip, or fallback to first trip location
|
||||||
if trip_most_recent_location:
|
if trip_most_recent_location:
|
||||||
return trip_most_recent_location
|
return trip_most_recent_location
|
||||||
|
|
||||||
# Fallback: determine location based on trip progression and date
|
# Fallback to first location if no specific location found
|
||||||
locations = trip.locations()
|
locations = trip.locations()
|
||||||
if locations:
|
if locations:
|
||||||
# If only one location, use it (when on a trip, always show the location)
|
city, country = locations[0]
|
||||||
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
|
||||||
|
|
@ -263,7 +236,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 = (None, get_country("gb"))
|
most_recent_location = ("Bristol", 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)
|
||||||
|
|
@ -294,7 +267,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 = (None, get_country("gb"))
|
most_recent_location = ("Bristol", get_country("gb"))
|
||||||
else:
|
else:
|
||||||
most_recent_location = (
|
most_recent_location = (
|
||||||
acc.get("location", "Unknown"),
|
acc.get("location", "Unknown"),
|
||||||
|
|
@ -309,10 +282,13 @@ def get_location_for_date(
|
||||||
if locations:
|
if locations:
|
||||||
final_city, final_country = locations[-1]
|
final_city, final_country = locations[-1]
|
||||||
days_since_trip = (target_date - trip.end).days
|
days_since_trip = (target_date - trip.end).days
|
||||||
|
|
||||||
# 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 (
|
||||||
return (None, get_country("gb"))
|
hasattr(final_country, "alpha_2")
|
||||||
|
and final_country.alpha_2 == "GB"
|
||||||
|
):
|
||||||
|
return ("Bristol", 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
|
||||||
|
|
@ -321,35 +297,20 @@ def get_location_for_date(
|
||||||
and hasattr(final_country, "alpha_2")
|
and hasattr(final_country, "alpha_2")
|
||||||
and (
|
and (
|
||||||
# European countries (close by rail/ferry)
|
# European countries (close by rail/ferry)
|
||||||
final_country.alpha_2
|
final_country.alpha_2 in {"BE", "NL", "FR", "DE", "CH", "AT", "IT", "ES"}
|
||||||
in {"BE", "NL", "FR", "DE", "CH", "AT", "IT", "ES"}
|
|
||||||
# Nearby Balkan countries
|
# Nearby Balkan countries
|
||||||
or final_country.alpha_2
|
or final_country.alpha_2 in {"GR", "AL", "XK", "HR", "SI", "MK", "BA", "ME", "RS", "BG", "RO"}
|
||||||
in {
|
|
||||||
"GR",
|
|
||||||
"AL",
|
|
||||||
"XK",
|
|
||||||
"HR",
|
|
||||||
"SI",
|
|
||||||
"MK",
|
|
||||||
"BA",
|
|
||||||
"ME",
|
|
||||||
"RS",
|
|
||||||
"BG",
|
|
||||||
"RO",
|
|
||||||
}
|
|
||||||
# International trips (assume return home after trip ends)
|
# International trips (assume return home after trip ends)
|
||||||
or final_country.alpha_2
|
or final_country.alpha_2 in {"US", "CA", "IN", "JP", "CN", "AU", "NZ", "BR", "AR", "ZA"}
|
||||||
in {"US", "CA", "IN", "JP", "CN", "AU", "NZ", "BR", "AR", "ZA"}
|
|
||||||
)
|
)
|
||||||
):
|
):
|
||||||
return (None, get_country("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
|
||||||
|
|
||||||
return (None, get_country("gb"))
|
return ("Bristol", get_country("gb"))
|
||||||
|
|
||||||
|
|
||||||
def weekends(
|
def weekends(
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,9 @@
|
||||||
</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 %}
|
{% if city == "Bristol" and country.alpha_2 | upper == "GB" %}
|
||||||
|
{# <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, timedelta
|
from datetime import date, datetime
|
||||||
|
|
||||||
import agenda.busy
|
import agenda.busy
|
||||||
import agenda.travel as travel
|
import agenda.travel as travel
|
||||||
|
|
@ -26,43 +26,54 @@ 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":
|
||||||
# When free (no events), should be home (None)
|
assert weekend[day + "_location"][0] == "Bristol" or bool(
|
||||||
# When traveling (events), should be away (City name)
|
weekend[day]
|
||||||
assert bool(weekend[day + "_location"][0]) == bool(weekend[day])
|
)
|
||||||
|
|
||||||
# Test some specific cases
|
# Debug the April 29 issue
|
||||||
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
|
||||||
)
|
)
|
||||||
assert not april_29_location[0] # Should be home (None)
|
print(f"\nDirect call for April 29: {april_29_location}")
|
||||||
|
|
||||||
l = agenda.busy.get_location_for_date(
|
# 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(
|
||||||
date(2025, 2, 15), trips, bookings, accommodations, airports
|
date(2025, 2, 15), trips, bookings, accommodations, airports
|
||||||
)
|
)
|
||||||
assert l[0] == "Hackettstown"
|
assert l1[0] == "Hackettstown"
|
||||||
|
|
||||||
l = agenda.busy.get_location_for_date(
|
l2 = agenda.busy.get_location_for_date(
|
||||||
date(2025, 7, 1), trips, bookings, accommodations, airports
|
date(2025, 7, 1), trips, bookings, accommodations, airports
|
||||||
)
|
)
|
||||||
assert not l[0]
|
assert l2[0] == "Bristol"
|
||||||
|
|
||||||
l = agenda.busy.get_location_for_date(
|
l2 = agenda.busy.get_location_for_date(
|
||||||
date(2023, 12, 2), trips, bookings, accommodations, airports
|
date(2023, 12, 2), trips, bookings, accommodations, airports
|
||||||
)
|
)
|
||||||
assert not l[0]
|
assert l2[0] == "Bristol"
|
||||||
|
|
||||||
l = agenda.busy.get_location_for_date(
|
l2 = agenda.busy.get_location_for_date(
|
||||||
date(2023, 10, 7), trips, bookings, accommodations, airports
|
date(2023, 10, 7), trips, bookings, accommodations, airports
|
||||||
)
|
)
|
||||||
assert not l[0]
|
assert l2[0] == "Bristol"
|
||||||
|
|
||||||
l = agenda.busy.get_location_for_date(
|
l2 = agenda.busy.get_location_for_date(
|
||||||
date(2023, 2, 18), trips, bookings, accommodations, airports
|
date(2023, 2, 18), trips, bookings, accommodations, airports
|
||||||
)
|
)
|
||||||
assert not l[0]
|
assert l2[0] == "Bristol"
|
||||||
|
|
||||||
l = agenda.busy.get_location_for_date(
|
l2 = agenda.busy.get_location_for_date(
|
||||||
date(2025, 8, 2), trips, bookings, accommodations, airports
|
date(2025, 8, 2), trips, bookings, accommodations, airports
|
||||||
)
|
)
|
||||||
assert not l[0]
|
assert l2[0] == "Bristol"
|
||||||
|
|
||||||
|
# Fix the April 29 case
|
||||||
|
assert april_29_location[0] == "Bristol"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue