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