From 75242c2952900576b9da3e577e0e7f4eb5639cdd Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 27 May 2024 10:57:46 +0200 Subject: [PATCH] Show end times for travel --- agenda/types.py | 30 +++++++++++++++++++++--------- templates/macros.html | 4 +++- templates/trip/list.html | 10 +++++++--- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/agenda/types.py b/agenda/types.py index 0e5bfc4..472db94 100644 --- a/agenda/types.py +++ b/agenda/types.py @@ -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 ) ) diff --git a/templates/macros.html b/templates/macros.html index 50e55cf..ae77489 100644 --- a/templates/macros.html +++ b/templates/macros.html @@ -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 %} diff --git a/templates/trip/list.html b/templates/trip/list.html index c39a516..346f186 100644 --- a/templates/trip/list.html +++ b/templates/trip/list.html @@ -111,14 +111,18 @@ {% set c = get_country(e.detail.country) %}
{{ 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) }})
{% else %}
{{ e.get_emoji() }} - {{ display_time(e.when) }} + {{ display_time(e.start_time) }} – - {{ e.title }} + {{ e.start_loc }} + → + {{ display_time(e.end_time) }} + – + {{ e.end_loc }} {% if e.element_type == "flight" %} airline: {{ e.detail.airline_name }} flight number: {{ e.detail.airline }}{{ e.detail.flight_number }}