Split trip list into future and past pages

Redo page layout and trip display. Map is now shown on the right.
This commit is contained in:
Edward Betts 2024-05-18 12:02:21 +02:00
parent 3ec7f5c18a
commit cd16b857a0
4 changed files with 346 additions and 1 deletions

View file

@ -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.