From 07ad592895dcfe46bf36e9021a3293cdfc017d4a Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Sat, 21 Oct 2023 19:20:49 +1100 Subject: [PATCH] Add form to create user overview graphs --- app.py | 23 +++-- templates/partials/user_workouts_graphs.html | 88 ++++++++++++++++++++ templates/workouts_list.html | 7 +- 3 files changed, 105 insertions(+), 13 deletions(-) create mode 100644 templates/partials/user_workouts_graphs.html diff --git a/app.py b/app.py index 15803b1..c3d5e6f 100644 --- a/app.py +++ b/app.py @@ -216,6 +216,11 @@ def create_workout(user_id): @app.route('/user//workouts/graph', methods=['GET']) def graph_user_workouts(user_id): + if htmx: + return f""" + No image + """ user = User.query.get(user_id) workouts = user.workouts @@ -464,7 +469,11 @@ def generate_user_data(user, workouts=[]): 'workouts_count': len(workouts), 'workouts': workouts, 'daily_duration_sparkline': generate_daily_duration_sparkline(workouts), - 'calendar_month': generate_calendar_monthly_view(workouts, datetime.now().date()) + 'calendar_month': generate_calendar_monthly_view(workouts, datetime.now().date()), + 'attributes': [('workout_count', 'Workout count'), ('duration', 'Duration'), ('average_rpm', 'Average RPM'), ('average_bpm', 'Average BPM'), ('distance', 'Distance'), ('calories', 'Calories')], + 'periods': ['day', 'week', 'month'], + 'first_workout_date': workouts[-1]['start_time_date'] if workouts else None, + 'last_workout_date': workouts[0]['start_time_date'] if workouts else None, } @@ -513,7 +522,7 @@ def format_workout_data(workout, user): 'user_id': user.id, 'user_name': user.name, 'start_time': format_date_with_ordinal(workout.started_at, '%#H:%M %B %dth %Y'), - 'start_time_date': workout.started_at, + 'start_time_date': workout.started_at.date(), 'start_time_ago': humanize.naturaltime(workout.started_at), 'duration': humanize.naturaldelta(duration), 'duration_minutes': duration.total_seconds() / 60, @@ -613,8 +622,8 @@ def generate_calendar_monthly_view(workouts, selected_date): start_date, end_date = get_month_bounds(selected_date) # Build a lookup dictionary for faster access - workout_lookup = {w['start_time_date'].date( - ): w for w in workouts if start_date <= w['start_time_date'].date() <= end_date} + workout_lookup = {w['start_time_date'] + : w for w in workouts if start_date <= w['start_time_date'] <= end_date} current_date = datetime.now().date() days_of_month = [ @@ -655,11 +664,11 @@ def generate_daily_duration_sparkline(workouts): return '' # Determine date range based on workouts data - start_date = workouts[-1]['start_time_date'].date() - end_date = workouts[0]['start_time_date'].date() + start_date = workouts[-1]['start_time_date'] + end_date = workouts[0]['start_time_date'] # Build a mapping of dates to their respective durations for easier lookup - workouts_by_date = {w['start_time_date'].date(): int( + workouts_by_date = {w['start_time_date']: int( w['duration_minutes']) for w in workouts} daily_durations = [workouts_by_date.get( diff --git a/templates/partials/user_workouts_graphs.html b/templates/partials/user_workouts_graphs.html new file mode 100644 index 0000000..8c9105d --- /dev/null +++ b/templates/partials/user_workouts_graphs.html @@ -0,0 +1,88 @@ +
+
+
+ + +
+
+
+ +
+
+ +
+ +
+
+
+ +
+
+ +
+ +
+
+
+
+ + +
+
+
+ +
+ No image + No image +
\ No newline at end of file diff --git a/templates/workouts_list.html b/templates/workouts_list.html index a18b12a..6a81576 100644 --- a/templates/workouts_list.html +++ b/templates/workouts_list.html @@ -75,12 +75,7 @@