From a7296c943b23e16828919ce9e4b1e8d45e83e822 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sat, 6 Apr 2024 09:24:10 +0200 Subject: [PATCH] Show trip total distance on trip list page Closes: #142 --- agenda/types.py | 8 ++++++++ templates/trip_list.html | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/agenda/types.py b/agenda/types.py index ed59d86..d150db6 100644 --- a/agenda/types.py +++ b/agenda/types.py @@ -133,6 +133,14 @@ class Trip: """Countries flags for trip.""" return "".join(c.flag for c in self.countries) + def total_distance(self) -> float | None: + """Total distance for trip.""" + return ( + sum(t["distance"] for t in self.travel) + if all(t.get("distance") for t in self.travel) + else None + ) + @dataclass class Holiday: diff --git a/templates/trip_list.html b/templates/trip_list.html index 58bc5a9..7ab7d9e 100644 --- a/templates/trip_list.html +++ b/templates/trip_list.html @@ -52,6 +52,7 @@

{{ heading }}

{{ items | count }} trips

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

@@ -63,6 +64,11 @@ {% 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 conf in trip.conferences %} {{ conference_row(conf, "going") }}