From b5493e627cde49f06c5d9cd680a1ec76dea6eb48 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Mon, 4 Nov 2024 22:33:08 +1100 Subject: [PATCH] Fix tags, may have increased load on database while only using one query to fetch workout, topsets, and all tags --- app.py | 4 ++-- db.py | 29 ++++++++++++++++------- features/workout.py | 25 +++++++++++-------- templates/partials/workout_tags.html | 11 ++++----- templates/partials/workout_tags_list.html | 6 +++-- templates/workout.html | 6 ++--- 6 files changed, 48 insertions(+), 33 deletions(-) diff --git a/app.py b/app.py index 4fc41ca..12e94e1 100644 --- a/app.py +++ b/app.py @@ -376,8 +376,8 @@ def get_workout_note(person_id, workout_id): @ app.route("/person//workout//tag/add", methods=['POST']) def add_tag_to_workout(person_id, workout_id): tags_id = [int(i) for i in request.form.getlist('tag_id')] - workout_tags = db.add_tag_for_workout(workout_id, tags_id) - return render_template('partials/workout_tags_list.html', workout_tags=workout_tags) + tags = db.add_tag_for_workout(workout_id, tags_id) + return render_template('partials/workout_tags_list.html', tags=tags) @ app.route("/person//workout//tag/new", methods=['POST']) diff --git a/db.py b/db.py index 98a5b54..ad52bf2 100644 --- a/db.py +++ b/db.py @@ -342,14 +342,24 @@ class DataBase(): note, person_id, workout_id], commit=True) def add_tag_for_workout(self, workout_id, tags_id): - # First, delete tags that are not in the new selection - self.execute( - """ - DELETE FROM workout_tag - WHERE workout_id = %s AND tag_id NOT IN %s - """, - [workout_id, tuple(tags_id)], commit=True - ) + # If tags_id is not empty, delete tags that are not in the new selection + if tags_id: + self.execute( + """ + DELETE FROM workout_tag + WHERE workout_id = %s AND tag_id NOT IN %s + """, + [workout_id, tuple(tags_id)], commit=True + ) + else: + # If tags_id is empty, delete all tags for this workout + self.execute( + """ + DELETE FROM workout_tag + WHERE workout_id = %s + """, + [workout_id], commit=True + ) # Then, attempt to insert the new tags for tag_id in tags_id: @@ -368,7 +378,8 @@ class DataBase(): T.tag_id AS "tag_id", T.person_id AS "person_id", T.name AS "tag_name", - T.filter AS "tag_filter" + T.filter AS "tag_filter", + TRUE AS "is_selected" FROM Workout_Tag WT LEFT JOIN Tag T ON WT.tag_id=T.tag_id WHERE WT.workout_id=%s""", [workout_id]) diff --git a/features/workout.py b/features/workout.py index fa5dce4..221cbdd 100644 --- a/features/workout.py +++ b/features/workout.py @@ -18,17 +18,19 @@ class Workout: t.repetitions, t.weight, tag.tag_id, - tag.name AS tag_name + tag.name AS tag_name, + tag.filter as tag_filter, + CASE WHEN wt.workout_id IS NOT NULL THEN TRUE ELSE FALSE END AS is_selected FROM workout w - JOIN + LEFT JOIN topset t ON w.workout_id = t.workout_id - JOIN + LEFT JOIN exercise e ON t.exercise_id = e.exercise_id LEFT JOIN workout_tag wt ON w.workout_id = wt.workout_id LEFT JOIN - tag ON wt.tag_id = tag.tag_id + tag ON TRUE -- Join to get all tags WHERE w.person_id = %s AND w.workout_id = %s; @@ -37,8 +39,6 @@ class Workout: if not data: return {"error": "Workout not found"}, 404 - - exercises = self.execute("SELECT exercise_id, name FROM exercise;") # Initialize workout dictionary workout_data = { @@ -48,7 +48,6 @@ class Workout: "note": data[0]["note"], "tags": [], "top_sets": [], - "exercises": exercises } # Initialize helpers for tracking unique tags and topsets @@ -57,15 +56,21 @@ class Workout: # Process each row and add tags and topsets to workout_data for row in data: - # Add unique tags + # Add all unique tags with is_selected property tag_id = row["tag_id"] if tag_id and tag_id not in tag_set: - workout_data["tags"].append({"tag_id": tag_id, "tag_name": row["tag_name"]}) + workout_data["tags"].append({ + "tag_id": tag_id, + "tag_name": row["tag_name"], + "tag_filter": row["tag_filter"], + "person_id": person_id, + "is_selected": row["is_selected"] + }) tag_set.add(tag_id) # Add unique topsets based on topset_id topset_id = row["topset_id"] - if topset_id not in topsets_seen: + if topset_id and topset_id not in topsets_seen: workout_data["top_sets"].append({ "topset_id": topset_id, "exercise_id": row["exercise_id"], diff --git a/templates/partials/workout_tags.html b/templates/partials/workout_tags.html index e1b34f6..bb010af 100644 --- a/templates/partials/workout_tags.html +++ b/templates/partials/workout_tags.html @@ -1,7 +1,6 @@
- {{ render_partial('partials/workout_tags_list.html', workout_tags=workout_tags) - }} + {{ render_partial('partials/workout_tags_list.html', tags=tags)}}
+