Move code around a bit
This commit is contained in:
parent
b1507702cf
commit
ab60721e15
|
@ -1,6 +1,7 @@
|
|||
"""Trips."""
|
||||
|
||||
import os
|
||||
import typing
|
||||
from datetime import date, datetime, time
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
|
@ -121,6 +122,29 @@ def build_trip_list(
|
|||
return [trip for _, trip in sorted(trips.items())]
|
||||
|
||||
|
||||
def add_coordinates_for_unbooked_flights(
|
||||
routes: list[StrDict], coordinates: list[StrDict]
|
||||
) -> None:
|
||||
"""Add coordinates for flights that haven't been booked yet."""
|
||||
if not (
|
||||
any(route["type"] == "unbooked_flight" for route in routes)
|
||||
and not any(pin["type"] == "airport" for pin in coordinates)
|
||||
):
|
||||
return
|
||||
|
||||
data_dir = flask.current_app.config["PERSONAL_DATA"]
|
||||
airports = typing.cast(dict[str, StrDict], travel.parse_yaml("airports", data_dir))
|
||||
lhr = airports["LHR"]
|
||||
coordinates.append(
|
||||
{
|
||||
"name": lhr["name"],
|
||||
"type": "airport",
|
||||
"latitude": lhr["latitude"],
|
||||
"longitude": lhr["longitude"],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def collect_trip_coordinates(trip: Trip) -> list[StrDict]:
|
||||
"""Extract and deduplicate airport and station coordinates from trip."""
|
||||
stations = {}
|
||||
|
|
48
web_view.py
48
web_view.py
|
@ -7,7 +7,6 @@ import operator
|
|||
import os.path
|
||||
import sys
|
||||
import traceback
|
||||
import typing
|
||||
from datetime import date, datetime, timedelta
|
||||
|
||||
import flask
|
||||
|
@ -22,7 +21,7 @@ import agenda.holidays
|
|||
import agenda.thespacedevs
|
||||
import agenda.trip
|
||||
from agenda import format_list_with_ampersand, travel, uk_tz
|
||||
from agenda.types import StrDict
|
||||
from agenda.types import Trip
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
app.debug = False
|
||||
|
@ -315,20 +314,20 @@ def human_readable_delta(future_date: date) -> str | None:
|
|||
return " ".join(parts) if parts else None
|
||||
|
||||
|
||||
@app.route("/trip/<start>")
|
||||
def trip_page(start: str) -> str:
|
||||
"""Individual trip page."""
|
||||
route_distances = agenda.travel.load_route_distances(app.config["DATA_DIR"])
|
||||
trip_list = [
|
||||
def get_trip_list(route_distances: agenda.travel.RouteDistances) -> list[Trip]:
|
||||
"""Get list of trips respecting current authentication status."""
|
||||
return [
|
||||
trip
|
||||
for trip in agenda.trip.build_trip_list(route_distances=route_distances)
|
||||
if flask.g.user.is_authenticated or not trip.private
|
||||
]
|
||||
|
||||
trip_iter = iter(trip_list)
|
||||
today = date.today()
|
||||
data_dir = flask.current_app.config["PERSONAL_DATA"]
|
||||
|
||||
def get_prev_current_and_next_trip(
|
||||
start: str, trip_list: list[Trip]
|
||||
) -> tuple[Trip | None, Trip | None, Trip | None]:
|
||||
"""Get previous trip, this trip and next trip."""
|
||||
trip_iter = iter(trip_list)
|
||||
prev_trip = None
|
||||
for trip in trip_iter:
|
||||
if trip.start.isoformat() == start:
|
||||
|
@ -336,26 +335,25 @@ def trip_page(start: str) -> str:
|
|||
prev_trip = trip
|
||||
next_trip = next(trip_iter, None)
|
||||
|
||||
return (prev_trip, trip, next_trip)
|
||||
|
||||
|
||||
@app.route("/trip/<start>")
|
||||
def trip_page(start: str) -> str:
|
||||
"""Individual trip page."""
|
||||
route_distances = agenda.travel.load_route_distances(app.config["DATA_DIR"])
|
||||
trip_list = get_trip_list(route_distances)
|
||||
|
||||
prev_trip, trip, next_trip = get_prev_current_and_next_trip(start, trip_list)
|
||||
if not trip:
|
||||
flask.abort(404)
|
||||
|
||||
today = date.today()
|
||||
|
||||
coordinates = agenda.trip.collect_trip_coordinates(trip)
|
||||
routes = agenda.trip.get_trip_routes(trip)
|
||||
if any(route["type"] == "unbooked_flight" for route in routes) and not any(
|
||||
pin["type"] == "airport" for pin in coordinates
|
||||
):
|
||||
airports = typing.cast(
|
||||
dict[str, StrDict], travel.parse_yaml("airports", data_dir)
|
||||
)
|
||||
lhr = airports["LHR"]
|
||||
coordinates.append(
|
||||
{
|
||||
"name": lhr["name"],
|
||||
"type": "airport",
|
||||
"latitude": lhr["latitude"],
|
||||
"longitude": lhr["longitude"],
|
||||
}
|
||||
)
|
||||
|
||||
agenda.trip.add_coordinates_for_unbooked_flights(routes, coordinates)
|
||||
|
||||
for route in routes:
|
||||
if "geojson_filename" in route:
|
||||
|
|
Loading…
Reference in a new issue