diff --git a/agenda/types.py b/agenda/types.py index 86ea7f9..0f4c086 100644 --- a/agenda/types.py +++ b/agenda/types.py @@ -1,5 +1,6 @@ """Types.""" +import collections import datetime import typing from collections import Counter @@ -22,6 +23,26 @@ def as_date(d: DateOrDateTime) -> datetime.date: return d +def as_datetime(d: DateOrDateTime) -> datetime.datetime: + """Date/time of event.""" + t0 = datetime.datetime.min.time() + return ( + d + if isinstance(d, datetime.datetime) + else datetime.datetime.combine(d, t0).replace(tzinfo=datetime.timezone.utc) + ) + + +@dataclass +class TripElement: + """Trip element.""" + + when: DateOrDateTime + title: str + element_type: str + detail: StrDict + + @dataclass class Trip: """Trip.""" @@ -157,6 +178,88 @@ class Trip: return list(transport_distances.items()) + def elements(self) -> list[TripElement]: + """Trip elements ordered by time.""" + elements: list[TripElement] = [] + + for item in self.accommodation: + title = "Airbnb" if item.get("operator") == "airbnb" else item["name"] + start = TripElement( + when=item["from"], + title=title, + detail=item, + element_type="check-in", + ) + + elements.append(start) + + end = TripElement( + when=item["to"], + title=title, + detail=item, + element_type="check-out", + ) + + elements.append(end) + + for item in self.travel: + if item["type"] == "flight": + flight_from = item["from_airport"] + flight_to = item["to_airport"] + name = ( + "✈️ " + + f"{flight_from['name']} ({flight_from['iata']}) -> " + + f"{flight_to['name']} ({flight_to['iata']})" + ) + + elements.append( + TripElement( + when=item["depart"], + title=name, + detail=item, + element_type="flight", + ) + ) + if item["type"] == "train": + name = f"{item['from']} -> {item['to']}" + elements.append( + TripElement( + when=item["depart"], + title=name, + detail=item, + element_type="train", + ) + ) + if item["type"] == "ferry": + name = f"{item['from']} -> {item['to']}" + elements.append( + TripElement( + when=item["depart"], + title=name, + detail=item, + element_type="ferry", + ) + ) + + return sorted(elements, key=lambda e: as_datetime(e.when)) + + def elements_grouped_by_day(self) -> list[tuple[datetime.date, list[TripElement]]]: + """Group trip elements by day.""" + # Create a dictionary to hold lists of TripElements grouped by their date + grouped_elements: collections.defaultdict[datetime.date, list[TripElement]] = ( + collections.defaultdict(list) + ) + + for element in self.elements(): + # Extract the date part of the 'when' attribute + day = as_date(element.when) + grouped_elements[day].append(element) + + # Convert the dictionary to a sorted list of tuples + grouped_elements_list = sorted(grouped_elements.items()) + + return grouped_elements_list + # Example usage: # You would call the function with your travel list here to get the results. diff --git a/templates/navbar.html b/templates/navbar.html index f0ea7bf..3bc28ff 100644 --- a/templates/navbar.html +++ b/templates/navbar.html @@ -2,7 +2,8 @@ {% set pages = [ {"endpoint": "index", "label": "Home" }, - {"endpoint": "trip_list", "label": "Trips" }, + {"endpoint": "trip_future_list", "label": "Future trips" }, + {"endpoint": "trip_past_list", "label": "Past trips" }, {"endpoint": "conference_list", "label": "Conference" }, {"endpoint": "travel_list", "label": "Travel" }, {"endpoint": "accommodation_list", "label": "Accommodation" }, diff --git a/templates/trip/list.html b/templates/trip/list.html new file mode 100644 index 0000000..f8c228e --- /dev/null +++ b/templates/trip/list.html @@ -0,0 +1,193 @@ +{% extends "base.html" %} + +{% from "macros.html" import trip_link, display_date_no_year, display_date, display_datetime, display_time, conference_row, accommodation_row, flight_row, train_row, ferry_row with context %} + +{% set row = { "flight": flight_row, "train": train_row, "ferry": ferry_row } %} + +{% block title %}Trips - Edward Betts{% endblock %} + +{% block style %} + + + +{% set conference_column_count = 8 %} +{% set accommodation_column_count = 8 %} +{% set travel_column_count = 10 %} + +{% endblock %} + +{% macro section(heading, item_list) %} + {% if item_list %} + {% set items = item_list | list %} +

