Move two functions to agenda/trip.py

This commit is contained in:
Edward Betts 2025-11-03 12:22:27 +00:00
parent f718535624
commit b4126d04f8
4 changed files with 64 additions and 63 deletions

View file

@ -2,8 +2,8 @@
from datetime import date, datetime, timedelta
from .trip import depart_datetime
from .types import SchengenCalculation, SchengenStay, StrDict
from .utils import depart_datetime
# Schengen Area countries as of 2025
SCHENGEN_COUNTRIES = {

View file

@ -3,14 +3,14 @@
import decimal
import os
import typing
from datetime import date, datetime, time
from zoneinfo import ZoneInfo
from datetime import date
import flask
import yaml
from agenda import travel
from agenda import travel, trip_schengen
from agenda.types import StrDict, Trip
from agenda.utils import depart_datetime
class Airline(typing.TypedDict, total=False):
@ -122,18 +122,6 @@ def load_coaches(
return coaches
def depart_datetime(item: StrDict) -> datetime:
"""Return a datetime for this travel item.
If the travel item already has a datetime return that, otherwise if the
departure time is just a date return midnight UTC for that date.
"""
depart = item["depart"]
if isinstance(depart, datetime):
return depart
return datetime.combine(depart, time.min).replace(tzinfo=ZoneInfo("UTC"))
def process_flight(
flight: StrDict, by_iata: dict[str, Airline], airports: list[StrDict]
) -> None:
@ -502,3 +490,33 @@ def get_coordinates_and_routes(
route["geojson"] = read_geojson(data_dir, route.pop("geojson_filename"))
return (coordinates, routes)
def get_trip_list(
route_distances: travel.RouteDistances | None = None,
) -> list[Trip]:
"""Get list of trips respecting current authentication status."""
trips = [
trip
for trip in build_trip_list(route_distances=route_distances)
if flask.g.user.is_authenticated or not trip.private
]
# Add Schengen compliance information to each trip
for trip in trips:
trip_schengen.add_schengen_compliance_to_trip(trip)
return trips
def get_current_trip(today: date) -> Trip | None:
"""Get current trip."""
trip_list = get_trip_list(route_distances=None)
current = [
item
for item in trip_list
if item.start <= today and (item.end or item.start) >= today
]
assert len(current) < 2
return current[0] if current else None

View file

@ -2,8 +2,10 @@
import os
import typing
from datetime import date, datetime, timedelta, timezone
from time import time
from datetime import date, datetime, time, timedelta, timezone
from zoneinfo import ZoneInfo
from .types import StrDict
def as_date(d: datetime | date) -> date:
@ -118,3 +120,15 @@ async def time_function(
exception = e
end_time = time()
return name, result, end_time - start_time, exception
def depart_datetime(item: StrDict) -> datetime:
"""Return a datetime for this travel item.
If the travel item already has a datetime return that, otherwise if the
departure time is just a date return midnight UTC for that date.
"""
depart = item["depart"]
if isinstance(depart, datetime):
return depart
return datetime.combine(depart, time.min).replace(tzinfo=ZoneInfo("UTC"))