Create seperate page view for workout (WIP)

This commit is contained in:
Peter Stockings
2024-11-02 23:09:26 +11:00
parent 1384eb007b
commit 817a6016e2
4 changed files with 193 additions and 0 deletions

5
app.py
View File

@@ -433,6 +433,11 @@ def get_stats_for_person(person_id):
stats = db.stats.fetch_stats_for_person(person_id) stats = db.stats.fetch_stats_for_person(person_id)
return render_template('partials/stats.html', stats=stats) return render_template('partials/stats.html', stats=stats)
@ app.route("/person/<int:person_id>/workout/<int:workout_id>", 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 @app.teardown_appcontext
def closeConnection(exception): def closeConnection(exception):

2
db.py
View File

@@ -8,6 +8,7 @@ from urllib.parse import urlparse
from flask import g from flask import g
from features.calendar import Calendar from features.calendar import Calendar
from features.stats import Stats 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 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): def __init__(self, app):
self.calendar = Calendar(self.execute) self.calendar = Calendar(self.execute)
self.stats = Stats(self.execute) self.stats = Stats(self.execute)
self.workout = Workout(self.execute)
db_url = urlparse(os.environ['DATABASE_URL']) db_url = urlparse(os.environ['DATABASE_URL'])
# if db_url is null then throw error # if db_url is null then throw error
if not db_url: if not db_url:

78
features/workout.py Normal file
View File

@@ -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

108
templates/workout.html Normal file
View File

@@ -0,0 +1,108 @@
{% extends 'base.html' %}
{% block content %}
<div class=' p-2 md:p-4 m-2'>
<div class="relative w-full h-full">
<!-- Modal content -->
<div class="relative bg-white rounded-lg shadow">
<!-- Modal header -->
<div class="flex items-start justify-between p-2 md:p-4 border-0 md:border-b rounded-t">
<div class="flex flex-col w-full">
<div class="flex items-center justify-between">
<div class="w-full">
<h3 class="text-xl font-bold text-gray-900">{{ person_name }}</h3>
{{ 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) }}
</div>
</div>
<div class="grid grid-cols-1 lg:grid-cols-2">
{{ 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) }}
</div>
</div>
<button type="button"
class="absolute right-0 text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center dark:hover:bg-gray-600 dark:hover:text-white"
data-modal-toggle="defaultModal" _="on click trigger closeModal">
<svg aria-hidden="true" class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg" data-darkreader-inline-fill=""
style="--darkreader-inline-fill:currentColor;">
<path fill-rule="evenodd"
d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
clip-rule="evenodd"></path>
</svg>
<span class="sr-only">Close modal</span>
</button>
</div>
</div>
<!-- Modal body -->
<div class="p-3 md:p-6 space-y-6">
<table class="items-center w-full bg-transparent border-collapse table-fixed">
<thead>
<tr>
<th
class="px-4 bg-gray-50 text-gray-700 align-middle py-3 text-xs font-semibold text-left uppercase border-l-0 border-r-0 whitespace-nowrap">
Exercise</th>
<th
class="px-4 bg-gray-50 text-gray-700 align-middle py-3 text-xs font-semibold text-left uppercase border-l-0 border-r-0 whitespace-nowrap">
Top Set</th>
<th
class="bg-gray-50 text-gray-700 align-middle py-3 text-xs font-semibold text-left uppercase border-l-0 border-r-0 whitespace-nowrap w-9 sm:w-20">
</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-100" id="new-workout" hx-target="closest tr"
hx-swap="outerHTML swap:0.5s">
{% for top_set in top_sets %}
{{ render_partial('partials/topset.html', person_id=person_id,
workout_id=workout_id, **top_set) }}
{% endfor %}
</tbody>
</table>
{% if top_sets|length == 0 %}
<div class="bg-purple-100 rounded-lg py-5 px-6 mb-4 text-base text-purple-700 mb-3" role="alert"
id="no-workouts">
No top_sets found.
</div>
{% endif %}
</div>
<!-- Modal footer -->
<div class="flex items-center p-3 md:p-6 space-x-2 border-t border-gray-200 rounded-b dark:border-gray-600">
{{ render_partial('partials/new_set_form.html', person_id=person_id,
workout_id=workout_id,
exercises=exercises,
has_value=False) }}
</div>
<div id="exercise-progress-{{ person_id }}" class="mx-0 md:mx-5">
<div class="w-full md:w-1/3 px-2 md:px-3 mb-6 md:mb-0">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-city">
Exercise
</label>
<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"
type="text" name="repetitions" </div>
</div>
</div>
</div>
{% endblock %}