From 3dcc61005ee5af609c6a670d52f1308ed567ff73 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Sun, 20 Nov 2022 14:14:04 +1100 Subject: [PATCH] Convert edit/add/remove people form in settings to htmx driven and trigger event to refresh list of people and workout count on insert/update/remove person --- app.py | 91 +++++++++++++++++++++++++++-- db.py | 87 ++++++++++++++------------- templates/base.html | 23 ++------ templates/partials/people_link.html | 16 +++++ templates/settings.html | 18 +++--- 5 files changed, 161 insertions(+), 74 deletions(-) create mode 100644 templates/partials/people_link.html diff --git a/app.py b/app.py index cdc792d..5163cfe 100644 --- a/app.py +++ b/app.py @@ -19,6 +19,12 @@ def dashboard(): return render_template('index.html', model=people_and_exercise_rep_maxes) +@ app.route("/person/list", methods=['GET']) +def get_person_list(): + people = db.get_people_and_workout_count(-1) + return render_template('partials/people_link.html', people=people) + + @ app.route("/person/") @ validate_person def get_person(person_id): @@ -216,14 +222,91 @@ def delete_topset(person_id, workout_id, topset_id): @ app.route("/person", methods=['POST']) def create_person(): name = request.form.get("name") - db.create_person(name) - return redirect(url_for('settings')) + new_person_id = db.create_person(name) + return f""" + + + { name } + + + + Edit + + + Remove + + + + """, 200, {"HX-Trigger": "updatedPeople"} -@ app.route("/person//delete", methods=['GET', 'POST']) +@ app.route("/person//delete", methods=['DELETE']) def delete_person(person_id): db.delete_person(person_id) - return redirect(url_for('settings')) + return "", 200, {"HX-Trigger": "updatedPeople"} + + +@ app.route("/person//edit_form", methods=['GET']) +def get_person_edit_form(person_id): + person = db.get_person(person_id) + return f""" + + + + + + + Update + + + Cancel + + + + """ + + +@ app.route("/person//name", methods=['PUT']) +def update_person_name(person_id): + new_name = request.form.get("name") + db.update_person_name(person_id, new_name) + return f""" + + + {new_name} + + + + Edit + + + Remove + + + + """, 200, {"HX-Trigger": "updatedPeople"} + + +@ app.route("/person//name", methods=['GET']) +def get_person_name(person_id): + person = db.get_person(person_id) + return f""" + + + {person['PersonName']} + + + + Edit + + + Remove + + + + """ @ app.route("/exercise", methods=['POST']) diff --git a/db.py b/db.py index 4721b43..28d11d0 100644 --- a/db.py +++ b/db.py @@ -65,8 +65,9 @@ class DataBase(): return person def create_person(self, name): - self.execute('INSERT INTO Person (Name) VALUES (%s)', - [name], commit=True) + new_person = self.execute('INSERT INTO Person (Name) VALUES (%s) RETURNING PersonId AS "PersonId"', [ + name], commit=True, one=True) + return new_person['PersonId'] def delete_person(self, person_id): self.execute('DELETE FROM TopSet WHERE WorkoutId IN (SELECT WorkoutId FROM Workout WHERE PersonId=%s)', [ @@ -76,9 +77,13 @@ class DataBase(): self.execute('DELETE FROM Person WHERE PersonId=%s', [person_id], commit=True) + def update_person_name(self, person_id, name): + self.execute('UPDATE Person SET Name=%s WHERE PersonId=%s', [ + name, person_id], commit=True) + def is_valid_workout(self, person_id, workout_id): workout = self.execute('SELECT W.WorkoutId AS "WorkoutId" FROM Person P, Workout W WHERE P.PersonId=W.PersonId AND P.PersonId=%s AND W.WorkoutId=%s LIMIT 1', [ - person_id, workout_id], one=True) + person_id, workout_id], one=True) return workout def is_valid_topset(self, person_id, workout_id, topset_id): @@ -121,12 +126,12 @@ class DataBase(): return self.execute(""" SELECT P.PersonId AS "PersonId", - P.Name AS "Name", - COUNT(W.WorkoutId) AS "NumberOfWorkouts", - CASE P.PersonId - WHEN %s - THEN 1 - ELSE 0 + P.Name AS "Name", + COUNT(W.WorkoutId) AS "NumberOfWorkouts", + CASE P.PersonId + WHEN %s + THEN 1 + ELSE 0 END "IsActive" FROM Person P LEFT JOIN Workout W ON P.PersonId = W.PersonId @@ -141,15 +146,15 @@ class DataBase(): def get_person(self, person_id): topsets = self.execute(""" - SELECT - P.PersonId AS "PersonId", - P.Name AS "PersonName", - W.WorkoutId AS "WorkoutId", - W.StartDate AS "StartDate", - T.TopSetId AS "TopSetId", + SELECT + P.PersonId AS "PersonId", + P.Name AS "PersonName", + W.WorkoutId AS "WorkoutId", + W.StartDate AS "StartDate", + T.TopSetId AS "TopSetId", E.ExerciseId AS "ExerciseId", - E.Name AS "ExerciseName", - T.Repetitions AS "Repetitions", + E.Name AS "ExerciseName", + T.Repetitions AS "Repetitions", T.Weight AS "Weight" FROM Person P LEFT JOIN Workout W ON P.PersonId=W.PersonId @@ -167,15 +172,15 @@ class DataBase(): def get_workout(self, person_id, workout_id): topsets = self.execute(""" - SELECT - P.PersonId AS "PersonId", - P.Name AS "PersonName", - W.WorkoutId AS "WorkoutId", - W.StartDate AS "StartDate", - T.TopSetId AS "TopSetId", - E.ExerciseId AS "ExerciseId", - E.Name AS "ExerciseName", - T.Repetitions AS "Repetitions", + SELECT + P.PersonId AS "PersonId", + P.Name AS "PersonName", + W.WorkoutId AS "WorkoutId", + W.StartDate AS "StartDate", + T.TopSetId AS "TopSetId", + E.ExerciseId AS "ExerciseId", + E.Name AS "ExerciseName", + T.Repetitions AS "Repetitions", T.Weight AS "Weight" FROM Person P LEFT JOIN Workout W ON P.PersonId=W.PersonId @@ -195,15 +200,15 @@ class DataBase(): def get_topset(self, person_id, workout_id, topset_id): topset = self.execute(""" - SELECT - P.PersonId AS "PersonId", - P.Name AS "PersonName", - W.WorkoutId AS "WorkoutId", - W.StartDate AS "StartDate", - T.TopSetId AS "TopSetId", - E.ExerciseId AS "ExerciseId", - E.Name AS "ExerciseName", - T.Repetitions AS "Repetitions", + SELECT + P.PersonId AS "PersonId", + P.Name AS "PersonName", + W.WorkoutId AS "WorkoutId", + W.StartDate AS "StartDate", + T.TopSetId AS "TopSetId", + E.ExerciseId AS "ExerciseId", + E.Name AS "ExerciseName", + T.Repetitions AS "Repetitions", T.Weight AS "Weight" FROM Person P INNER JOIN Workout W ON P.PersonId=W.PersonId @@ -228,15 +233,15 @@ class DataBase(): def get_all_topsets(self): all_topsets = self.execute(""" - SELECT - P.PersonId AS "PersonId", - P.Name AS "PersonName", - W.WorkoutId AS "WorkoutId", - W.StartDate AS "StartDate", + SELECT + P.PersonId AS "PersonId", + P.Name AS "PersonName", + W.WorkoutId AS "WorkoutId", + W.StartDate AS "StartDate", T.TopSetId AS "TopSetId", E.ExerciseId AS "ExerciseId", E.Name AS "ExerciseName", - T.Repetitions AS "Repetitions", + T.Repetitions AS "Repetitions", T.Weight AS "Weight", round((100 * T.Weight)/(101.3-2.67123 * T.Repetitions),0)::numeric::integer AS "Estimated1RM" FROM Person P diff --git a/templates/base.html b/templates/base.html index 2f4c1e0..154ddd6 100644 --- a/templates/base.html +++ b/templates/base.html @@ -85,25 +85,10 @@ Dashboard -
    - - {% for p in get_list_of_people_and_workout_count() %} -
  • - - - - - - {{ p['Name']}} - {{ - p['NumberOfWorkouts'] }} - -
  • - {% endfor %} +
      + {{ render_partial('partials/people_link.html', + people=get_list_of_people_and_workout_count()) }}
    diff --git a/templates/partials/people_link.html b/templates/partials/people_link.html new file mode 100644 index 0000000..3167cee --- /dev/null +++ b/templates/partials/people_link.html @@ -0,0 +1,16 @@ +{% for p in people %} +
  • + + + + + + {{ p['Name']}} + {{ + p['NumberOfWorkouts'] }} + +
  • +{% endfor %} \ No newline at end of file diff --git a/templates/settings.html b/templates/settings.html index 02e85cb..1e7fc42 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -17,10 +17,6 @@ - - + {% for p in people %} -
    - # - Name @@ -30,17 +26,19 @@
    - {{ loop.index }} - {{ p['Name'] }} - + Edit + + Remove @@ -54,7 +52,7 @@ -
    +