From f8c523c674113ad1dd4e4764bb0323031d4f12d1 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 27 May 2024 10:12:58 +0200 Subject: [PATCH 1/3] Add format_distance macro --- templates/macros.html | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/templates/macros.html b/templates/macros.html index fec5da9..50e55cf 100644 --- a/templates/macros.html +++ b/templates/macros.html @@ -3,6 +3,10 @@ {% macro display_date(dt) %}{{ dt.strftime("%a %-d %b %Y") }}{% endmacro %} {% macro display_date_no_year(dt) %}{{ dt.strftime("%a %-d %b") }}{% endmacro %} +{% macro format_distance(distance) %} + {{ "{:,.0f} km / {:,.0f} miles".format(distance, distance / 1.60934) }} +{% endmacro %} + {% macro trip_link(trip) %} {{ trip.title }} {% endmacro %} From cd8dfb74a4d60b257fa86eaed1ff54fb65cc1057 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 27 May 2024 10:13:24 +0200 Subject: [PATCH 2/3] Use defaultdict, not Counter for travel distances --- agenda/types.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/agenda/types.py b/agenda/types.py index 1b4a27a..0e5bfc4 100644 --- a/agenda/types.py +++ b/agenda/types.py @@ -3,7 +3,7 @@ import collections import datetime import typing -from collections import Counter +from collections import defaultdict from dataclasses import dataclass, field import emoji @@ -190,12 +190,12 @@ class Trip: Any travel item with a missing or None 'distance' field is ignored. """ - transport_distances: Counter[float] = Counter() + transport_distances: defaultdict[str, float] = defaultdict(float) for item in self.travel: distance = item.get("distance") if distance: - transport_type = item.get("type", "unknown") + transport_type: str = item.get("type", "unknown") transport_distances[transport_type] += distance return list(transport_distances.items()) From 38f2e10c6df06fe12adf7456e68d4ca9cbb697b6 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 27 May 2024 10:16:03 +0200 Subject: [PATCH 3/3] Show distances for all past and future trips. --- templates/trip/list.html | 31 +++++++++++++++++++++++++------ web_view.py | 26 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/templates/trip/list.html b/templates/trip/list.html index a35ac5b..c39a516 100644 --- a/templates/trip/list.html +++ b/templates/trip/list.html @@ -1,6 +1,6 @@ {% extends "base.html" %} -{% from "macros.html" import trip_link, display_date_no_year, display_date, display_datetime, display_time with context %} +{% from "macros.html" import trip_link, display_date_no_year, display_date, display_datetime, display_time, format_distance with context %} {% block title %}{{ heading }} - Edward Betts{% endblock %} @@ -52,6 +52,18 @@ {% set items = item_list | list %}

{{ heading }}

{{ items | count }} trips

+ +
Total distance: {{ format_distance(total_distance) }}
+ + {% for transport_type, distance in distances_by_transport_type %} +
+ {{ transport_type | title }} + distance: {{format_distance(distance) }} +
+ {% endfor %} + + + {% for trip in items %} {% set distances_by_transport_type = trip.distances_by_transport_type() %} {% set total_distance = trip.total_distance() %} @@ -74,15 +86,17 @@
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) }} +
+ Total distance: + {{ format_distance(total_distance) }}
{% endif %} {% if distances_by_transport_type %} {% for transport_type, distance in distances_by_transport_type %} -
{{ transport_type | title }} distance: - {{ "{:,.0f} km / {:,.0f} miles".format(distance, distance / 1.60934) }} +
+ {{ transport_type | title }} + distance: {{format_distance(distance) }}
{% endfor %} {% endif %} @@ -108,9 +122,14 @@ {% if e.element_type == "flight" %} airline: {{ e.detail.airline_name }} flight number: {{ e.detail.airline }}{{ e.detail.flight_number }} - duration: {{ e.detail.duration }} + {% if e.detail.duration %} + duration: {{ e.detail.duration }} + {% endif %} {#
{{ e.detail | pprint }}
#} {% endif %} + {% if e.detail.distance %} + distance: {{ format_distance(e.detail.distance) }} + {% endif %}
{% endif %} {% endfor %} diff --git a/web_view.py b/web_view.py index 286d4ad..8c30bbd 100755 --- a/web_view.py +++ b/web_view.py @@ -8,6 +8,7 @@ import operator import os.path import sys import traceback +from collections import defaultdict from datetime import date, datetime, timedelta import flask @@ -286,6 +287,27 @@ def trip_list() -> werkzeug.Response: return flask.redirect(flask.url_for("trip_future_list")) +def calc_total_distance(trips: list[Trip]) -> float: + """Total distance for trips.""" + total = 0.0 + for item in trips: + dist = item.total_distance() + if dist: + total += dist + + return total + + +def sum_distances_by_transport_type(trips: list[Trip]) -> list[tuple[str, float]]: + """Sum distances by transport type.""" + distances_by_transport_type: defaultdict[str, float] = defaultdict(float) + for trip in trips: + for transport_type, dist in trip.distances_by_transport_type(): + distances_by_transport_type[transport_type] += dist + + return list(distances_by_transport_type.items()) + + @app.route("/trip/past") def trip_past_list() -> str: """Page showing a list of past trips.""" @@ -307,6 +329,8 @@ def trip_past_list() -> str: get_country=agenda.get_country, format_list_with_ampersand=format_list_with_ampersand, fx_rate=agenda.fx.get_rates(app.config), + total_distance=calc_total_distance(past), + distances_by_transport_type=sum_distances_by_transport_type(past), ) @@ -337,6 +361,8 @@ def trip_future_list() -> str: get_country=agenda.get_country, format_list_with_ampersand=format_list_with_ampersand, fx_rate=agenda.fx.get_rates(app.config), + total_distance=calc_total_distance(current + future), + distances_by_transport_type=sum_distances_by_transport_type(current + future), )