From 797fc19a754c20228c9d03295e032be767cc4b5a Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Fri, 13 Oct 2023 22:15:37 +1100 Subject: [PATCH] When a day with a workout is selected a workout overview is rendered --- app.py | 101 ++++++++------- templates/partials/calendar.html | 10 +- templates/partials/selected_workout_view.html | 116 ++++++++++++++++++ templates/workouts_list.html | 5 +- 4 files changed, 187 insertions(+), 45 deletions(-) create mode 100644 templates/partials/selected_workout_view.html diff --git a/app.py b/app.py index 81bb1c6..4ca1d33 100644 --- a/app.py +++ b/app.py @@ -293,6 +293,14 @@ def workout_list(user_id): return render_template('partials/workouts_list_fragment.html', workouts=workouts, user_id=user_id, workouts_all_loaded=True) +@ app.route("/user//workout//calendar_view", methods=['GET']) +def calendar_workout_view(user_id, workout_id): + workout = Workout.query.filter_by(user_id=user_id, id=workout_id).first() + user = User.query.get(user_id) + workout_view_data = get_workout_view_data(workout, user) + return render_template('partials/selected_workout_view.html', workout=workout_view_data, user_id=user_id) + + def render_users_and_workouts(): users = User.query.all() users_data = [] @@ -369,52 +377,59 @@ def get_workouts_for_user(user_id, max_date=None): Workout.created_at.desc()).all() for workout in workouts: - - duration = timedelta( - seconds=int(workout.duration)) if workout.duration else timedelta(seconds=0) - average_rpm = workout.average_rpm if workout.average_rpm else 0 - min_rpm = workout.min_rpm if workout.min_rpm else 0 - max_rpm = workout.max_rpm if workout.max_rpm else 0 - calories = workout.calories if workout.calories else 0 - distance = workout.distance if workout.distance else 0 - average_bpm = workout.average_bpm if workout.average_bpm else 0 - min_bpm = workout.min_bpm if workout.min_bpm else 0 - max_bpm = workout.max_bpm if workout.max_bpm else 0 - is_heart_rate_available = workout.is_heart_rate_available - is_cadence_available = workout.is_cadence_available - start_time = workout.started_at - - selected_graph_types = ['speed'] - if is_heart_rate_available: - selected_graph_types.append('heart_rate') - - if is_cadence_available or is_heart_rate_available: - workouts_data.append({ - 'id': workout.id, - 'user_id': user_id, - 'user_name': user.name, - 'start_time': format_date_with_ordinal(start_time, '%#H:%M %B %dth %Y'), - 'start_time_date': start_time, - 'start_time_ago': humanize.naturaltime(start_time), - 'duration': humanize.naturaldelta(duration), - 'duration_minutes': duration.total_seconds() / 60, - 'average_rpm': int(average_rpm), - 'min_rpm': int(min_rpm), - 'max_rpm': int(max_rpm), - 'calories': int(calories), - 'distance': int(distance), - 'bike_display_name': workout.bike.display_name, - 'selected_graph_types': selected_graph_types, - 'is_heart_rate_available': is_heart_rate_available, - 'is_cadence_available': is_cadence_available, - 'average_bpm': int(average_bpm), - 'min_bpm': int(min_bpm), - 'max_bpm': int(max_bpm), - }) + workout_data = get_workout_view_data(workout, user) + if workout_data: + workouts_data.append(workout_data) return workouts_data +def get_workout_view_data(workout, user): + duration = timedelta( + seconds=int(workout.duration)) if workout.duration else timedelta(seconds=0) + average_rpm = workout.average_rpm if workout.average_rpm else 0 + min_rpm = workout.min_rpm if workout.min_rpm else 0 + max_rpm = workout.max_rpm if workout.max_rpm else 0 + calories = workout.calories if workout.calories else 0 + distance = workout.distance if workout.distance else 0 + average_bpm = workout.average_bpm if workout.average_bpm else 0 + min_bpm = workout.min_bpm if workout.min_bpm else 0 + max_bpm = workout.max_bpm if workout.max_bpm else 0 + is_heart_rate_available = workout.is_heart_rate_available + is_cadence_available = workout.is_cadence_available + start_time = workout.started_at + + selected_graph_types = ['speed'] + if is_heart_rate_available: + selected_graph_types.append('heart_rate') + + if is_cadence_available or is_heart_rate_available: + return { + 'id': workout.id, + 'user_id': user.id, + 'user_name': user.name, + 'start_time': format_date_with_ordinal(start_time, '%#H:%M %B %dth %Y'), + 'start_time_date': start_time, + 'start_time_ago': humanize.naturaltime(start_time), + 'duration': humanize.naturaldelta(duration), + 'duration_minutes': duration.total_seconds() / 60, + 'average_rpm': int(average_rpm), + 'min_rpm': int(min_rpm), + 'max_rpm': int(max_rpm), + 'calories': int(calories), + 'distance': int(distance), + 'bike_display_name': workout.bike.display_name, + 'selected_graph_types': selected_graph_types, + 'is_heart_rate_available': is_heart_rate_available, + 'is_cadence_available': is_cadence_available, + 'average_bpm': int(average_bpm), + 'min_bpm': int(min_bpm), + 'max_bpm': int(max_bpm), + } + else: + return None + + def create_graph(x_values, y_values, y_label, filename, x_label='Time'): fig, ax = plt.subplots() ax.plot(x_values, y_values) @@ -471,7 +486,7 @@ def generate_calendar_monthly_view(workouts, selected_date): timedelta(days=start[first_day_of_month.weekday()]) end = dict([(6, 6), (0, 5), (1, 4), - (2, 3), (3, 2), (4, 1), (5, 0)]) + (2, 3), (3, 2), (4, 1), (5, 0)]) end_date = last_day_of_month + \ timedelta(days=end[last_day_of_month.weekday()]) diff --git a/templates/partials/calendar.html b/templates/partials/calendar.html index 096d4f6..f1d6763 100644 --- a/templates/partials/calendar.html +++ b/templates/partials/calendar.html @@ -68,7 +68,11 @@ + + +
+
\ No newline at end of file diff --git a/templates/partials/selected_workout_view.html b/templates/partials/selected_workout_view.html new file mode 100644 index 0000000..162bf2d --- /dev/null +++ b/templates/partials/selected_workout_view.html @@ -0,0 +1,116 @@ +
+
+ +
+
+ + +
+
+ + + + {{ workout.duration }} +
+
+ + + + {{ workout.average_rpm + }} RPM +
+ {% if workout.is_heart_rate_available %} +
+ + + + {{workout.average_bpm }} BPM +
+ {% endif %} +
+ + + + {{ workout.distance }} KM +
+
+ + + + + + + {{ workout.calories }} CALS +
+
+ + + + {{ + workout.bike_display_name }} +
+
+ +
+
+
+ {% with workout=workout, graph_types=workout['selected_graph_types'] %} + {% include 'workout_view.html' %} + {% endwith %} +
+
+ +
+
+
\ No newline at end of file diff --git a/templates/workouts_list.html b/templates/workouts_list.html index 7bddf1b..c0618d8 100644 --- a/templates/workouts_list.html +++ b/templates/workouts_list.html @@ -65,7 +65,10 @@ {% endif %} \ No newline at end of file