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],
|
||||
accommodations: list[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."""
|
||||
# UK airports that indicate being home
|
||||
uk_airports = {"LHR", "LGW", "STN", "LTN", "BRS", "BHX", "MAN", "EDI", "GLA"}
|
||||
|
|
@ -135,22 +135,10 @@ def get_location_for_date(
|
|||
destination_airport = flight["to"]
|
||||
|
||||
if destination_airport in uk_airports:
|
||||
# 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"),
|
||||
)
|
||||
trip_most_recent_location = (
|
||||
"Bristol",
|
||||
get_country("gb"),
|
||||
)
|
||||
else:
|
||||
airport_info = airports.get(destination_airport)
|
||||
if airport_info:
|
||||
|
|
@ -189,9 +177,8 @@ 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 = (
|
||||
acc.get("location", "London"),
|
||||
"Bristol",
|
||||
get_country("gb"),
|
||||
)
|
||||
else:
|
||||
|
|
@ -200,28 +187,14 @@ def get_location_for_date(
|
|||
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:
|
||||
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()
|
||||
if locations:
|
||||
# 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]
|
||||
|
||||
city, country = locations[0]
|
||||
return (city, country)
|
||||
|
||||
# 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 destination_airport in uk_airports:
|
||||
most_recent_location = (None, get_country("gb"))
|
||||
most_recent_location = ("Bristol", get_country("gb"))
|
||||
else:
|
||||
# Get destination airport location for non-UK arrivals
|
||||
airport_info = airports.get(destination_airport)
|
||||
|
|
@ -294,7 +267,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 = (None, get_country("gb"))
|
||||
most_recent_location = ("Bristol", get_country("gb"))
|
||||
else:
|
||||
most_recent_location = (
|
||||
acc.get("location", "Unknown"),
|
||||
|
|
@ -311,8 +284,11 @@ def get_location_for_date(
|
|||
days_since_trip = (target_date - trip.end).days
|
||||
|
||||
# If trip ended in UK, you should be home now
|
||||
if hasattr(final_country, "alpha_2") and final_country.alpha_2 == "GB":
|
||||
return (None, get_country("gb"))
|
||||
if (
|
||||
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
|
||||
# (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 (
|
||||
# European countries (close by rail/ferry)
|
||||
final_country.alpha_2
|
||||
in {"BE", "NL", "FR", "DE", "CH", "AT", "IT", "ES"}
|
||||
final_country.alpha_2 in {"BE", "NL", "FR", "DE", "CH", "AT", "IT", "ES"}
|
||||
# Nearby Balkan countries
|
||||
or final_country.alpha_2
|
||||
in {
|
||||
"GR",
|
||||
"AL",
|
||||
"XK",
|
||||
"HR",
|
||||
"SI",
|
||||
"MK",
|
||||
"BA",
|
||||
"ME",
|
||||
"RS",
|
||||
"BG",
|
||||
"RO",
|
||||
}
|
||||
or final_country.alpha_2 in {"GR", "AL", "XK", "HR", "SI", "MK", "BA", "ME", "RS", "BG", "RO"}
|
||||
# International trips (assume return home after trip ends)
|
||||
or final_country.alpha_2
|
||||
in {"US", "CA", "IN", "JP", "CN", "AU", "NZ", "BR", "AR", "ZA"}
|
||||
or final_country.alpha_2 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
|
||||
if most_recent_location:
|
||||
return most_recent_location
|
||||
|
||||
return (None, get_country("gb"))
|
||||
return ("Bristol", get_country("gb"))
|
||||
|
||||
|
||||
def weekends(
|
||||
|
|
|
|||
|
|
@ -47,7 +47,9 @@
|
|||
</td>
|
||||
{% if extra_class %}<td class="{{ extra_class|trim }}">{% else %}<td>{% endif %}
|
||||
{% 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 }}
|
||||
{% endif %}
|
||||
</td>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from datetime import date, datetime, timedelta
|
||||
from datetime import date, datetime
|
||||
|
||||
import agenda.busy
|
||||
import agenda.travel as travel
|
||||
|
|
@ -26,43 +26,54 @@ def test_get_location_for_date() -> None:
|
|||
|
||||
for weekend in weekends:
|
||||
for day in "saturday", "sunday":
|
||||
# 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])
|
||||
assert weekend[day + "_location"][0] == "Bristol" or bool(
|
||||
weekend[day]
|
||||
)
|
||||
|
||||
# Test some specific cases
|
||||
# Debug the April 29 issue
|
||||
april_29_location = agenda.busy.get_location_for_date(
|
||||
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
|
||||
)
|
||||
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
|
||||
)
|
||||
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
|
||||
)
|
||||
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
|
||||
)
|
||||
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
|
||||
)
|
||||
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
|
||||
)
|
||||
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