{{ heading }}

+

{{ items | count }} trips

+ {% for trip in items %} + {% set total_distance = trip.total_distance() %} + {% set end = trip.end %} +
+

+ {{ trip_link(trip) }} + ({{ display_date(trip.start) }})

+
Countries: {{ trip.countries_str }}
+ {% if end %} +
Dates: {{ display_date_no_year(trip.start) }} to {{ display_date_no_year(end) }}
+ {% else %} +
Start: {{ display_date_no_year(trip.start) }} (end date missing)
+ {% endif %} + {% if total_distance %} +
Total distance: + {{ "{:,.0f} km / {:,.0f} miles".format(total_distance, total_distance / 1.60934) }} +
+ {% endif %} + + {# + {% for day in trip.days() %} +

{{ display_date_no_year(day) }}

+ {% endfor %} + #} + + {% for item in trip.conferences %} + {% set country = get_country(item.country) if item.country else None %} +
+
+
+ {{ item.name }} + + {{ display_date_no_year(item.start) }} to {{ display_date_no_year(item.end) }} + +
+

+ Topic: {{ item.topic }} + | Venue: {{ item.venue }} + | Location: {{ item.location }} + {% if country %} + {{ country.flag }} + {% elif item.online %} + 💻 Online + {% else %} + + country code {{ item.country }} not found + + {% endif %} + {% if item.free %} + | free to attend + {% elif item.price and item.currency %} + | price: {{ item.price }} {{ item.currency }} + {% endif %} +

+
+
+ {% endfor %} + + + + {% set date_heading = None %} + {% for day, elements in trip.elements_grouped_by_day() %} +

{{ display_date_no_year(day) }}

+ {% for e in elements %} +
+
+ {{ display_time(e.when) }} + — + {{ e.element_type }} + — + {{ e.title }} +
+
+ {% endfor %} + {% endfor %} + +
+ {% endfor %} + {% endif %} +{% endmacro %} + + +{% block content %} +
+
+
+
+
+ {{ section(heading, trips) }} +
+
+{% endblock %} + +{% block scripts %} + + + + + + + +{% endblock %} diff --git a/web_view.py b/web_view.py index c2f1142..2034fe4 100755 --- a/web_view.py +++ b/web_view.py @@ -280,6 +280,54 @@ def trip_list() -> str: ) +@app.route("/trip/past") +def trip_past_list() -> str: + """Page showing a list of past trips.""" + route_distances = agenda.travel.load_route_distances(app.config["DATA_DIR"]) + trip_list = get_trip_list(route_distances) + today = date.today() + + past = [item for item in trip_list if (item.end or item.start) < today] + + coordinates, routes = agenda.trip.get_coordinates_and_routes(past) + + return flask.render_template( + "trip/list.html", + heading="Past trips", + trips=reversed(past), + coordinates=coordinates, + routes=routes, + today=today, + get_country=agenda.get_country, + format_list_with_ampersand=format_list_with_ampersand, + fx_rate=agenda.fx.get_rates(app.config), + ) + + +@app.route("/trip/future") +def trip_future_list() -> str: + """Page showing a list of future trips.""" + route_distances = agenda.travel.load_route_distances(app.config["DATA_DIR"]) + trip_list = get_trip_list(route_distances) + today = date.today() + + future = [item for item in trip_list if item.start > today] + + coordinates, routes = agenda.trip.get_coordinates_and_routes(future) + + return flask.render_template( + "trip/list.html", + heading="Future trips", + trips=future, + coordinates=coordinates, + routes=routes, + today=today, + get_country=agenda.get_country, + format_list_with_ampersand=format_list_with_ampersand, + fx_rate=agenda.fx.get_rates(app.config), + ) + + @app.route("/trip/text") def trip_list_text() -> str: """Page showing a list of trips."""