Compare commits
No commits in common. "38f2e10c6df06fe12adf7456e68d4ca9cbb697b6" and "093000bbc35271c95aa631a97f3aa77b5e39deba" have entirely different histories.
38f2e10c6d
...
093000bbc3
|
@ -3,7 +3,7 @@
|
||||||
import collections
|
import collections
|
||||||
import datetime
|
import datetime
|
||||||
import typing
|
import typing
|
||||||
from collections import defaultdict
|
from collections import Counter
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
|
|
||||||
import emoji
|
import emoji
|
||||||
|
@ -190,12 +190,12 @@ class Trip:
|
||||||
|
|
||||||
Any travel item with a missing or None 'distance' field is ignored.
|
Any travel item with a missing or None 'distance' field is ignored.
|
||||||
"""
|
"""
|
||||||
transport_distances: defaultdict[str, float] = defaultdict(float)
|
transport_distances: Counter[float] = Counter()
|
||||||
|
|
||||||
for item in self.travel:
|
for item in self.travel:
|
||||||
distance = item.get("distance")
|
distance = item.get("distance")
|
||||||
if distance:
|
if distance:
|
||||||
transport_type: str = item.get("type", "unknown")
|
transport_type = item.get("type", "unknown")
|
||||||
transport_distances[transport_type] += distance
|
transport_distances[transport_type] += distance
|
||||||
|
|
||||||
return list(transport_distances.items())
|
return list(transport_distances.items())
|
||||||
|
|
|
@ -3,10 +3,6 @@
|
||||||
{% macro display_date(dt) %}{{ dt.strftime("%a %-d %b %Y") }}{% endmacro %}
|
{% macro display_date(dt) %}{{ dt.strftime("%a %-d %b %Y") }}{% endmacro %}
|
||||||
{% macro display_date_no_year(dt) %}{{ dt.strftime("%a %-d %b") }}{% 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) %}
|
{% macro trip_link(trip) %}
|
||||||
<a href="{{ url_for("trip_page", start=trip.start.isoformat()) }}">{{ trip.title }}</a>
|
<a href="{{ url_for("trip_page", start=trip.start.isoformat()) }}">{{ trip.title }}</a>
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
|
|
||||||
{% from "macros.html" import trip_link, display_date_no_year, display_date, display_datetime, display_time, format_distance with context %}
|
{% from "macros.html" import trip_link, display_date_no_year, display_date, display_datetime, display_time with context %}
|
||||||
|
|
||||||
{% block title %}{{ heading }} - Edward Betts{% endblock %}
|
{% block title %}{{ heading }} - Edward Betts{% endblock %}
|
||||||
|
|
||||||
|
@ -52,18 +52,6 @@
|
||||||
{% set items = item_list | list %}
|
{% set items = item_list | list %}
|
||||||
<div class="heading"><h2>{{ heading }}</h2></div>
|
<div class="heading"><h2>{{ heading }}</h2></div>
|
||||||
<p>{{ items | count }} trips</p>
|
<p>{{ items | count }} trips</p>
|
||||||
|
|
||||||
<div>Total distance: {{ format_distance(total_distance) }}</div>
|
|
||||||
|
|
||||||
{% for transport_type, distance in distances_by_transport_type %}
|
|
||||||
<div>
|
|
||||||
{{ transport_type | title }}
|
|
||||||
distance: {{format_distance(distance) }}
|
|
||||||
</div>
|
|
||||||
{% endfor %}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
{% for trip in items %}
|
{% for trip in items %}
|
||||||
{% set distances_by_transport_type = trip.distances_by_transport_type() %}
|
{% set distances_by_transport_type = trip.distances_by_transport_type() %}
|
||||||
{% set total_distance = trip.total_distance() %}
|
{% set total_distance = trip.total_distance() %}
|
||||||
|
@ -86,17 +74,15 @@
|
||||||
<div>Start: {{ display_date_no_year(trip.start) }} (end date missing)</div>
|
<div>Start: {{ display_date_no_year(trip.start) }} (end date missing)</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if total_distance %}
|
{% if total_distance %}
|
||||||
<div>
|
<div>Total distance:
|
||||||
Total distance:
|
{{ "{:,.0f} km / {:,.0f} miles".format(total_distance, total_distance / 1.60934) }}
|
||||||
{{ format_distance(total_distance) }}
|
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if distances_by_transport_type %}
|
{% if distances_by_transport_type %}
|
||||||
{% for transport_type, distance in distances_by_transport_type %}
|
{% for transport_type, distance in distances_by_transport_type %}
|
||||||
<div>
|
<div>{{ transport_type | title }} distance:
|
||||||
{{ transport_type | title }}
|
{{ "{:,.0f} km / {:,.0f} miles".format(distance, distance / 1.60934) }}
|
||||||
distance: {{format_distance(distance) }}
|
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@ -122,14 +108,9 @@
|
||||||
{% if e.element_type == "flight" %}
|
{% if e.element_type == "flight" %}
|
||||||
<span class="text-nowrap"><strong>airline:</strong> {{ e.detail.airline_name }}</span>
|
<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>
|
<span class="text-nowrap"><strong>flight number:</strong> {{ e.detail.airline }}{{ e.detail.flight_number }}</span>
|
||||||
{% if e.detail.duration %}
|
|
||||||
<span class="text-nowrap"><strong>duration:</strong> {{ e.detail.duration }}</span>
|
<span class="text-nowrap"><strong>duration:</strong> {{ e.detail.duration }}</span>
|
||||||
{% endif %}
|
|
||||||
{# <pre>{{ e.detail | pprint }}</pre> #}
|
{# <pre>{{ e.detail | pprint }}</pre> #}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if e.detail.distance %}
|
|
||||||
<span class="text-nowrap"><strong>distance:</strong> {{ format_distance(e.detail.distance) }}</span>
|
|
||||||
{% endif %}
|
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
26
web_view.py
26
web_view.py
|
@ -8,7 +8,6 @@ import operator
|
||||||
import os.path
|
import os.path
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
from collections import defaultdict
|
|
||||||
from datetime import date, datetime, timedelta
|
from datetime import date, datetime, timedelta
|
||||||
|
|
||||||
import flask
|
import flask
|
||||||
|
@ -287,27 +286,6 @@ def trip_list() -> werkzeug.Response:
|
||||||
return flask.redirect(flask.url_for("trip_future_list"))
|
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")
|
@app.route("/trip/past")
|
||||||
def trip_past_list() -> str:
|
def trip_past_list() -> str:
|
||||||
"""Page showing a list of past trips."""
|
"""Page showing a list of past trips."""
|
||||||
|
@ -329,8 +307,6 @@ def trip_past_list() -> str:
|
||||||
get_country=agenda.get_country,
|
get_country=agenda.get_country,
|
||||||
format_list_with_ampersand=format_list_with_ampersand,
|
format_list_with_ampersand=format_list_with_ampersand,
|
||||||
fx_rate=agenda.fx.get_rates(app.config),
|
fx_rate=agenda.fx.get_rates(app.config),
|
||||||
total_distance=calc_total_distance(past),
|
|
||||||
distances_by_transport_type=sum_distances_by_transport_type(past),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -361,8 +337,6 @@ def trip_future_list() -> str:
|
||||||
get_country=agenda.get_country,
|
get_country=agenda.get_country,
|
||||||
format_list_with_ampersand=format_list_with_ampersand,
|
format_list_with_ampersand=format_list_with_ampersand,
|
||||||
fx_rate=agenda.fx.get_rates(app.config),
|
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…
Reference in a new issue