Return number of workouts for users and average/max rpm for workouts
This commit is contained in:
14
app.py
14
app.py
@@ -26,6 +26,7 @@ class User(db.Model):
|
|||||||
__tablename__ = 'users'
|
__tablename__ = 'users'
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
name = db.Column(db.String(255), nullable=False)
|
name = db.Column(db.String(255), nullable=False)
|
||||||
|
workouts = db.relationship('Workout', backref='user', lazy=True)
|
||||||
|
|
||||||
|
|
||||||
class Workout(db.Model):
|
class Workout(db.Model):
|
||||||
@@ -57,7 +58,8 @@ def users():
|
|||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
# get a list of all users in the database
|
# get a list of all users in the database
|
||||||
users = User.query.all()
|
users = User.query.all()
|
||||||
users_list = [{'id': user.id, 'name': user.name} for user in users]
|
users_list = [{'id': user.id, 'name': user.name,
|
||||||
|
'workouts': len(user.workouts)} for user in users]
|
||||||
return jsonify(users_list), 200
|
return jsonify(users_list), 200
|
||||||
|
|
||||||
elif request.method == 'POST':
|
elif request.method == 'POST':
|
||||||
@@ -105,11 +107,19 @@ def create_workout(user_id):
|
|||||||
duration_str = str(duration)
|
duration_str = str(duration)
|
||||||
else:
|
else:
|
||||||
duration_str = str(duration).split('.')[0]
|
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({
|
workouts_data.append({
|
||||||
'id': workout.id,
|
'id': workout.id,
|
||||||
'started_at': start_time.strftime('%a %b %d %Y %H:%M:%S'),
|
'started_at': start_time.strftime('%a %b %d %Y %H:%M:%S'),
|
||||||
'finished_at': end_time.strftime('%a %b %d %Y %H:%M:%S'),
|
'finished_at': end_time.strftime('%a %b %d %Y %H:%M:%S'),
|
||||||
'duration': duration_str
|
'duration': duration_str,
|
||||||
|
'avg_rpm': int(avg_rpm),
|
||||||
|
'max_rpm': max_rpm
|
||||||
})
|
})
|
||||||
return jsonify({'workouts': workouts_data}), 200
|
return jsonify({'workouts': workouts_data}), 200
|
||||||
|
|
||||||
|
|||||||
@@ -48,7 +48,6 @@
|
|||||||
<svg id="graph" class="bg-white shadow-md"></svg>
|
<svg id="graph" class="bg-white shadow-md"></svg>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<img src="http://127.0.0.1:5000/user/1/workout/2" alt="No image">
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
|
|||||||
Reference in New Issue
Block a user