Add ability to add/update/delete exercise categories
This commit is contained in:
7
app.py
7
app.py
@@ -230,16 +230,17 @@ def settings():
|
||||
people = db.get_people()
|
||||
exercises = db.get_all_exercises()
|
||||
all_attributes = db.exercises.get_attributes_by_category()
|
||||
categories_list = db.exercises.get_all_attribute_categories()
|
||||
|
||||
# Format options for custom_select
|
||||
formatted_options = {}
|
||||
for cat, attrs in all_attributes.items():
|
||||
formatted_options[cat] = [{"id": a['attribute_id'], "name": a['name']} for a in attrs]
|
||||
formatted_options[cat] = [{"id": a['attribute_id'], "attribute_id": a['attribute_id'], "name": a['name'], "category_id": a['category_id']} for a in attrs]
|
||||
|
||||
if htmx:
|
||||
return render_block(app.jinja_env, "settings.html", "content",
|
||||
people=people, exercises=exercises, all_attributes=formatted_options), 200, {"HX-Trigger": "updatedPeople"}
|
||||
return render_template('settings.html', people=people, exercises=exercises, all_attributes=formatted_options)
|
||||
people=people, exercises=exercises, all_attributes=formatted_options, categories_list=categories_list), 200, {"HX-Trigger": "updatedPeople"}
|
||||
return render_template('settings.html', people=people, exercises=exercises, all_attributes=formatted_options, categories_list=categories_list)
|
||||
|
||||
|
||||
@app.route("/settings/activity_logs")
|
||||
|
||||
@@ -147,3 +147,38 @@ class Exercises:
|
||||
|
||||
return distribution
|
||||
|
||||
# Category Management
|
||||
def add_category(self, name):
|
||||
result = self.execute('INSERT INTO exercise_attribute_category (name) VALUES (%s) RETURNING category_id, name', [name], commit=True, one=True)
|
||||
return result
|
||||
|
||||
def update_category(self, category_id, name):
|
||||
self.execute('UPDATE exercise_attribute_category SET name = %s WHERE category_id = %s', [name, category_id], commit=True)
|
||||
return {"category_id": category_id, "name": name}
|
||||
|
||||
def delete_category(self, category_id):
|
||||
# First delete all attributes in this category
|
||||
attributes = self.execute('SELECT attribute_id FROM exercise_attribute WHERE category_id = %s', [category_id])
|
||||
for attr in attributes:
|
||||
self.delete_attribute(attr['attribute_id'])
|
||||
|
||||
self.execute('DELETE FROM exercise_attribute_category WHERE category_id = %s', [category_id], commit=True)
|
||||
|
||||
# Attribute Management
|
||||
def add_attribute(self, name, category_id):
|
||||
result = self.execute('INSERT INTO exercise_attribute (name, category_id) VALUES (%s, %s) RETURNING attribute_id, name, category_id', [name, category_id], commit=True, one=True)
|
||||
return result
|
||||
|
||||
def update_attribute(self, attribute_id, name, category_id=None):
|
||||
if category_id:
|
||||
self.execute('UPDATE exercise_attribute SET name = %s, category_id = %s WHERE attribute_id = %s', [name, category_id, attribute_id], commit=True)
|
||||
else:
|
||||
self.execute('UPDATE exercise_attribute SET name = %s WHERE attribute_id = %s', [name, attribute_id], commit=True)
|
||||
return self.execute('SELECT attribute_id, name, category_id FROM exercise_attribute WHERE attribute_id = %s', [attribute_id], one=True)
|
||||
|
||||
def delete_attribute(self, attribute_id):
|
||||
# Remove from all exercises first
|
||||
self.execute('DELETE FROM exercise_to_attribute WHERE attribute_id = %s', [attribute_id], commit=True)
|
||||
# Delete the attribute
|
||||
self.execute('DELETE FROM exercise_attribute WHERE attribute_id = %s', [attribute_id], commit=True)
|
||||
|
||||
|
||||
@@ -115,3 +115,70 @@ def delete_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 ""
|
||||
|
||||
# Category Management Routes
|
||||
@exercises_bp.route("/category", methods=['POST'])
|
||||
@login_required
|
||||
@admin_required
|
||||
def create_category():
|
||||
name = request.form.get("name")
|
||||
category = db.exercises.add_category(name)
|
||||
db.activityRequest.log(current_user.id, 'CREATE_CATEGORY', 'category', category['category_id'], f"Created attribute category: {name}")
|
||||
return render_template('partials/exercise/category_admin.html', category_id=category['category_id'], name=category['name'], attributes=[])
|
||||
|
||||
@exercises_bp.route("/category/<int:category_id>", methods=['GET', 'PUT'])
|
||||
@login_required
|
||||
@admin_required
|
||||
def update_category(category_id):
|
||||
if request.method == 'GET':
|
||||
category = db.exercises.execute('SELECT category_id, name FROM exercise_attribute_category WHERE category_id = %s', [category_id], one=True)
|
||||
is_edit = request.args.get('is_edit') == 'true'
|
||||
all_attrs = db.exercises.execute('SELECT attribute_id, name FROM exercise_attribute WHERE category_id = %s', [category_id])
|
||||
return render_template('partials/exercise/category_admin.html', category_id=category_id, name=category['name'], attributes=all_attrs, is_edit=is_edit)
|
||||
|
||||
name = request.form.get("name")
|
||||
category = db.exercises.update_category(category_id, name)
|
||||
db.activityRequest.log(current_user.id, 'UPDATE_CATEGORY', 'category', category_id, f"Updated attribute category: {name}")
|
||||
all_attrs = db.exercises.execute('SELECT attribute_id, name FROM exercise_attribute WHERE category_id = %s', [category_id])
|
||||
return render_template('partials/exercise/category_admin.html', category_id=category_id, name=name, attributes=all_attrs)
|
||||
|
||||
@exercises_bp.route("/category/<int:category_id>", methods=['DELETE'])
|
||||
@login_required
|
||||
@admin_required
|
||||
def delete_category(category_id):
|
||||
db.exercises.delete_category(category_id)
|
||||
db.activityRequest.log(current_user.id, 'DELETE_CATEGORY', 'category', category_id, f"Deleted attribute category")
|
||||
return ""
|
||||
|
||||
# Attribute Management Routes
|
||||
@exercises_bp.route("/attribute", methods=['POST'])
|
||||
@login_required
|
||||
@admin_required
|
||||
def create_attribute():
|
||||
name = request.form.get("name")
|
||||
category_id = request.form.get("category_id", type=int)
|
||||
attribute = db.exercises.add_attribute(name, category_id)
|
||||
db.activityRequest.log(current_user.id, 'CREATE_ATTRIBUTE', 'attribute', attribute['attribute_id'], f"Created attribute: {name}")
|
||||
return render_template('partials/exercise/attribute_admin.html', attribute=attribute)
|
||||
|
||||
@exercises_bp.route("/attribute/<int:attribute_id>", methods=['GET', 'PUT'])
|
||||
@login_required
|
||||
@admin_required
|
||||
def update_attribute(attribute_id):
|
||||
if request.method == 'GET':
|
||||
attribute = db.exercises.execute('SELECT attribute_id, name, category_id FROM exercise_attribute WHERE attribute_id = %s', [attribute_id], one=True)
|
||||
is_edit = request.args.get('is_edit') == 'true'
|
||||
return render_template('partials/exercise/attribute_admin.html', attribute=attribute, is_edit=is_edit)
|
||||
|
||||
name = request.form.get("name")
|
||||
attribute = db.exercises.update_attribute(attribute_id, name)
|
||||
db.activityRequest.log(current_user.id, 'UPDATE_ATTRIBUTE', 'attribute', attribute_id, f"Updated attribute: {name}")
|
||||
return render_template('partials/exercise/attribute_admin.html', attribute=attribute)
|
||||
|
||||
@exercises_bp.route("/attribute/<int:attribute_id>", methods=['DELETE'])
|
||||
@login_required
|
||||
@admin_required
|
||||
def delete_attribute(attribute_id):
|
||||
db.exercises.delete_attribute(attribute_id)
|
||||
db.activityRequest.log(current_user.id, 'DELETE_ATTRIBUTE', 'attribute', attribute_id, "Deleted attribute")
|
||||
return ""
|
||||
|
||||
45
templates/partials/exercise/attribute_admin.html
Normal file
45
templates/partials/exercise/attribute_admin.html
Normal file
@@ -0,0 +1,45 @@
|
||||
<div id="attribute-{{ attribute.attribute_id }}"
|
||||
class="group flex items-center px-3 py-1 bg-white border border-gray-200 rounded-lg shadow-sm hover:border-cyan-300 transition-all">
|
||||
{% if is_edit %}
|
||||
<form hx-put="{{ url_for('exercises.update_attribute', attribute_id=attribute.attribute_id) }}"
|
||||
hx-target="#attribute-{{ attribute.attribute_id }}" hx-swap="outerHTML" class="flex items-center space-x-2">
|
||||
<input type="text" name="name" value="{{ attribute.name }}"
|
||||
class="text-xs font-semibold text-gray-700 bg-gray-50 border-none p-0 focus:ring-0 w-20" autofocus
|
||||
onfocus="this.select()">
|
||||
<button type="submit" class="text-green-600 hover:text-green-700">
|
||||
<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M5 13l4 4L19 7"></path>
|
||||
</svg>
|
||||
</button>
|
||||
<button type="button" hx-get="{{ url_for('exercises.update_attribute', attribute_id=attribute.attribute_id) }}"
|
||||
hx-target="#attribute-{{ attribute.attribute_id }}" hx-swap="outerHTML"
|
||||
class="text-gray-400 hover:text-gray-600">
|
||||
<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M6 18L18 6M6 6l12 12"></path>
|
||||
</svg>
|
||||
</button>
|
||||
</form>
|
||||
{% else %}
|
||||
<span class="text-xs font-medium text-gray-700 mr-2">{{ attribute.name }}</span>
|
||||
<div class="hidden group-hover:flex items-center space-x-1 ml-auto">
|
||||
<button hx-get="{{ url_for('exercises.update_attribute', attribute_id=attribute.attribute_id) }}?is_edit=true"
|
||||
hx-target="#attribute-{{ attribute.attribute_id }}" hx-swap="outerHTML"
|
||||
class="p-1 text-gray-400 hover:text-cyan-600 transition-colors">
|
||||
<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
||||
d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z">
|
||||
</path>
|
||||
</svg>
|
||||
</button>
|
||||
<button hx-delete="{{ url_for('exercises.update_attribute', attribute_id=attribute.attribute_id) }}"
|
||||
hx-target="#attribute-{{ attribute.attribute_id }}" hx-swap="outerHTML" hx-confirm="Delete this attribute?"
|
||||
class="p-1 text-gray-400 hover:text-red-600 transition-colors">
|
||||
<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
||||
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16">
|
||||
</path>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
76
templates/partials/exercise/category_admin.html
Normal file
76
templates/partials/exercise/category_admin.html
Normal file
@@ -0,0 +1,76 @@
|
||||
<div id="category-{{ category_id }}"
|
||||
class="bg-gray-50 border border-gray-200 rounded-2xl p-6 transition-all hover:shadow-md">
|
||||
<div class="flex items-center justify-between mb-6">
|
||||
<div class="flex items-center space-x-4">
|
||||
<div class="p-2 bg-cyan-100 rounded-lg text-cyan-600">
|
||||
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
||||
d="M7 7h.01M7 11h.01M7 15h.01M11 7h.01M11 11h.01M11 15h.01M15 7h.01M15 11h.01M15 15h.01M19 7h.01M19 11h.01M19 15h.01M7 3h10a2 2 0 012 2v14a2 2 0 01-2 2H7a2 2 0 01-2-2V5a2 2 0 012-2z">
|
||||
</path>
|
||||
</svg>
|
||||
</div>
|
||||
{% if is_edit %}
|
||||
<form hx-put="{{ url_for('exercises.update_category', category_id=category_id) }}"
|
||||
hx-target="#category-{{ category_id }}" hx-swap="outerHTML" class="flex items-center space-x-2">
|
||||
<input type="text" name="name" value="{{ name }}"
|
||||
class="text-lg font-bold text-gray-900 bg-white border border-cyan-200 rounded-lg px-2 py-1 focus:ring-2 focus:ring-cyan-500/20 focus:border-cyan-500 outline-none"
|
||||
autofocus onfocus="this.select()">
|
||||
<button type="submit" class="text-green-600 hover:text-green-700 font-bold p-1">
|
||||
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M5 13l4 4L19 7"></path>
|
||||
</svg>
|
||||
</button>
|
||||
<button type="button" hx-get="{{ url_for('exercises.update_category', category_id=category_id) }}"
|
||||
hx-target="#category-{{ category_id }}" hx-swap="outerHTML"
|
||||
class="text-gray-400 hover:text-gray-600 p-1">
|
||||
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M6 18L18 6M6 6l12 12">
|
||||
</path>
|
||||
</svg>
|
||||
</button>
|
||||
</form>
|
||||
{% else %}
|
||||
<h5 class="text-lg font-bold text-gray-900">{{ name }}</h5>
|
||||
<button hx-get="{{ url_for('exercises.update_category', category_id=category_id) }}?is_edit=true"
|
||||
hx-target="#category-{{ category_id }}" hx-swap="outerHTML"
|
||||
class="text-gray-400 hover:text-cyan-600 p-1 transition-colors">
|
||||
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
||||
d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z">
|
||||
</path>
|
||||
</svg>
|
||||
</button>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
<button hx-delete="{{ url_for('exercises.update_category', category_id=category_id) }}"
|
||||
hx-target="#category-{{ category_id }}" hx-swap="outerHTML"
|
||||
hx-confirm="Deleteting '{{ name }}' will also delete all its attributes. Are you sure?"
|
||||
class="text-gray-400 hover:text-red-600 p-2 rounded-lg hover:bg-red-50 transition-all">
|
||||
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
||||
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16">
|
||||
</path>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="flex flex-wrap gap-2 mb-6" id="attributes-list-{{ category_id }}">
|
||||
{% for attr in attributes %}
|
||||
{{ render_partial('partials/exercise/attribute_admin.html', attribute=attr) }}
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
<form hx-post="{{ url_for('exercises.create_attribute') }}" hx-target="#attributes-list-{{ category_id }}"
|
||||
hx-swap="beforeend" _="on htmx:afterRequest reset() me" class="flex items-center space-x-2">
|
||||
<input type="hidden" name="category_id" value="{{ category_id }}">
|
||||
<input type="text" name="name" placeholder="Add new option..."
|
||||
class="flex-1 text-sm bg-white border border-gray-200 rounded-xl px-4 py-2 focus:ring-2 focus:ring-cyan-500/20 focus:border-cyan-500 outline-none transition-all">
|
||||
<button type="submit"
|
||||
class="bg-white border border-gray-200 text-gray-600 hover:text-cyan-600 hover:border-cyan-200 p-2 rounded-xl transition-all shadow-sm active:scale-95">
|
||||
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path>
|
||||
</svg>
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
@@ -279,6 +279,48 @@
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<!-- Category & Attribute Management Section -->
|
||||
<div class="mt-12 pt-12 border-t border-gray-100">
|
||||
<div class="mb-8">
|
||||
<h4 class="text-lg font-bold text-gray-900">Manage Categories & Options</h4>
|
||||
<p class="text-sm text-gray-500">Add or edit muscle groups, equipment types, and other exercise
|
||||
attributes.</p>
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-8" id="categories-admin-list">
|
||||
{% for cat in categories_list %}
|
||||
{% set options = all_attributes.get(cat.name, []) %}
|
||||
{{ render_partial('partials/exercise/category_admin.html', category_id=cat.category_id,
|
||||
name=cat.name, attributes=options) }}
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
<!-- Add New Category Form -->
|
||||
<div class="mt-8 p-6 bg-gray-50/50 rounded-2xl border border-dashed border-gray-300">
|
||||
<form hx-post="{{ url_for('exercises.create_category') }}" hx-target="#categories-admin-list"
|
||||
hx-swap="beforeend" _="on htmx:afterRequest reset() me"
|
||||
class="flex flex-col sm:flex-row items-center gap-4">
|
||||
<div class="w-full sm:flex-1">
|
||||
<label class="block text-xs font-bold text-gray-400 uppercase mb-2">New Category
|
||||
Name</label>
|
||||
<input type="text" name="name" placeholder="e.g. Difficulty, Intensity..."
|
||||
class="w-full text-sm bg-white border border-gray-200 rounded-xl px-4 py-2.5 focus:ring-2 focus:ring-cyan-500/20 focus:border-cyan-500 outline-none transition-all shadow-sm"
|
||||
required>
|
||||
</div>
|
||||
<div class="w-full sm:w-auto self-end">
|
||||
<button type="submit"
|
||||
class="w-full flex items-center justify-center text-white bg-gray-800 hover:bg-gray-900 font-bold rounded-xl text-sm px-6 py-2.5 transition-all shadow-md active:scale-95">
|
||||
<svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
||||
d="M12 4v16m8-8H4"></path>
|
||||
</svg>
|
||||
Create Category
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user