Create blueprint for exercises

This commit is contained in:
Peter Stockings
2026-02-08 16:08:30 +11:00
parent ef91dc1fe4
commit 0cd74f7207
10 changed files with 140 additions and 111 deletions

100
app.py
View File

@@ -22,6 +22,7 @@ from routes.endpoints import endpoints_bp # Import the new endpoints blueprint
from routes.export import export_bp # Import the new export blueprint from routes.export import export_bp # Import the new export blueprint
from routes.tags import tags_bp # Import the new tags blueprint from routes.tags import tags_bp # Import the new tags blueprint
from routes.programs import programs_bp # Import the new programs blueprint from routes.programs import programs_bp # Import the new programs blueprint
from routes.exercises import exercises_bp # Import the new exercises blueprint
from extensions import db from extensions import db
from utils import convert_str_to_date from utils import convert_str_to_date
from flask_htmx import HTMX from flask_htmx import HTMX
@@ -71,6 +72,7 @@ app.register_blueprint(endpoints_bp) # Register the endpoints blueprint (prefix
app.register_blueprint(export_bp) # Register the export blueprint (prefix defined in blueprint file) app.register_blueprint(export_bp) # Register the export blueprint (prefix defined in blueprint file)
app.register_blueprint(tags_bp) # Register the tags blueprint (prefix defined in blueprint file) app.register_blueprint(tags_bp) # Register the tags blueprint (prefix defined in blueprint file)
app.register_blueprint(programs_bp) # Register the programs blueprint (prefix defined in blueprint file) app.register_blueprint(programs_bp) # Register the programs blueprint (prefix defined in blueprint file)
app.register_blueprint(exercises_bp) # Register the exercises blueprint
@app.after_request @app.after_request
def response_minify(response): def response_minify(response):
@@ -219,72 +221,8 @@ def get_person_name(person_id):
return render_template('partials/person.html', person_id=person_id, name=name) return render_template('partials/person.html', person_id=person_id, name=name)
@ app.route("/exercise", methods=['POST'])
@login_required
def create_exercise():
name = request.form.get("name")
attribute_ids = request.form.getlist('attribute_ids')
exercise = db.create_exercise(name, attribute_ids)
db.activityRequest.log(current_user.id, 'CREATE_EXERCISE', 'exercise', exercise['exercise_id'], f"Created exercise: {name}")
return render_template('partials/exercise.html',
exercise_id=exercise['exercise_id'],
name=exercise['name'],
attributes=exercise['attributes'])
@ app.route("/exercise/<int:exercise_id>", methods=['GET'])
def get_exercise(exercise_id):
exercise = db.get_exercise(exercise_id)
return render_template('partials/exercise.html',
exercise_id=exercise_id,
name=exercise['name'],
attributes=exercise['attributes'])
@ app.route("/exercise/<int:exercise_id>/edit_form", methods=['GET'])
@login_required
def get_exercise_edit_form(exercise_id):
exercise = db.get_exercise(exercise_id)
all_attributes = db.exercises.get_attributes_by_category()
# Format options for custom_select
formatted_options = {}
ex_attr_ids = [a['attribute_id'] for a in exercise['attributes']]
for cat, attrs in all_attributes.items():
formatted_options[cat] = [
{
"id": a['attribute_id'],
"name": a['name'],
"selected": a['attribute_id'] in ex_attr_ids
} for a in attrs
]
return render_template('partials/exercise.html',
exercise_id=exercise_id,
name=exercise['name'],
attributes=exercise['attributes'],
all_attributes=formatted_options,
is_edit=True)
@ app.route("/exercise/<int:exercise_id>/update", methods=['PUT'])
@login_required
def update_exercise(exercise_id):
new_name = request.form.get('name')
attribute_ids = request.form.getlist('attribute_ids')
exercise = db.update_exercise(exercise_id, new_name, attribute_ids)
db.activityRequest.log(current_user.id, 'UPDATE_EXERCISE', 'exercise', exercise_id, f"Updated exercise: {new_name}")
return render_template('partials/exercise.html',
exercise_id=exercise_id,
name=exercise['name'],
attributes=exercise['attributes'])
""" @ app.route("/exercise/<int:exercise_id>/delete", methods=['DELETE'])
def delete_exercise(exercise_id):
db.delete_exercise(exercise_id)
return "" """
@ app.route("/settings") @ app.route("/settings")
@ login_required @ login_required
@@ -364,41 +302,7 @@ def get_people_graphs():
return render_template('partials/people_graphs.html', graphs=graphs, refresh_url=request.full_path) return render_template('partials/people_graphs.html', graphs=graphs, refresh_url=request.full_path)
@app.route("/exercises/get")
def get_exercises():
query = request.args.get('query')
person_id = request.args.get('person_id', type=int)
exercises = db.exercises.get(query)
return render_template('partials/exercise/exercise_dropdown.html', exercises=exercises, person_id=person_id)
@app.route("/exercise/<int:exercise_id>/edit_name", methods=['GET', 'POST'])
@login_required
def edit_exercise_name(exercise_id):
exercise = db.exercises.get_exercise(exercise_id)
person_id = request.args.get('person_id', type=int)
if request.method == 'GET':
return render_template('partials/exercise/edit_exercise_name.html', exercise=exercise, person_id=person_id)
else:
updated_name = request.form['name']
updated_exercise = db.exercises.update_exercise_name(exercise_id, updated_name)
return render_template('partials/exercise/exercise_list_item.html', exercise=updated_exercise, person_id=person_id)
@app.route("/exercises/add", methods=['POST'])
@login_required
def add_exercise():
exercise_name = request.form['query']
new_exercise = db.exercises.add_exercise(exercise_name)
person_id = request.args.get('person_id', type=int)
return render_template('partials/exercise/exercise_list_item.html', exercise=new_exercise, person_id=person_id)
@ app.route("/exercise/<int:exercise_id>/delete", methods=['DELETE'])
@login_required
@admin_required
def delete_exercise(exercise_id):
exercise = db.get_exercise(exercise_id)
db.exercises.delete_exercise(exercise_id)
db.activityRequest.log(current_user.id, 'DELETE_EXERCISE', 'exercise', exercise_id, f"Deleted exercise: {exercise['name']}")
return ""
@app.teardown_appcontext @app.teardown_appcontext
def closeConnection(exception): def closeConnection(exception):

