From 3025e048750779c30fa6ce9d51a98f88aa8e7f10 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Mon, 27 Mar 2023 23:13:11 +0200 Subject: profile: add longest and shortest track --- fietsboek/templates/profile.jinja2 | 80 ++++++++++++++++++++++++++++++++++---- fietsboek/views/profile.py | 31 +++++++++++---- 2 files changed, 95 insertions(+), 16 deletions(-) diff --git a/fietsboek/templates/profile.jinja2 b/fietsboek/templates/profile.jinja2 index 516cee9..fe49990 100644 --- a/fietsboek/templates/profile.jinja2 +++ b/fietsboek/templates/profile.jinja2 @@ -1,5 +1,59 @@ {% extends "layout.jinja2" %} +{% macro render_track_card(track) %} +
+
+ {{ track.title | default(track.date, true) }} + {% if track.text_tags() %} + {% for tag in track.tags %}{{ tag.tag }} {% endfor %} + {% endif %} +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
{{ _("page.details.date") }}{{ track.date | format_datetime }}{{ _("page.details.length") }}{{ (track.length / 1000) | round(2) | format_decimal }} km
{{ _("page.details.start_time") }}{{ track.start_time | format_datetime }}{{ _("page.details.end_time") }}{{ track.end_time | format_datetime }}
{{ _("page.details.uphill") }}{{ track.uphill | round(2) | format_decimal }} m{{ _("page.details.downhill") }}{{ track.downhill | round(2) | format_decimal }} m
{{ _("page.details.moving_time") }}{{ track.moving_time }}{{ _("page.details.stopped_time") }}{{ track.stopped_time }}
{{ _("page.details.max_speed") }}{{ mps_to_kph(track.max_speed) | round(2) | format_decimal }} km/h{{ _("page.details.avg_speed") }}{{ mps_to_kph(track.avg_speed) | round(2) | format_decimal }} km/h
+ +
    +
  • {{ track.owner.name }}
  • + {% for user in track.tagged_people %} +
  • {{ user.name }}
  • + {% endfor %} +
+
+
+{% endmacro %} + {% block content %}

{{ user.name }}

@@ -10,37 +64,47 @@ - + - + - + - + - + - + - + - +
{{ _("page.profile.length") }}{{ (total_length / 1000) | round(2) | format_decimal }} km{{ (total.length / 1000) | round(2) | format_decimal }} km
{{ _("page.profile.uphill") }}{{ total_uphill | round(2) | format_decimal }} m{{ total.uphill | round(2) | format_decimal }} m
{{ _("page.profile.downhill") }}{{ total_downhill | round(2) | format_decimal }} m{{ total.downhill | round(2) | format_decimal }} m
{{ _("page.profile.moving_time") }}{{ total_moving_time }}{{ total.moving_time }}
{{ _("page.profile.stopped_time") }}{{ total_stopped_time }}{{ total.stopped_time }}
{{ _("page.profile.max_speed") }}{{ mps_to_kph(max_speed) | round(2) | format_decimal }} km/h{{ mps_to_kph(total.max_speed) | round(2) | format_decimal }} km/h
{{ _("page.profile.avg_speed") }}{{ mps_to_kph(avg_speed) | round(2) | format_decimal }} km/h{{ mps_to_kph(total.avg_speed) | round(2) | format_decimal }} km/h
{{ _("page.profile.number_of_tracks") }}{{ number_of_tracks }}{{ total.count }}
+ + {% if total.longest_distance_track %} +

{{ _("page.profile.longest_distance_track") }}

+ {{ render_track_card(total.longest_distance_track) }} + {% endif %} + + {% if total.shortest_distance_track %} +

{{ _("page.profile.shortest_distance_track") }}

+ {{ render_track_card(total.shortest_distance_track) }} + {% endif %}
{% endblock %} diff --git a/fietsboek/views/profile.py b/fietsboek/views/profile.py index 2a4203e..df49c3c 100644 --- a/fietsboek/views/profile.py +++ b/fietsboek/views/profile.py @@ -2,6 +2,7 @@ import datetime import sqlite3 from dataclasses import dataclass +from typing import Optional from pyramid.httpexceptions import HTTPNotFound from pyramid.request import Request @@ -44,6 +45,12 @@ class CumulativeStats: max_speed: float = 0.0 """Overall maximum speed, in m/s.""" + longest_distance_track: Optional[TrackWithMetadata] = None + """The track with the longest distance.""" + + shortest_distance_track: Optional[TrackWithMetadata] = None + """The track with the shortest distance.""" + def add(self, track: TrackWithMetadata): """Adds a track to this stats collection. @@ -57,6 +64,18 @@ class CumulativeStats: self.stopped_time += track.stopped_time self.max_speed = max(self.max_speed, track.max_speed) + if ( + self.shortest_distance_track is None + or self.longest_distance_track.length < track.length + ): + self.longest_distance_track = track + + if ( + self.shortest_distance_track is None + or self.shortest_distance_track.length > track.length + ): + self.shortest_distance_track = track + @property def avg_speed(self) -> float: """Average speed, in m/s.""" @@ -92,6 +111,9 @@ def profile(request: Request) -> dict: meta = TrackWithMetadata(track, request.data_manager) total.add(meta) + total.moving_time = round_to_seconds(total.moving_time) + total.stopped_time = round_to_seconds(total.stopped_time) + heatmap_url = None tilehunt_url = None try: @@ -126,15 +148,8 @@ def profile(request: Request) -> dict: return { "user": request.context, - "total_length": total.length, - "total_uphill": total.uphill, - "total_downhill": total.downhill, - "total_moving_time": round_to_seconds(total.moving_time), - "total_stopped_time": round_to_seconds(total.stopped_time), - "max_speed": total.max_speed, - "avg_speed": total.avg_speed, + "total": total, "mps_to_kph": util.mps_to_kph, - "number_of_tracks": total.count, "heatmap_url": heatmap_url, "tilehunt_url": tilehunt_url, } -- cgit v1.2.3