From 114b501adfb250d923e5efa6caf1caa8a9c84fb3 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Thu, 30 Mar 2023 11:32:37 +1100 Subject: [PATCH] Set bike per workout and more style changes --- app.py | 26 +++++++++- templates/users.html | 12 ++--- templates/users_and_workouts.html | 11 ++-- templates/users_and_workouts_wrapper.html | 9 ++++ templates/workouts_list.html | 61 +++++++++++++---------- 5 files changed, 76 insertions(+), 43 deletions(-) create mode 100644 templates/users_and_workouts_wrapper.html diff --git a/app.py b/app.py index 50d2631..be6e1e7 100644 --- a/app.py +++ b/app.py @@ -48,8 +48,11 @@ class Workout(db.Model): user_id = db.Column(db.Integer, db.ForeignKey( 'users.id', ondelete='CASCADE'), nullable=False) created_at = db.Column(db.DateTime, nullable=False, default=db.func.now()) + bike_id = db.Column(db.Integer, db.ForeignKey( + 'bikes.id', ondelete='CASCADE'), nullable=False) cadence_readings = db.relationship( 'CadenceReading', backref='workout', lazy=True) + bike = db.relationship('Bike', backref='workouts', lazy=True) class CadenceReading(db.Model): @@ -109,11 +112,13 @@ def workouts(user_id): return render_template('workouts_list.html', workouts=workouts_data) elif request.method == 'POST': + user = User.query.get(user_id) + data = request.json data = data['workout'] # create a new workout - workout = Workout(user_id=user_id) + workout = Workout(user_id=user_id, bike_id=user.bike_id) db.session.add(workout) db.session.commit() @@ -199,6 +204,18 @@ def render_users_and_workouts(): users_data = [] for user in users: workouts = get_workouts_for_user(user.id) + if not workouts: + user_data = { + 'id': user.id, + 'name': user.name, + 'bike_id': user.bike_id, + 'workouts_count': 0, + 'workouts': [], + 'workout_counts_by_week': 0, + 'duration_by_week': 0 + } + users_data.append(user_data) + continue workout_counts_by_week = [] duration_by_week = [] # get start date from last workout @@ -236,6 +253,10 @@ def render_users_and_workouts(): 'duration_by_week': duration_by_week } users_data.append(user_data) + + if htmx: + render_template('users_and_workouts_wrapper.html', + users=users_data, bikes=Bike.query.all()) return render_template('users_and_workouts.html', users=users_data, bikes=Bike.query.all()) @@ -265,7 +286,8 @@ def get_workouts_for_user(user_id): 'duration_minutes': duration.total_seconds() / 60, 'average_rpm': int(average_rpm), 'calories': int(calories), - 'distance': int(distance) + 'distance': int(distance), + 'bike_display_name': workout.bike.display_name }) return workouts_data diff --git a/templates/users.html b/templates/users.html index dac0484..b0ade3c 100644 --- a/templates/users.html +++ b/templates/users.html @@ -1,8 +1,8 @@ -
-
-
+
+
@@ -10,11 +10,11 @@ class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500" id="grid-city" type="text" placeholder="Full name" name="name" required>
-
+
-
+
- {% for b in bikes %} - - {% endfor %} - - +
+
+ +
+ + +
+
- - +
-
+