diff --git a/app.py b/app.py index fae7c05..578eef3 100644 --- a/app.py +++ b/app.py @@ -9,6 +9,7 @@ from routes.auth import auth, get_person_by_id from routes.changelog import changelog_bp from routes.calendar import calendar_bp # Import the new calendar blueprint from routes.notes import notes_bp # Import the new notes blueprint +from routes.workout import workout_bp # Import the new workout blueprint from extensions import db from utils import convert_str_to_date, generate_plot from flask_htmx import HTMX @@ -38,6 +39,7 @@ app.register_blueprint(auth, url_prefix='/auth') app.register_blueprint(changelog_bp, url_prefix='/changelog') app.register_blueprint(calendar_bp) # Register the calendar blueprint app.register_blueprint(notes_bp) # Register the notes blueprint +app.register_blueprint(workout_bp) # Register the workout blueprint @app.after_request def response_minify(response): @@ -131,99 +133,6 @@ def person_overview(person_id): return render_template('person_overview.html', **render_args), 200, {"HX-Push-Url": url_for('person_overview', person_id=person_id, min_date=min_date, max_date=max_date, exercise_id=selected_exercise_ids), "HX-Trigger": "refreshStats"} - -# Route moved to routes/notes.py - - -@ app.route("/person//workout", methods=['POST']) -def create_workout(person_id): - new_workout_id = db.create_workout(person_id) - workout = db.get_workout(person_id, new_workout_id) - (person_tags, workout_tags, selected_workout_tag_ids) = db.get_workout_tags( - person_id, new_workout_id) - - view_model = db.workout.get(person_id, new_workout_id) - return render_block(app.jinja_env, 'workout.html', 'content', **view_model) - -@ app.route("/person//workout//delete", methods=['GET']) -@ validate_workout -def delete_workout(person_id, workout_id): - db.delete_workout(workout_id) - return redirect(url_for('calendar.get_calendar', person_id=person_id)) - #return "", 200, {"HX-Trigger": "updatedPeople", "HX-Push-Url": url_for('calendar.get_calendar', person_id=person_id)} - - -@ app.route("/person//workout//start_date_edit_form", methods=['GET']) -@ validate_workout -def get_workout_start_date_edit_form(person_id, workout_id): - workout = db.get_workout(person_id, workout_id) - return render_template('partials/start_date.html', person_id=person_id, workout_id=workout_id, start_date=workout['start_date'], is_edit=True) - - -@ app.route("/person//workout//start_date", methods=['PUT']) -@ validate_workout -def update_workout_start_date(person_id, workout_id): - new_start_date = request.form.get('start-date') - db.update_workout_start_date(workout_id, new_start_date) - return render_template('partials/start_date.html', person_id=person_id, workout_id=workout_id, start_date=convert_str_to_date(new_start_date, '%Y-%m-%d')) - - -@ app.route("/person//workout//start_date", methods=['GET']) -@ validate_workout -def get_workout_start_date(person_id, workout_id): - workout = db.get_workout(person_id, workout_id) - return render_template('partials/start_date.html', person_id=person_id, workout_id=workout_id, start_date=workout['start_date']) - - -@ app.route("/person//workout//topset/", methods=['GET']) -@ validate_topset -def get_topset(person_id, workout_id, topset_id): - topset = db.get_topset(topset_id) - return render_template('partials/topset.html', person_id=person_id, workout_id=workout_id, topset_id=topset_id, exercise_id=topset['exercise_id'], exercise_name=topset['exercise_name'], repetitions=topset['repetitions'], weight=topset['weight']) - - -@ app.route("/person//workout//topset//edit_form", methods=['GET']) -@ validate_topset -def get_topset_edit_form(person_id, workout_id, topset_id): - exercises = db.get_all_exercises() - topset = db.get_topset(topset_id) - return render_template('partials/topset.html', person_id=person_id, workout_id=workout_id, topset_id=topset_id, exercises=exercises, exercise_id=topset['exercise_id'], exercise_name=topset['exercise_name'], repetitions=topset['repetitions'], weight=topset['weight'], is_edit=True) - - -@ app.route("/person//workout//topset", methods=['POST']) -@ validate_workout -def create_topset(person_id, workout_id): - exercise_id = request.form.get("exercise_id") - repetitions = request.form.get("repetitions") - weight = request.form.get("weight") - - new_topset_id = db.create_topset( - workout_id, exercise_id, repetitions, weight) - exercise = db.get_exercise(exercise_id) - - return render_template('partials/topset.html', person_id=person_id, workout_id=workout_id, topset_id=new_topset_id, exercise_id=exercise_id, exercise_name=exercise['name'], repetitions=repetitions, weight=weight), 200, {"HX-Trigger": "topsetAdded"} - - -@ app.route("/person//workout//topset/", methods=['PUT']) -@ validate_workout -def update_topset(person_id, workout_id, topset_id): - exercise_id = request.form.get("exercise_id") - repetitions = request.form.get("repetitions") - weight = request.form.get("weight") - - db.update_topset(exercise_id, repetitions, weight, topset_id) - exercise = db.get_exercise(exercise_id) - - return render_template('partials/topset.html', person_id=person_id, workout_id=workout_id, topset_id=topset_id, exercise_name=exercise['name'], repetitions=repetitions, weight=weight) - - -@ app.route("/person//workout//topset//delete", methods=['DELETE']) -@ validate_topset -def delete_topset(person_id, workout_id, topset_id): - db.delete_topset(topset_id) - return "" - - @ app.route("/person", methods=['POST']) def create_person(): name = request.form.get("name") @@ -328,38 +237,6 @@ def delete_tag(tag_id): db.delete_tag_for_dashboard(tag_id) return redirect(url_for('dashboard') + tag_filter) -# Routes moved to routes/notes.py - - -@ 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')] - 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']) -def create_new_tag_for_workout(person_id, workout_id): - tag_name = request.form.get('tag_name') - workout_tags = db.create_tag_for_workout(person_id, workout_id, tag_name) - 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) - - 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, exercise_id=exercise_id) - - (repetitions, weight, exercise_name) = topset - return render_template('partials/new_set_form.html', person_id=person_id, workout_id=workout_id, exercise_id=exercise_id, exercise_name=exercise_name, repetitions=repetitions, weight=weight) - @ app.route("/person//exercise//sparkline", methods=['GET']) def get_exercise_progress_for_user(person_id, exercise_id): min_date = convert_str_to_date(request.args.get( @@ -399,14 +276,6 @@ def get_people_graphs(): return render_template('partials/people_graphs.html', graphs=graphs, refresh_url=request.full_path) - -@ app.route("/person//workout/", methods=['GET']) -def show_workout(person_id, workout_id): - view_model = db.workout.get(person_id, workout_id) - if htmx: - return render_block(app.jinja_env, 'workout.html', 'content', **view_model) - return render_template('workout.html', **view_model) - @app.route("/exercises/get") def get_exercises(): query = request.args.get('query') diff --git a/db.py b/db.py index 8e6e6ce..709dd50 100644 --- a/db.py +++ b/db.py @@ -10,7 +10,6 @@ from features.exercises import Exercises from features.people_graphs import PeopleGraphs from features.person_overview import PersonOverview from features.stats import Stats -from features.workout import Workout from features.sql_explorer import SQLExplorer from features.dashboard import Dashboard from utils import get_exercise_graph_model @@ -19,7 +18,6 @@ from utils import get_exercise_graph_model class DataBase(): def __init__(self, app=None): self.stats = Stats(self.execute) - self.workout = Workout(self.execute) self.exercises = Exercises(self.execute) self.sql_explorer = SQLExplorer(self.execute) self.person_overview = PersonOverview(self.execute) diff --git a/features/workout.py b/features/workout.py deleted file mode 100644 index bfd7e0a..0000000 --- a/features/workout.py +++ /dev/null @@ -1,88 +0,0 @@ -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, - p.name AS person_name, - 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, - tag.filter as tag_filter, - CASE WHEN wt.workout_id IS NOT NULL THEN TRUE ELSE FALSE END AS is_selected - FROM - workout w - LEFT JOIN - person p ON w.person_id = p.person_id - LEFT JOIN - topset t ON w.workout_id = t.workout_id - 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 TRUE -- Join to get all tags - WHERE - w.person_id = %s - AND w.workout_id = %s - ORDER BY t.topset_id ASC; - """ - data = self.execute(query, [person_id, workout_id]) - - if not data: - return {"error": "Workout not found"}, 404 - - # Initialize workout dictionary - workout_data = { - "workout_id": data[0]["workout_id"], - "person_id": data[0]["person_id"], - "person_name": data[0]["person_name"], - "start_date": data[0]["start_date"], - "note": data[0]["note"], - "tags": [], - "top_sets": [], - } - - # 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 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"], - "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 and 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/routes/workout.py b/routes/workout.py new file mode 100644 index 0000000..732643b --- /dev/null +++ b/routes/workout.py @@ -0,0 +1,244 @@ +from flask import Blueprint, render_template, redirect, url_for, request, current_app +from jinja2_fragments import render_block +from flask_htmx import HTMX +from extensions import db +from decorators import validate_workout, validate_topset +from utils import convert_str_to_date +from collections import defaultdict # Import defaultdict + +workout_bp = Blueprint('workout', __name__) +htmx = HTMX() + +# --- Database Helper Function (Moved from features/workout.py) --- + +def _get_workout_view_model(person_id, workout_id): + """Fetches and formats workout data for display.""" + query = """ + SELECT + w.workout_id, + w.person_id, + p.name AS person_name, + 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, + tag.filter as tag_filter, + -- Check if the tag is associated with *this specific workout* + EXISTS ( + SELECT 1 FROM workout_tag wt_check + WHERE wt_check.workout_id = w.workout_id AND wt_check.tag_id = tag.tag_id + ) AS is_selected + FROM + workout w + LEFT JOIN + person p ON w.person_id = p.person_id + LEFT JOIN + topset t ON w.workout_id = t.workout_id + LEFT JOIN + exercise e ON t.exercise_id = e.exercise_id + -- Cross Join with tags relevant to the person to get all possible tags + CROSS JOIN + tag + WHERE + w.person_id = %s AND w.workout_id = %s + -- Ensure we only get tags associated with the person or general tags (person_id IS NULL) + AND (tag.person_id = w.person_id OR tag.person_id IS NULL) + ORDER BY t.topset_id ASC, tag.name ASC; + """ + # Note: The original query had a potential issue joining tags. + # The revised query uses a CROSS JOIN with person-specific tags + # and then checks association using EXISTS for the 'is_selected' flag. + # This ensures all relevant tags for the person are listed, + # and correctly marks those applied to this workout. + + data = db.execute(query, [person_id, workout_id]) + + if not data: + # If workout exists but has no topsets/tags, fetch basic info + basic_info = db.execute( + """SELECT w.workout_id, w.person_id, p.name as person_name, w.start_date, w.note + FROM workout w JOIN person p ON w.person_id = p.person_id + WHERE w.person_id = %s AND w.workout_id = %s""", + [person_id, workout_id], one=True + ) + if not basic_info: + return {"error": "Workout not found"}, 404 # Truly not found + # Workout exists but is empty + return { + "workout_id": basic_info["workout_id"], + "person_id": basic_info["person_id"], + "person_name": basic_info["person_name"], + "start_date": basic_info["start_date"], + "note": basic_info["note"], + "tags": [], # Fetch all person tags separately if needed + "top_sets": [], + } + + + # Initialize workout dictionary using the first row for base details + workout_data = { + "workout_id": data[0]["workout_id"], + "person_id": data[0]["person_id"], + "person_name": data[0]["person_name"], + "start_date": data[0]["start_date"], + "note": data[0]["note"], + "tags": [], + "top_sets": [], + } + + # Use sets to track unique IDs efficiently + tag_ids_seen = set() + topset_ids_seen = set() + + for row in data: + # Process Tags + tag_id = row.get("tag_id") + if tag_id is not None and tag_id not in tag_ids_seen: + workout_data["tags"].append({ + "tag_id": tag_id, + "tag_name": row.get("tag_name"), + "tag_filter": row.get("tag_filter"), + "person_id": person_id, # Assuming tags are person-specific or global + "is_selected": row.get("is_selected", False) + }) + tag_ids_seen.add(tag_id) + + # Process Topsets + topset_id = row.get("topset_id") + if topset_id is not None and topset_id not in topset_ids_seen: + workout_data["top_sets"].append({ + "topset_id": topset_id, + "exercise_id": row.get("exercise_id"), + "exercise_name": row.get("exercise_name"), + "repetitions": row.get("repetitions"), + "weight": row.get("weight") + }) + topset_ids_seen.add(topset_id) + + # Sort tags alphabetically by name for consistent display + workout_data["tags"].sort(key=lambda x: x.get('tag_name', '')) + + return workout_data + + +# --- Routes --- + +@workout_bp.route("/person//workout", methods=['POST']) +def create_workout(person_id): + new_workout_id = db.create_workout(person_id) + # Use the local helper function to get the view model + view_model = _get_workout_view_model(person_id, new_workout_id) + if "error" in view_model: # Handle case where workout creation might fail or is empty + # Decide on appropriate error handling, maybe redirect or show error message + return redirect(url_for('calendar.get_calendar', person_id=person_id)) + return render_block(current_app.jinja_env, 'workout.html', 'content', **view_model) + +@workout_bp.route("/person//workout//delete", methods=['GET']) +@validate_workout +def delete_workout(person_id, workout_id): + db.delete_workout(workout_id) + return redirect(url_for('calendar.get_calendar', person_id=person_id)) + +@workout_bp.route("/person//workout//start_date_edit_form", methods=['GET']) +@validate_workout +def get_workout_start_date_edit_form(person_id, workout_id): + # Fetch only the necessary data (start_date) + workout = db.execute("SELECT start_date FROM workout WHERE workout_id = %s", [workout_id], one=True) + start_date = workout.get('start_date') if workout else None + return render_template('partials/start_date.html', person_id=person_id, workout_id=workout_id, start_date=start_date, is_edit=True) + +@workout_bp.route("/person//workout//start_date", methods=['PUT']) +@validate_workout +def update_workout_start_date(person_id, workout_id): + new_start_date_str = request.form.get('start-date') + db.update_workout_start_date(workout_id, new_start_date_str) + # Convert string back to date for rendering the partial + new_start_date = convert_str_to_date(new_start_date_str, '%Y-%m-%d') + return render_template('partials/start_date.html', person_id=person_id, workout_id=workout_id, start_date=new_start_date) + +@workout_bp.route("/person//workout//start_date", methods=['GET']) +@validate_workout +def get_workout_start_date(person_id, workout_id): + # Fetch only the necessary data (start_date) + workout = db.execute("SELECT start_date FROM workout WHERE workout_id = %s", [workout_id], one=True) + start_date = workout.get('start_date') if workout else None + return render_template('partials/start_date.html', person_id=person_id, workout_id=workout_id, start_date=start_date) + +@workout_bp.route("/person//workout//topset/", methods=['GET']) +@validate_topset +def get_topset(person_id, workout_id, topset_id): + topset = db.get_topset(topset_id) # Keep using db.py for simple gets for now + return render_template('partials/topset.html', person_id=person_id, workout_id=workout_id, topset_id=topset_id, exercise_id=topset.get('exercise_id'), exercise_name=topset.get('exercise_name'), repetitions=topset.get('repetitions'), weight=topset.get('weight')) + +@workout_bp.route("/person//workout//topset//edit_form", methods=['GET']) +@validate_topset +def get_topset_edit_form(person_id, workout_id, topset_id): + exercises = db.get_all_exercises() + topset = db.get_topset(topset_id) + return render_template('partials/topset.html', person_id=person_id, workout_id=workout_id, topset_id=topset_id, exercises=exercises, exercise_id=topset.get('exercise_id'), exercise_name=topset.get('exercise_name'), repetitions=topset.get('repetitions'), weight=topset.get('weight'), is_edit=True) + +@workout_bp.route("/person//workout//topset", methods=['POST']) +@validate_workout +def create_topset(person_id, workout_id): + exercise_id = request.form.get("exercise_id") + repetitions = request.form.get("repetitions") + weight = request.form.get("weight") + new_topset_id = db.create_topset(workout_id, exercise_id, repetitions, weight) + exercise = db.get_exercise(exercise_id) + return render_template('partials/topset.html', person_id=person_id, workout_id=workout_id, topset_id=new_topset_id, exercise_id=exercise_id, exercise_name=exercise.get('name'), repetitions=repetitions, weight=weight), 200, {"HX-Trigger": "topsetAdded"} + +@workout_bp.route("/person//workout//topset/", methods=['PUT']) +@validate_workout +def update_topset(person_id, workout_id, topset_id): + exercise_id = request.form.get("exercise_id") + repetitions = request.form.get("repetitions") + weight = request.form.get("weight") + db.update_topset(exercise_id, repetitions, weight, topset_id) + exercise = db.get_exercise(exercise_id) + return render_template('partials/topset.html', person_id=person_id, workout_id=workout_id, topset_id=topset_id, exercise_name=exercise.get('name'), repetitions=repetitions, weight=weight) + +@workout_bp.route("/person//workout//topset//delete", methods=['DELETE']) +@validate_topset +def delete_topset(person_id, workout_id, topset_id): + db.delete_topset(topset_id) + return "" + +@workout_bp.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')] + tags = db.add_tag_for_workout(workout_id, tags_id) # Keep using db.py for complex tag logic for now + return render_template('partials/workout_tags_list.html', tags=tags) + +@workout_bp.route("/person//workout//tag/new", methods=['POST']) +def create_new_tag_for_workout(person_id, workout_id): + tag_name = request.form.get('tag_name') + workout_tags = db.create_tag_for_workout(person_id, workout_id, tag_name) # Keep using db.py for complex tag logic for now + return render_template('partials/workout_tags_list.html', workout_tags=workout_tags) + +@workout_bp.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) + topset = db.get_most_recent_topset_for_exercise(person_id, exercise_id) # Keep using db.py for now + if not topset: + return render_template('partials/new_set_form.html', person_id=person_id, workout_id=workout_id, exercises=exercises, exercise_id=exercise_id) + (repetitions, weight, exercise_name) = topset + return render_template('partials/new_set_form.html', person_id=person_id, workout_id=workout_id, exercise_id=exercise_id, exercise_name=exercise_name, repetitions=repetitions, weight=weight) + +@workout_bp.route("/person//workout/", methods=['GET']) +def show_workout(person_id, workout_id): + # Use the local helper function to get the view model + view_model = _get_workout_view_model(person_id, workout_id) + if "error" in view_model: + # Decide on appropriate error handling + return redirect(url_for('calendar.get_calendar', person_id=person_id)) # Redirect to calendar on error + if htmx: + return render_block(current_app.jinja_env, 'workout.html', 'content', **view_model) + return render_template('workout.html', **view_model) \ No newline at end of file diff --git a/templates/calendar.html b/templates/calendar.html index aa53077..1c0991a 100644 --- a/templates/calendar.html +++ b/templates/calendar.html @@ -95,7 +95,7 @@ {% for workout in day.workouts %}
{% for set in workout.sets %}
diff --git a/templates/partials/start_date.html b/templates/partials/start_date.html index 1a47e86..45f725f 100644 --- a/templates/partials/start_date.html +++ b/templates/partials/start_date.html @@ -2,7 +2,7 @@ {% if is_edit|default(false, true) == false %} {{ strftime(start_date, "%b %d %Y") }} Edit @@ -20,10 +20,10 @@ + hx-put="{{ url_for('workout.update_workout_start_date', person_id=person_id, workout_id=workout_id) }}"> - Cancel diff --git a/templates/partials/topset.html b/templates/partials/topset.html index 9344da3..9886cee 100644 --- a/templates/partials/topset.html +++ b/templates/partials/topset.html @@ -45,7 +45,7 @@ {% if is_edit|default(false, true) == false %}