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 %} +
{{ items | count }} trips
+ {% for trip in items %} + {% set total_distance = trip.total_distance() %} + {% set end = trip.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 %} +
+