diff --git a/app.py b/app.py index 8d72aa5..0abb525 100644 --- a/app.py +++ b/app.py @@ -433,6 +433,11 @@ def get_stats_for_person(person_id): stats = db.stats.fetch_stats_for_person(person_id) return render_template('partials/stats.html', stats=stats) +@ app.route("/person//workout/", methods=['GET']) +def show_workout(person_id, workout_id): + view_model = db.workout.get(person_id, workout_id) + return render_template('workout.html', **view_model) + @app.teardown_appcontext def closeConnection(exception): diff --git a/db.py b/db.py index 77f479a..796f066 100644 --- a/db.py +++ b/db.py @@ -8,6 +8,7 @@ from urllib.parse import urlparse from flask import g from features.calendar import Calendar from features.stats import Stats +from features.workout import Workout from utils import count_prs_over_time, get_all_exercises_from_topsets, get_exercise_graph_model, get_stats_from_topsets, get_topsets_for_person, get_weekly_pr_graph_model, get_workout_counts, get_workouts @@ -15,6 +16,7 @@ class DataBase(): def __init__(self, app): self.calendar = Calendar(self.execute) self.stats = Stats(self.execute) + self.workout = Workout(self.execute) db_url = urlparse(os.environ['DATABASE_URL']) # if db_url is null then throw error if not db_url: diff --git a/features/workout.py b/features/workout.py new file mode 100644 index 0000000..fa5dce4 --- /dev/null +++ b/features/workout.py @@ -0,0 +1,78 @@ +from flask import jsonify + + +class Workout: + def __init__(self, db_connection_method): + self.execute = db_connection_method + + def get(self, person_id, workout_id): + query = """ + SELECT + w.workout_id, + w.person_id, + w.start_date, + w.note, + t.topset_id, + e.exercise_id, + e.name AS exercise_name, + t.repetitions, + t.weight, + tag.tag_id, + tag.name AS tag_name + FROM + workout w + JOIN + topset t ON w.workout_id = t.workout_id + 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 + WHERE + w.person_id = %s + AND w.workout_id = %s; + """ + data = self.execute(query, [person_id, workout_id]) + + if not data: + return {"error": "Workout not found"}, 404 + + exercises = self.execute("SELECT exercise_id, name FROM exercise;") + + # Initialize workout dictionary + workout_data = { + "workout_id": data[0]["workout_id"], + "person_id": data[0]["person_id"], + "start_date": data[0]["start_date"], + "note": data[0]["note"], + "tags": [], + "top_sets": [], + "exercises": exercises + } + + # Initialize helpers for tracking unique tags and topsets + tag_set = set() + topsets_seen = set() + + # Process each row and add tags and topsets to workout_data + for row in data: + # Add unique tags + 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"]}) + tag_set.add(tag_id) + + # Add unique topsets based on topset_id + topset_id = row["topset_id"] + if topset_id not in topsets_seen: + workout_data["top_sets"].append({ + "topset_id": topset_id, + "exercise_id": row["exercise_id"], + "exercise_name": row["exercise_name"], + "repetitions": row["repetitions"], + "weight": row["weight"] + }) + topsets_seen.add(topset_id) + + return workout_data diff --git a/templates/workout.html b/templates/workout.html new file mode 100644 index 0000000..cce5399 --- /dev/null +++ b/templates/workout.html @@ -0,0 +1,108 @@ +{% extends 'base.html' %} + +{% block content %} + +
+
+ +
+ +
+
+
+
+

{{ person_name }}

+ + {{ render_partial('partials/workout_tags.html', person_id=person_id, + workout_id=workout_id, + person_tags=person_tags, workout_tags=workout_tags, + selected_workout_tag_ids=selected_workout_tag_ids) }} + +
+ +
+
+ {{ render_partial('partials/start_date.html', person_id=person_id, + workout_id=workout_id, + start_date=start_date) }} + + + {{ render_partial('partials/workout_note.html', person_id=person_id, + workout_id=workout_id, + note=note) }} + +
+ +
+ + +
+ +
+ +
+ + + + + + + + + + {% for top_set in top_sets %} + {{ render_partial('partials/topset.html', person_id=person_id, + workout_id=workout_id, **top_set) }} + {% endfor %} + +
+ Exercise + Top Set +
+ + {% if top_sets|length == 0 %} + + {% endif %} +
+ +
+ {{ render_partial('partials/new_set_form.html', person_id=person_id, + workout_id=workout_id, + exercises=exercises, + has_value=False) }} +
+
+ +
+ + + +
+ + +
+
+ + {% endblock %} \ No newline at end of file