Swtich from person list to new overview page

This commit is contained in:
Peter Stockings
2025-01-27 01:17:23 +11:00
parent 0ed0c20e93
commit 6b741e50d0
7 changed files with 8 additions and 256 deletions

45
app.py
View File

@@ -6,7 +6,7 @@ import jinja_partials
from jinja2_fragments import render_block
from decorators import validate_person, validate_topset, validate_workout
from db import DataBase
from utils import get_people_and_exercise_rep_maxes, convert_str_to_date, get_earliest_and_latest_workout_date, filter_workout_topsets, first_and_last_visible_days_in_month, generate_plot
from utils import get_people_and_exercise_rep_maxes, convert_str_to_date, first_and_last_visible_days_in_month, generate_plot
from flask_htmx import HTMX
import minify_html
from urllib.parse import quote
@@ -75,45 +75,6 @@ def get_person_list():
people = db.get_people_and_workout_count(-1)
return render_template('partials/people_link.html', people=people)
@ app.route("/person/<int:person_id>/workout/list", methods=['GET'])
@ validate_person
def get_person(person_id):
person = db.get_person(person_id)
tags = db.get_tags_for_person(person_id)
(min_date, max_date) = get_earliest_and_latest_workout_date(person)
min_date = request.args.get(
'min_date', default=min_date, type=convert_str_to_date)
max_date = request.args.get(
'max_date', default=max_date, type=convert_str_to_date)
selected_exercise_ids = request.args.getlist('exercise_id', type=int)
all_exercise_ids_for_person = [e['ExerciseId']
for e in person['Exercises']]
if not selected_exercise_ids and htmx.trigger_name != 'exercise_id':
selected_exercise_ids = all_exercise_ids_for_person
person['Workouts'] = [filter_workout_topsets(workout, selected_exercise_ids) for workout in person['Workouts'] if
workout['StartDate'] <= max_date and workout['StartDate'] >= min_date]
# Filter out workouts that dont contain any of the selected exercises
person['Workouts'] = [workout for workout in person['Workouts'] if
workout['TopSets']]
filtered_exercises = filter(
lambda e: e['ExerciseId'] in selected_exercise_ids, person['Exercises'])
person['FilteredExercises'] = list(filtered_exercises)
person['ExerciseProgressGraphs'] = list(filter(lambda e: e['ExerciseId'] in selected_exercise_ids, person['ExerciseProgressGraphs']))
if htmx:
return render_block(app.jinja_env, 'person.html', 'content', person=person, selected_exercise_ids=selected_exercise_ids, max_date=max_date, min_date=min_date, tags=tags), 200, {"HX-Trigger": "refreshStats"}
return render_template('person.html', person=person, selected_exercise_ids=selected_exercise_ids, max_date=max_date, min_date=min_date, tags=tags), 200, {"HX-Trigger": "refreshStats"}
@ app.route("/person/<int:person_id>/workout/overview", methods=['GET'])
def person_overview(person_id):
min_date = request.args.get('min_date', type=convert_str_to_date)
@@ -154,8 +115,8 @@ def get_calendar(person_id):
'date'), '%Y-%m-%d') or date.today()
selected_view = request.args.get('view') or 'month'
if selected_view == 'all':
return redirect(url_for('get_person', person_id=person_id))
if selected_view == 'overview':
return redirect(url_for('person_overview', person_id=person_id))
elif selected_view == 'notes':
return redirect(url_for('get_person_notes', person_id=person_id))

View File

@@ -23,7 +23,7 @@ def validate_workout(func):
from app import db
workout = db.is_valid_workout(person_id, workout_id)
if workout is None:
return render_template('error.html', error='404', message=f'Unable to find Workout({workout_id}) completed by Person({person_id})', url=url_for('get_person', person_id=person_id))
return render_template('error.html', error='404', message=f'Unable to find Workout({workout_id}) completed by Person({person_id})', url=url_for('person_overview', person_id=person_id))
return func(*args, **kwargs)
return wrapper

View File

@@ -46,7 +46,7 @@
<option value="month" {% if view=='month' %}selected{% endif %}>Month</option>
<option value="year" {% if view=='year' %}selected{% endif %}>Year</option>
<option value="notes">Notes</option>
<option value="all">All</option>
<option value="overview">Overview</option>
</select>
</div>
</div>

View File

