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)

This commit is contained in:
Peter Stockings
2023-08-20 23:42:00 +10:00
parent ec35b78afd
commit 0cb737e1b8
4 changed files with 90 additions and 47 deletions

19
app.py
View File

@@ -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/<int:person_id>/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/<int:person_id>/workout/<int:workout_id>/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():

25
db.py
View File

@@ -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

View File

@@ -0,0 +1,43 @@
<div class="w-full md:w-1/3 px-3 mb-6 md:mb-0">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-state">
Exercise
</label>
<div class="relative">
<div class="w-full">
<select data-te-select-init data-te-select-filter="true" data-te-select-size="lg" name="exercise_id"
class="block appearance-none w-full bg-gray-200 border border-gray-200 text-gray-700 py-3 px-4 pr-8 rounded leading-tight focus:outline-none focus:bg-white focus:border-gray-500"
hx-get="{{ url_for('get_most_recent_topset_for_exercise', person_id=person_id, workout_id=workout_id) }}"
hx-target="#new-set-workout-{{ workout_id }}" hx-swap="innerHTML" {% if has_value==True %} _="init js(me)
te.Select.getOrCreateInstance(me).setValue('{{ exercise_id | safe }}')
end" {% else %} _="init js(me)
te.Select.getOrCreateInstance(me)
end" {% endif %}>
{% for e in exercises %}
<option value="{{ e.exercise_id}}">{{
e.name
}}</option>
{% endfor %}
</select>
</div>
</div>
</div>
<div class="w-full md:w-1/3 px-3 mb-6 md:mb-0">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-city">
Reps
</label>
<input
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="number" name="repetitions" {% if has_value==True %} value="{{ repetitions }}" {% endif %}>
</div>
<div class="w-full md:w-1/3 px-3 mb-6 md:mb-0">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-zip">
Weight
</label>
<input
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-zip" type="number" name="weight" step="any" {% if has_value==True %} value="{{ weight }}" {% endif %}>
</div>

View File

@@ -103,51 +103,11 @@
then call _hyperscript.processNode(#notifications-container)
then reset() me">
<div class="flex flex-wrap -mx-3 mb-2">
<div class="w-full md:w-1/3 px-3 mb-6 md:mb-0">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2"
for="grid-state">
Exercise
</label>
<div class="relative">
<div class="w-full">
<select data-te-select-init data-te-select-filter="true" data-te-select-size="lg"
name="exercise_id"
class="block appearance-none w-full bg-gray-200 border border-gray-200 text-gray-700 py-3 px-4 pr-8 rounded leading-tight focus:outline-none focus:bg-white focus:border-gray-500"
_="init js(me)
te.Select.getOrCreateInstance(me)
end">
{% for e in workout['Exercises'] %}
<option value="{{ e['ExerciseId'] }}">{{
e['Name']
}}</option>
{% endfor %}
</select>
</div>
</div>
</div>
<div class="w-full md:w-1/3 px-3 mb-6 md:mb-0">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2"
for="grid-city">
Reps
</label>
<input
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="number" name="repetitions">
</div>
<div class="w-full md:w-1/3 px-3 mb-6 md:mb-0">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2"
for="grid-zip">
Weight
</label>
<input
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-zip" type="number" name="weight" step="any">
</div>
<div class="flex flex-wrap -mx-3 mb-2" id="new-set-workout-{{ workout['WorkoutId'] }}">
{{ render_partial('partials/new_set_form.html', person_id=workout['PersonId'],
workout_id=workout['WorkoutId'],
exercises=exercises,
has_value=False) }}
</div>
<div class="grid grid-cols-1 sm:grid-cols-2 gap-4 pt-2 px-0 sm:px-2">
<button