Change homepage to users overview with link to new workout page

This commit is contained in:
Peter Stockings
2023-03-10 21:31:48 +11:00
parent e943f45625
commit 2498aa44c9

95
app.py
View File

@@ -1,3 +1,4 @@
from sqlalchemy import func
from datetime import timedelta
import io
from flask_sqlalchemy import SQLAlchemy
@@ -49,9 +50,26 @@ class CadenceReading(db.Model):
rpm = db.Column(db.Integer, nullable=False)
@app.route("/")
def home():
return render_template('attemptv2.html')
@app.route('/', methods=['GET'])
def get_workouts():
users = User.query.all()
users_data = []
for user in users:
workouts = Workout.query.filter_by(user_id=user.id).all()
user_data = {
'id': user.id,
'name': user.name,
'workouts_count': len(workouts),
'workouts': get_workouts_for_user(user.id)
}
users_data.append(user_data)
return render_template('users_and_workouts.html', users=users_data)
@app.route("/user/<int:user_id>/new_workout")
def new_workout(user_id):
user = User.query.get(user_id)
return render_template('new_workout.html', user=user)
@app.route('/users', methods=['GET', 'POST'])
@@ -88,41 +106,10 @@ def delete_user(user_id):
@app.route('/user/<int:user_id>/workouts', methods=['GET', 'POST'])
def create_workout(user_id):
def workouts(user_id):
if request.method == 'GET':
# get a list of all workouts for a user
workouts = Workout.query.filter_by(user_id=user_id).all()
workouts_data = []
for workout in workouts:
cadence_readings = CadenceReading.query.filter_by(
workout_id=workout.id).all()
if cadence_readings:
# get the earliest and latest cadence readings timestamps
start_time = min(
reading.created_at for reading in cadence_readings)
end_time = max(
reading.created_at for reading in cadence_readings)
duration = end_time - start_time
# format the duration as hh:mm:ss or mm:ss or ss
if duration >= timedelta(hours=1):
duration_str = str(duration)
else:
duration_str = str(duration).split('.')[0]
# calculate average and maximum rpm
rpms = [reading.rpm for reading in cadence_readings]
avg_rpm = sum(rpms) / len(rpms)
max_rpm = max(rpms)
workouts_data.append({
'id': workout.id,
'started_at': start_time.strftime('%a %b %d %Y %H:%M:%S'),
'finished_at': end_time.strftime('%a %b %d %Y %H:%M:%S'),
'duration': duration_str,
'avg_rpm': int(avg_rpm),
'max_rpm': max_rpm
})
return jsonify({'workouts': workouts_data}), 200
workouts_data = get_workouts_for_user(user_id)
return render_template('workouts_list.html', workouts=workouts_data)
elif request.method == 'POST':
data = request.json
@@ -200,6 +187,40 @@ def workout(user_id, workout_id):
return jsonify({'message': 'Workout {} not found for user {}.'.format(workout_id, user_id)}), 404
@app.route('/user/<int:user_id>/workouts', methods=['GET', 'DELETE'])
def workouts_for_user(user_id):
workouts_data = get_workouts_for_user(user_id)
return render_template('workouts_list.html', workouts=workouts_data)
def get_workouts_for_user(user_id):
workouts_data = []
workouts = Workout.query.filter_by(user_id=user_id).all()
for workout in workouts:
cadence_readings = CadenceReading.query.filter_by(
workout_id=workout.id).all()
if cadence_readings:
start_time = min(
reading.created_at for reading in cadence_readings)
end_time = max(
reading.created_at for reading in cadence_readings)
duration = end_time - start_time
if duration >= timedelta(hours=1):
duration_str = str(duration)
else:
duration_str = str(duration).split('.')[0]
average_rpm = sum(
reading.rpm for reading in cadence_readings) / len(cadence_readings)
workouts_data.append({
'id': workout.id,
'user_id': user_id,
'start_time': start_time.strftime('%Y-%m-%d %H:%M:%S'),
'duration': duration_str,
'average_rpm': int(average_rpm)
})
return workouts_data
if __name__ == '__main__':
app.run(debug=True)