Show distances for all past and future trips.
This commit is contained in:
parent
cd8dfb74a4
commit
38f2e10c6d
2 changed files with 51 additions and 6 deletions
26
web_view.py
26
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),
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue