Improve users and workouts overview page WIP
This commit is contained in:
38
app.py
38
app.py
@@ -1,5 +1,5 @@
|
||||
from sqlalchemy import func
|
||||
from datetime import timedelta
|
||||
from datetime import datetime, timedelta
|
||||
import io
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask import Flask, make_response, render_template, request, jsonify
|
||||
@@ -8,6 +8,7 @@ from flask_htmx import HTMX
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib.dates as mdates
|
||||
import os
|
||||
import sparklines
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
@@ -165,7 +166,7 @@ def view_workout(user_id, workout_id):
|
||||
|
||||
|
||||
@app.route('/user/<int:user_id>/workout/<int:workout_id>/delete', methods=['DELETE'])
|
||||
def delete_workout(user_io, workout_id):
|
||||
def delete_workout(user_id, workout_id):
|
||||
# Delete the workout and its associated cadence readings
|
||||
CadenceReading.query.filter_by(workout_id=workout_id).delete()
|
||||
db.session.delete(workout)
|
||||
@@ -198,12 +199,41 @@ def render_users_and_workouts():
|
||||
users_data = []
|
||||
for user in users:
|
||||
workouts = get_workouts_for_user(user.id)
|
||||
workout_counts_by_week = []
|
||||
duration_by_week = []
|
||||
# get start date from last workout
|
||||
start_date = workouts[-1]['start_time_date'].date()
|
||||
# get end date from first workout
|
||||
end_date = workouts[0]['start_time_date'].date()
|
||||
# calculate number of weeks between start and end date
|
||||
num_weeks = (end_date - start_date).days // 7 + 1
|
||||
|
||||
for i in range(num_weeks):
|
||||
# Calculate the start and end of the current week
|
||||
start = start_date + timedelta(days=7*i)
|
||||
end = start + timedelta(days=6)
|
||||
|
||||
# Get the workouts for the current week
|
||||
weekly_workouts = [w for w in workouts if start <=
|
||||
w['start_time_date'].date() <= end]
|
||||
|
||||
# Calculate the workout counts and duration for the current week
|
||||
workout_counts_by_week.append(len(weekly_workouts))
|
||||
duration_by_week.append(
|
||||
int(sum([int(w['duration_minutes']) for w in weekly_workouts]))/len(weekly_workouts))
|
||||
|
||||
workout_counts_sparkline = sparklines.sparklines(
|
||||
workout_counts_by_week)
|
||||
duration_sparkline = sparklines.sparklines(duration_by_week)
|
||||
|
||||
user_data = {
|
||||
'id': user.id,
|
||||
'name': user.name,
|
||||
'bike_id': user.bike_id,
|
||||
'workouts_count': len(workouts),
|
||||
'workouts': workouts
|
||||
'workouts': workouts,
|
||||
'workout_counts_by_week': workout_counts_by_week,
|
||||
'duration_by_week': duration_by_week
|
||||
}
|
||||
users_data.append(user_data)
|
||||
return render_template('users_and_workouts.html', users=users_data, bikes=Bike.query.all())
|
||||
@@ -230,7 +260,9 @@ def get_workouts_for_user(user_id):
|
||||
'id': workout.id,
|
||||
'user_id': user_id,
|
||||
'start_time': format_date_with_ordinal(start_time, '%#H:%M %B %dth %Y'),
|
||||
'start_time_date': start_time,
|
||||
'duration': format_duration(duration),
|
||||
'duration_minutes': duration.total_seconds() / 60,
|
||||
'average_rpm': int(average_rpm),
|
||||
'calories': int(calories),
|
||||
'distance': int(distance)
|
||||
|
||||
Reference in New Issue
Block a user