From 0cb737e1b8d313c3a9b413904ffcff0df7dfddd4 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Sun, 20 Aug 2023 23:42:00 +1000 Subject: [PATCH] When you select an exercise for a new topset autofill the reps/weight feilds with the most recent topset values if possible (I beleive I have introduced a minor defect where adding a new set no longer clears reps/weight feild, but for some reason still clears exercise) --- app.py | 19 +++++++++- db.py | 25 +++++++++++++- templates/partials/new_set_form.html | 43 +++++++++++++++++++++++ templates/partials/workout_modal.html | 50 +++------------------------ 4 files changed, 90 insertions(+), 47 deletions(-) create mode 100644 templates/partials/new_set_form.html diff --git a/app.py b/app.py index 783aabc..9713b87 100644 --- a/app.py +++ b/app.py @@ -173,7 +173,8 @@ def get_workout_modal(person_id, workout_id): workout = db.get_workout(person_id, workout_id) (person_tags, workout_tags, selected_workout_tag_ids) = db.get_workout_tags( person_id, workout_id) - return render_template('partials/workout_modal.html', workout=workout, person_tags=person_tags, workout_tags=workout_tags, selected_workout_tag_ids=selected_workout_tag_ids) + exercises = db.get_all_exercises() + return render_template('partials/workout_modal.html', workout=workout, person_tags=person_tags, workout_tags=workout_tags, selected_workout_tag_ids=selected_workout_tag_ids, exercises=exercises) @ app.route("/person//workout", methods=['POST']) @@ -404,6 +405,22 @@ def create_new_tag_for_workout(person_id, workout_id): return render_template('partials/workout_tags_list.html', workout_tags=workout_tags) +@ app.route("/person//workout//exercise/most_recent_topset_for_exercise", methods=['GET']) +def get_most_recent_topset_for_exercise(person_id, workout_id): + exercise_id = request.args.get('exercise_id', type=int) + exercises = db.get_all_exercises() + + if not exercise_id: + return render_template('partials/new_set_form.html', person_id=person_id, workout_id=workout_id, exercises=exercises, has_value=False) + + topset = db.get_most_recent_topset_for_exercise(person_id, exercise_id) + if not topset: + return render_template('partials/new_set_form.html', person_id=person_id, workout_id=workout_id, exercises=exercises, has_value=False) + + (repetitions, weight) = topset + return render_template('partials/new_set_form.html', person_id=person_id, workout_id=workout_id, exercises=exercises, has_value=True, exercise_id=exercise_id, repetitions=repetitions, weight=weight) + + # # TODO: Remove me, just for testing # @ app.route("/sparkline", methods=['GET']) # def get_sparkline(): diff --git a/db.py b/db.py index 29623a1..80fd4b1 100644 --- a/db.py +++ b/db.py @@ -227,7 +227,6 @@ class DataBase(): 'PersonName': next((t['PersonName'] for t in topsets), 'Unknown'), 'WorkoutId': workout_id, 'StartDate': topsets[0]['StartDate'], - 'Exercises': self.get_exercises(), 'TopSets': [{"TopSetId": t['TopSetId'], "ExerciseId": t['ExerciseId'], "ExerciseName": t['ExerciseName'], "Weight": t['Weight'], "Repetitions": t['Repetitions']} for t in topsets if t['TopSetId'] is not None], 'Note': note } @@ -439,3 +438,27 @@ class DataBase(): selected_workout_tag_ids = [wt['tag_id'] for wt in workout_tags] return (person_tags, workout_tags, selected_workout_tag_ids) + + def get_most_recent_topset_for_exercise(self, person_id, exercise_id): + topset = self.execute(""" + SELECT + t.repetitions, + t.weight + FROM + topset t JOIN workout w ON t.workout_id = w.workout_id + WHERE + w.person_id = %s AND t.exercise_id = %s + ORDER BY + w.start_date DESC + LIMIT 1; + """, [person_id, exercise_id], one=True) + + if not topset: + return None + else: + return (topset['repetitions'], topset['weight']) + + def get_all_exercises(self): + exercises = self.execute( + 'SELECT exercise_id, name FROM exercise') + return exercises diff --git a/templates/partials/new_set_form.html b/templates/partials/new_set_form.html new file mode 100644 index 0000000..eec8be0 --- /dev/null +++ b/templates/partials/new_set_form.html @@ -0,0 +1,43 @@ +
+ +
+ + +
+ +
+
+
+ +
+ + +
+ +
+ + +
\ No newline at end of file diff --git a/templates/partials/workout_modal.html b/templates/partials/workout_modal.html index 146d659..5d5f165 100644 --- a/templates/partials/workout_modal.html +++ b/templates/partials/workout_modal.html @@ -103,51 +103,11 @@ then call _hyperscript.processNode(#notifications-container) then reset() me"> -
-
- -
- - -
- -
-
-
- -
- - -
- -
- - -
+
+ {{ render_partial('partials/new_set_form.html', person_id=workout['PersonId'], + workout_id=workout['WorkoutId'], + exercises=exercises, + has_value=False) }}