Show end times for travel

This commit is contained in:
Edward Betts 2024-05-27 10:57:46 +02:00
parent 38f2e10c6d
commit 75242c2952
3 changed files with 31 additions and 13 deletions

View file

@ -38,10 +38,13 @@ def as_datetime(d: DateOrDateTime) -> datetime.datetime:
class TripElement:
"""Trip element."""
when: DateOrDateTime
start_time: DateOrDateTime
title: str
element_type: str
detail: StrDict
end_time: DateOrDateTime | None = None
start_loc: str | None = None
end_loc: str | None = None
def get_emoji(self) -> str | None:
"""Emoji for trip element."""
@ -207,7 +210,7 @@ class Trip:
for item in self.accommodation:
title = "Airbnb" if item.get("operator") == "airbnb" else item["name"]
start = TripElement(
when=item["from"],
start_time=item["from"],
title=title,
detail=item,
element_type="check-in",
@ -216,7 +219,7 @@ class Trip:
elements.append(start)
end = TripElement(
when=item["to"],
start_time=item["to"],
title=title,
detail=item,
element_type="check-out",
@ -233,10 +236,13 @@ class Trip:
elements.append(
TripElement(
when=item["depart"],
start_time=item["depart"],
end_time=item.get("arrive"),
title=name,
detail=item,
element_type="flight",
start_loc=airport_label(item["from_airport"]),
end_loc=airport_label(item["to_airport"]),
)
)
if item["type"] == "train":
@ -250,10 +256,13 @@ class Trip:
name = f"{leg['from']} {from_flag}{leg['to']} {to_flag}"
elements.append(
TripElement(
when=leg["depart"],
start_time=leg["depart"],
end_time=leg["arrive"],
title=name,
detail=leg,
element_type="train",
start_loc=f"{leg['from']} {from_flag}",
end_loc=f"{leg['to']} {to_flag}",
)
)
if item["type"] == "ferry":
@ -267,14 +276,17 @@ class Trip:
name = f"{item['from']} {from_flag}{item['to']} {to_flag}"
elements.append(
TripElement(
when=item["depart"],
start_time=item["depart"],
end_time=item["arrive"],
title=name,
detail=item,
element_type="ferry",
start_loc=f"{item['from']} {from_flag}",
end_loc=f"{item['to']} {to_flag}",
)
)
return sorted(elements, key=lambda e: as_datetime(e.when))
return sorted(elements, key=lambda e: as_datetime(e.start_time))
def elements_grouped_by_day(self) -> list[tuple[datetime.date, list[TripElement]]]:
"""Group trip elements by day."""
@ -285,7 +297,7 @@ class Trip:
for element in self.elements():
# Extract the date part of the 'when' attribute
day = as_date(element.when)
day = as_date(element.start_time)
grouped_elements[day].append(element)
# Sort elements within each day
@ -294,7 +306,7 @@ class Trip:
key=lambda e: (
e.element_type == "check-in", # check-out elements last
e.element_type != "check-out", # check-in elements first
as_datetime(e.when), # then sort by time
as_datetime(e.start_time), # then sort by time
)
)

View file

@ -1,5 +1,7 @@
{% macro display_datetime(dt) %}{{ dt.strftime("%a, %d, %b %Y %H:%M %z") }}{% endmacro %}
{% macro display_time(dt) %}{{ dt.strftime("%H:%M %z") }}{% endmacro %}
{% macro display_time(dt) %}
{% if dt %}{{ dt.strftime("%H:%M %z") }}{% endif %}
{% endmacro %}
{% macro display_date(dt) %}{{ dt.strftime("%a %-d %b %Y") }}{% endmacro %}
{% macro display_date_no_year(dt) %}{{ dt.strftime("%a %-d %b") }}{% endmacro %}

View file

@ -111,14 +111,18 @@
{% set c = get_country(e.detail.country) %}
<div>
{{ e.get_emoji() }} {{ e.title }} {{ c.flag }}
({{ accommodation_label[e.element_type] }} {{ display_time(e.when) }})
({{ accommodation_label[e.element_type] }} {{ display_time(e.start_time) }})
</div>
{% else %}
<div>
{{ e.get_emoji() }}
{{ display_time(e.when) }}
{{ display_time(e.start_time) }}
&ndash;
{{ e.title }}
{{ e.start_loc }}
{{ display_time(e.end_time) }}
&ndash;
{{ e.end_loc }}
{% if e.element_type == "flight" %}
<span class="text-nowrap"><strong>airline:</strong> {{ e.detail.airline_name }}</span>
<span class="text-nowrap"><strong>flight number:</strong> {{ e.detail.airline }}{{ e.detail.flight_number }}</span>