@@ -69,7 +69,7 @@
allowOutsideClick: () => !Swal.isLoading()
}).then((result) => {
if (result.isConfirmed) {
htmx.ajax('GET', `{{ (url_for('get_person', person_id=person_id) if person_id else url_for('dashboard')) + '?' + request.query_string.decode() }}`, '#container')
htmx.ajax('GET', `{{ (url_for('person_overview', person_id=person_id) if person_id else url_for('dashboard')) + '?' + request.query_string.decode() }}`, '#container')
}
})

View File

@@ -1,186 +0,0 @@
{% extends 'base.html' %}
{% block content %}
<div class="w-full mb-4 grid grid-cols-1 xl:grid-cols-2 gap-4">
{% for graph in person['PersonGraphs'] %}
<div class="bg-white shadow rounded-lg p-4 sm:p-6 xl:p-8">
{{ render_partial('partials/svg_line_graph.html', **graph) }}
</div>
{% endfor %}
</div>
<div class="flex max-w-full overflow-x-hidden">
<div class="bg-white shadow rounded-lg pt-2 pb-2 sm:w-full xl:p-8 md:w-full">
<div class="mb-4 flex items-center justify-between px-2 md:px-3">
<div>
<h3 class="text-xl font-bold text-gray-900 mb-2">{{ person['PersonName'] }}</h3>
<span class="text-base font-normal text-gray-500">List of workouts</span>
</div>
<div>
<div>
<select name="view" hx-get="{{ url_for('get_calendar', person_id=person['PersonId']) }}"
hx-target="#container" hx-push-url="true" _="init js(me) tail.select(me, {}) end" class="h-10 invisible">
<option value="month">Month</option>
<option value="year">Year</option>
<option value="notes">Notes</option>
<option value="all" selected>All</option>
</select>
</div>
</div>
</div>
<div class="flex flex-wrap mb-1">
<div class="w-full md:w-1/3 px-2 md:px-3 mb-6 md:mb-0">
<div class="mb-1 w-full">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-city">
Exercises
</label>
<select data-te-select-filter="true" data-te-select-size="lg" name="exercise_id"
class="bg-gray-50 border border-gray-300 " multiple
hx-get="{{ url_for('get_person', person_id=person['PersonId']) }}"
hx-include="[name='exercise_id'],[name='min_date'],[name='max_date'],[name='graph_axis']"
hx-target="#container" hx-push-url="true" _="init js(me)
tail.select(me, {
multiple: true,
search: true,
placeholder: 'Filter exercises',
})
end">
{% for e in person['Exercises'] %}
<option value="{{ e['ExerciseId'] }}" {% if e['ExerciseId'] in selected_exercise_ids
%}selected{% endif %}>{{
e['ExerciseName']
}}</option>
{% endfor %}
</select>
</div>
</div>
<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">
Min date
</label>
<div class="relative pr-2">
<div class="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none">
<svg aria-hidden="true" class="w-5 h-5 text-gray-500 dark:text-gray-400" fill="currentColor"
viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd"
d="M6 2a1 1 0 00-1 1v1H4a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2h-1V3a1 1 0 10-2 0v1H7V3a1 1 0 00-1-1zm0 5a1 1 0 000 2h8a1 1 0 100-2H6z"
clip-rule="evenodd"></path>
</svg>
</div>
<input type="date"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5 w-full"
name="min_date" value="{{ min_date }}"
hx-get="{{ url_for('get_person', person_id=person['PersonId']) }}"
hx-include="[name='exercise_id'],[name='min_date'],[name='max_date'],[name='graph_axis']"
hx-target="#container" hx-push-url="true" hx-trigger="change">
</div>
</div>
<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-zip">
Max date
</label>
<div class="relative pr-2">
<div class="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none">
<svg aria-hidden="true" class="w-5 h-5 text-gray-500 dark:text-gray-400" fill="currentColor"
viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd"
d="M6 2a1 1 0 00-1 1v1H4a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2h-1V3a1 1 0 10-2 0v1H7V3a1 1 0 00-1-1zm0 5a1 1 0 000 2h8a1 1 0 100-2H6z"
clip-rule="evenodd"></path>
</svg>
</div>
<input type="date"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5 w-full"
name="max_date" value="{{ max_date }}"
hx-get="{{ url_for('get_person', person_id=person['PersonId']) }}"
hx-include="[name='exercise_id'],[name='min_date'],[name='max_date'],[name='graph_axis']"
hx-target="#container" hx-push-url="true" hx-trigger="change">
</div>
</div>
</div>
{{ render_partial('partials/tags.html',person_id=person['PersonId'], tags=tags) }}
<div class="flex flex-col mt-3 w-screen sm:w-full overflow-auto">
<div class="overflow-x-auto rounded-lg">
<div class="flex justify-center">
<div class="flex justify-center shadow sm:rounded-lg w-screen sm:w-screen">
{% if person['Workouts']|length > 0 %}
<table class="min-w-content divide-y divide-gray-200">
<thead class="bg-gray-50">
<tr>
<th scope="col"
class="p-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Date
</th>
{% for e in person['FilteredExercises'] %}
<th scope="col"
class="p-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
{{ e['ExerciseName'] }}
</th>
{% endfor %}
</tr>
</thead>
<tbody class="bg-white">
{% for w in person['Workouts'] %}
<tr hx-get="{{ url_for('show_workout', person_id=person['PersonId'], workout_id=w['WorkoutId']) }}"
hx-push-url="true" hx-target="#container" class="cursor-pointer">
<td class="p-4 whitespace-nowrap text-sm font-normal text-gray-500">
{{ w['StartDate'] | strftime("%b %d %Y") }}
</td>
{% for e in person['FilteredExercises'] %}
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
{% set topset_exercise = w['TopSets'] |
get_first_element_from_list_with_matching_attribute('ExerciseId',
e['ExerciseId']) %}
{% if topset_exercise %}
{{ topset_exercise['Repetitions'] }} x {{ topset_exercise['Weight'] }}kg
{% endif %}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% if person['Workouts']|length == 0 %}
<div class="bg-purple-100 rounded-lg py-5 px-6 mb-4 text-base text-purple-700 mb-3"
role="alert">
No workouts found.
</div>
{% endif %}
</div>
</div>
</div>
</div>
</div>
</div>
<div class="mt-4 mb-4 w-full grid grid-cols-1 2xl:grid-cols-2 gap-4">
{% for graph in person['ExerciseProgressGraphs'] %}
{{ render_partial('partials/sparkline.html', **graph['ExerciseProgressGraph']) }}
{% endfor %}
</div>
{{ render_partial('partials/stats.html', stats=person['Stats']) }}
<button
class="fixed z-90 bottom-10 right-8 bg-blue-600 w-20 h-20 rounded-full drop-shadow-lg flex justify-center items-center text-white text-4xl hover:bg-blue-700 hover:drop-shadow-2xl hover:animate-bounce duration-300"
hx-post="{{ url_for('create_workout', person_id=person['PersonId']) }}" hx-target='body' hx-swap='beforeend'>
<svg viewBox="0 0 20 20" enable-background="new 0 0 20 20" class="w-6 h-6 inline-block">
<path fill="#FFFFFF" d="M16,10c0,0.553-0.048,1-0.601,1H11v4.399C11,15.951,10.553,16,10,16c-0.553,0-1-0.049-1-0.601V11H4.601
C4.049,11,4,10.553,4,10c0-0.553,0.049-1,0.601-1H9V4.601C9,4.048,9.447,4,10,4c0.553,0,1,0.048,1,0.601V9h4.399
C15.952,9,16,9.447,16,10z" />
</svg>
</button>
{% endblock %}

View File

@@ -22,8 +22,8 @@
class="h-10 invisible">
<option value="month">Month</option>
<option value="year">Year</option>
<option value="notes">Notes</option>
<option value="all" selected>All</option>
<option value="notes" selected>Notes</option>
<option value="overview">Overview</option>
</select>
</div>
</div>

View File

@@ -127,29 +127,6 @@ def convert_str_to_date(date_str, format='%Y-%m-%d'):
return None
def get_earliest_and_latest_workout_date(person):
workouts = person.get('Workouts', [])
if workouts:
# Initialize earliest and latest dates with the first workout's start date
earliest_date = latest_date = workouts[0]['StartDate']
for workout in workouts[1:]:
date = workout['StartDate']
if date < earliest_date:
earliest_date = date
if date > latest_date:
latest_date = date
return (earliest_date, latest_date)
# Return the current date for both if no workouts are present
current_date = datetime.now().date()
return (current_date, current_date)
def filter_workout_topsets(workout, selected_exercise_ids):
workout['TopSets'] = [topset for topset in workout['TopSets']
if topset['ExerciseId'] in selected_exercise_ids]
return workout
def flatten_list(list_of_lists):
return [item for sublist in list_of_lists for item in sublist]