View File

@@ -1,6 +1,6 @@
from flask import Blueprint, render_template, redirect, url_for, flash, request from flask import Blueprint, render_template, redirect, url_for, flash, request
from werkzeug.security import generate_password_hash, check_password_hash from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import login_user, login_required, logout_user from flask_login import login_user, login_required, logout_user, current_user
from forms.login import LoginForm from forms.login import LoginForm
from forms.signup import SignupForm from forms.signup import SignupForm
from extensions import db from extensions import db

103
routes/exercises.py Normal file
View File

@@ -0,0 +1,103 @@
from flask import Blueprint, render_template, request, url_for
from flask_login import login_required, current_user
from extensions import db
from decorators import admin_required
exercises_bp = Blueprint('exercises', __name__)
@exercises_bp.route("/exercise", methods=['POST'])
@login_required
def create_exercise():
name = request.form.get("name")
attribute_ids = request.form.getlist('attribute_ids')
exercise = db.create_exercise(name, attribute_ids)
db.activityRequest.log(current_user.id, 'CREATE_EXERCISE', 'exercise', exercise['exercise_id'], f"Created exercise: {name}")
return render_template('partials/exercise.html',
exercise_id=exercise['exercise_id'],
name=exercise['name'],
attributes=exercise['attributes'])
@exercises_bp.route("/exercise/<int:exercise_id>", methods=['GET'])
def get_exercise(exercise_id):
exercise = db.get_exercise(exercise_id)
return render_template('partials/exercise.html',
exercise_id=exercise_id,
name=exercise['name'],
attributes=exercise['attributes'])
@exercises_bp.route("/exercise/<int:exercise_id>/edit_form", methods=['GET'])
@login_required
def get_exercise_edit_form(exercise_id):
exercise = db.get_exercise(exercise_id)
all_attributes = db.exercises.get_attributes_by_category()
# Format options for custom_select
formatted_options = {}
ex_attr_ids = [a['attribute_id'] for a in exercise['attributes']]
for cat, attrs in all_attributes.items():
formatted_options[cat] = [
{
"id": a['attribute_id'],
"name": a['name'],
"selected": a['attribute_id'] in ex_attr_ids
} for a in attrs
]
return render_template('partials/exercise.html',
exercise_id=exercise_id,
name=exercise['name'],
attributes=exercise['attributes'],
all_attributes=formatted_options,
is_edit=True)
@exercises_bp.route("/exercise/<int:exercise_id>/update", methods=['PUT'])
@login_required
def update_exercise(exercise_id):
new_name = request.form.get('name')
attribute_ids = request.form.getlist('attribute_ids')
exercise = db.update_exercise(exercise_id, new_name, attribute_ids)
db.activityRequest.log(current_user.id, 'UPDATE_EXERCISE', 'exercise', exercise_id, f"Updated exercise: {new_name}")
return render_template('partials/exercise.html',
exercise_id=exercise_id,
name=exercise['name'],
attributes=exercise['attributes'])
@exercises_bp.route("/exercises/get")
def get_exercises():
query = request.args.get('query')
person_id = request.args.get('person_id', type=int)
exercises = db.exercises.get(query)
return render_template('partials/exercise/exercise_dropdown.html', exercises=exercises, person_id=person_id)
@exercises_bp.route("/exercise/<int:exercise_id>/edit_name", methods=['GET', 'POST'])
@login_required
def edit_exercise_name(exercise_id):
exercise = db.exercises.get_exercise(exercise_id)
person_id = request.args.get('person_id', type=int)
if request.method == 'GET':
return render_template('partials/exercise/edit_exercise_name.html', exercise=exercise, person_id=person_id)
else:
updated_name = request.form['name']
updated_exercise = db.exercises.update_exercise_name(exercise_id, updated_name)
return render_template('partials/exercise/exercise_list_item.html', exercise=updated_exercise, person_id=person_id)
@exercises_bp.route("/exercises/add", methods=['POST'])
@login_required
def add_exercise():
exercise_name = request.form['query']
new_exercise = db.exercises.add_exercise(exercise_name)
person_id = request.args.get('person_id', type=int)
return render_template('partials/exercise/exercise_list_item.html', exercise=new_exercise, person_id=person_id)
@exercises_bp.route("/exercise/<int:exercise_id>/delete", methods=['DELETE'])
@login_required
@admin_required
def delete_exercise(exercise_id):
exercise = db.get_exercise(exercise_id)
db.exercises.delete_exercise(exercise_id)
db.activityRequest.log(current_user.id, 'DELETE_EXERCISE', 'exercise', exercise_id, f"Deleted exercise: {exercise['name']}")
return ""

View File

@@ -216,7 +216,26 @@ def get_topset(person_id, workout_id, topset_id):
def get_topset_edit_form(person_id, workout_id, topset_id): def get_topset_edit_form(person_id, workout_id, topset_id):
exercises = db.get_all_exercises() exercises = db.get_all_exercises()
topset = db.get_topset(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, 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)
# Format exercises for custom_select
formatted_exercises = [
{
"exercise_id": ex['exercise_id'],
"name": ex['name'],
"selected": ex['exercise_id'] == topset.get('exercise_id')
} for ex in exercises
]
return render_template('partials/topset.html',
person_id=person_id,
workout_id=workout_id,
topset_id=topset_id,
exercises=formatted_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/<int:person_id>/workout/<int:workout_id>/topset", methods=['POST']) @workout_bp.route("/person/<int:person_id>/workout/<int:workout_id>/topset", methods=['POST'])
@login_required @login_required

View File

@@ -40,7 +40,7 @@
{% if is_edit|default(false, true) == false %} {% if is_edit|default(false, true) == false %}
<button <button
class="inline-flex justify-center p-2 text-blue-600 rounded-full cursor-pointer hover:bg-blue-100 dark:text-blue-500 dark:hover:bg-gray-600" class="inline-flex justify-center p-2 text-blue-600 rounded-full cursor-pointer hover:bg-blue-100 dark:text-blue-500 dark:hover:bg-gray-600"
hx-get="{{ url_for('get_exercise_edit_form', exercise_id=exercise_id) }}"> hx-get="{{ url_for('exercises.get_exercise_edit_form', exercise_id=exercise_id) }}">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" class="w-5 h-5"> stroke="currentColor" class="w-5 h-5">
<path stroke-linecap="round" stroke-linejoin="round" <path stroke-linecap="round" stroke-linejoin="round"
@@ -50,7 +50,7 @@
</button> </button>
<button <button
class="inline-flex justify-center p-2 text-blue-600 rounded-full cursor-pointer hover:bg-blue-100 dark:text-blue-500 dark:hover:bg-gray-600" class="inline-flex justify-center p-2 text-blue-600 rounded-full cursor-pointer hover:bg-blue-100 dark:text-blue-500 dark:hover:bg-gray-600"
hx-delete="{{ url_for('delete_exercise', exercise_id=exercise_id) }}" hx-delete="{{ url_for('exercises.delete_exercise', exercise_id=exercise_id) }}"
hx-confirm="Are you sure you wish to delete {{ name }} from exercises?"> hx-confirm="Are you sure you wish to delete {{ name }} from exercises?">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" class="w-5 h-5"> stroke="currentColor" class="w-5 h-5">
@@ -63,7 +63,7 @@
{% else %} {% else %}
<button <button
class="inline-flex justify-center p-2 text-blue-600 rounded-full cursor-pointer hover:bg-blue-100 dark:text-blue-500 dark:hover:bg-gray-600" class="inline-flex justify-center p-2 text-blue-600 rounded-full cursor-pointer hover:bg-blue-100 dark:text-blue-500 dark:hover:bg-gray-600"
hx-put="{{ url_for('update_exercise', exercise_id=exercise_id) }}" hx-include="closest tr"> hx-put="{{ url_for('exercises.update_exercise', exercise_id=exercise_id) }}" hx-include="closest tr">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" class="w-5 h-5"> stroke="currentColor" class="w-5 h-5">
@@ -74,7 +74,7 @@
<button <button
class="inline-flex justify-center p-2 text-blue-600 rounded-full cursor-pointer hover:bg-blue-100 dark:text-blue-500 dark:hover:bg-gray-600" class="inline-flex justify-center p-2 text-blue-600 rounded-full cursor-pointer hover:bg-blue-100 dark:text-blue-500 dark:hover:bg-gray-600"
hx-get="{{ url_for('get_exercise', exercise_id=exercise_id) }}"> hx-get="{{ url_for('exercises.get_exercise', exercise_id=exercise_id) }}">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" class="w-5 h-5"> stroke="currentColor" class="w-5 h-5">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" /> <path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />

View File

@@ -4,7 +4,8 @@
class="w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-2 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500" class="w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-2 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500"
_="on click from me call event.stopPropagation()"> _="on click from me call event.stopPropagation()">
<!-- Save Icon --> <!-- Save Icon -->
<button hx-post="{{ url_for('edit_exercise_name', exercise_id=exercise.exercise_id, person_id=person_id) }}" <button
hx-post="{{ url_for('exercises.edit_exercise_name', exercise_id=exercise.exercise_id, person_id=person_id) }}"
hx-target="closest li" hx-swap="outerHTML" hx-include="closest li" hx-target="closest li" hx-swap="outerHTML" hx-include="closest li"
class="text-gray-500 hover:text-gray-700 ml-2" _="on click from me call event.stopPropagation()"> class="text-gray-500 hover:text-gray-700 ml-2" _="on click from me call event.stopPropagation()">
<!-- Tick icon SVG --> <!-- Tick icon SVG -->
@@ -14,7 +15,8 @@
</svg> </svg>
</button> </button>
<!-- Delete Icon --> <!-- Delete Icon -->
<button hx-delete="{{ url_for('delete_exercise', exercise_id=exercise.exercise_id, person_id=person_id) }}" <button
hx-delete="{{ url_for('exercises.delete_exercise', exercise_id=exercise.exercise_id, person_id=person_id) }}"
hx-target="closest li" hx-swap="outerHTML" class="text-red-500 hover:text-red-700 ml-2" hx-target="closest li" hx-swap="outerHTML" class="text-red-500 hover:text-red-700 ml-2"
hx-confirm="Are you sure you wish to delete {{ exercise.name }} from exercises?" hx-confirm="Are you sure you wish to delete {{ exercise.name }} from exercises?"
_="on click from me call event.stopPropagation()"> _="on click from me call event.stopPropagation()">

View File

@@ -8,8 +8,8 @@
<div class="py-2 px-4 text-gray-500 flex items-center justify-between border border-gray-200"> <div class="py-2 px-4 text-gray-500 flex items-center justify-between border border-gray-200">
<span>No results found</span> <span>No results found</span>
<!-- Add Exercise Button --> <!-- Add Exercise Button -->
<button hx-post="{{ url_for('add_exercise', person_id=person_id) }}" hx-target="closest div" hx-swap="outerHTML" <button hx-post="{{ url_for('exercises.add_exercise', person_id=person_id) }}" hx-target="closest div"
hx-include="[name='query']" class="text-blue-500 hover:text-blue-700 font-semibold" hx-swap="outerHTML" hx-include="[name='query']" class="text-blue-500 hover:text-blue-700 font-semibold"
_="on click from me call event.stopPropagation()"> _="on click from me call event.stopPropagation()">
Add Exercise Add Exercise
</button> </button>

View File

@@ -5,7 +5,7 @@
<!-- Exercise Name --> <!-- Exercise Name -->
<span>{{ exercise.name }}</span> <span>{{ exercise.name }}</span>
<!-- Edit Icon --> <!-- Edit Icon -->
<a hx-get="{{ url_for('edit_exercise_name', exercise_id=exercise.exercise_id, person_id=person_id) }}" <a hx-get="{{ url_for('exercises.edit_exercise_name', exercise_id=exercise.exercise_id, person_id=person_id) }}"
hx-target="closest li" hx-swap="outerHTML" class="text-gray-500 hover:text-gray-700" hx-target="closest li" hx-swap="outerHTML" class="text-gray-500 hover:text-gray-700"
_="on click from me call event.stopPropagation()"> _="on click from me call event.stopPropagation()">
<!-- Edit icon SVG --> <!-- Edit icon SVG -->

View File

@@ -2,7 +2,7 @@
<input <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" 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="exercise-search" type="search" name="query" placeholder="Search exercises..." id="exercise-search" type="search" name="query" placeholder="Search exercises..."
hx-get="{{ url_for('get_exercises', person_id=person_id) }}" hx-target="#exercise-results" hx-get="{{ url_for('exercises.get_exercises', person_id=person_id) }}" hx-target="#exercise-results"
hx-trigger="keyup changed delay:500ms" hx-swap="innerHTML" autocomplete="off" {% if exercise_name %} hx-trigger="keyup changed delay:500ms" hx-swap="innerHTML" autocomplete="off" {% if exercise_name %}
value="{{ exercise_name }}" {% endif %} _=" value="{{ exercise_name }}" {% endif %} _="
on input on input

View File

@@ -213,7 +213,8 @@
<div class="mt-10"> <div class="mt-10">
<h4 class="text-lg font-semibold text-gray-900 mb-4">Add New Exercise</h4> <h4 class="text-lg font-semibold text-gray-900 mb-4">Add New Exercise</h4>
<form class="bg-gray-50 p-6 rounded-lg border border-gray-100" <form class="bg-gray-50 p-6 rounded-lg border border-gray-100"
hx-post="{{ url_for('create_exercise') }}" hx-swap="beforeend" hx-target="#new-exercise" _="on htmx:afterRequest hx-post="{{ url_for('exercises.create_exercise') }}" hx-swap="beforeend" hx-target="#new-exercise"
_="on htmx:afterRequest
render #notification-template with (message: 'Exercise added') then append it to #notifications-container render #notification-template with (message: 'Exercise added') then append it to #notifications-container
then call _hyperscript.processNode(#notifications-container) then call _hyperscript.processNode(#notifications-container)
then reset() me"> then reset() me">