Refactor dashboard

This commit is contained in:
Peter Stockings
2025-01-27 14:46:20 +11:00
parent a7592a29f6
commit f70438e4e4
5 changed files with 335 additions and 218 deletions

64
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, first_and_last_visible_days_in_month, generate_plot
from utils import convert_str_to_date, generate_plot
from flask_htmx import HTMX
import minify_html
from urllib.parse import quote
@@ -38,36 +38,44 @@ def response_minify(response):
return response
return response
@ app.route("/")
def dashboard():
all_topsets = db.get_all_topsets()
selected_people_ids = request.args.getlist('person_id', type=int)
min_date = request.args.get('min_date', type=convert_str_to_date)
max_date = request.args.get('max_date', type=convert_str_to_date)
selected_exercise_ids = request.args.getlist('exercise_id', type=int)
exercises = db.get_all_exercises()
people = db.get_people()
tags = db.get_tags_for_dashboard()
if not selected_people_ids and htmx.trigger_name != 'person_id':
selected_people_ids = db.dashboard.get_people_ids()
selected_person_ids = [int(i)
for i in request.args.getlist('person_id')]
if not selected_person_ids and htmx.trigger_name != 'person_id':
selected_person_ids = [p['PersonId'] for p in people]
selected_exercise_ids = [int(i)
for i in request.args.getlist('exercise_id')]
if not min_date or not max_date:
db_min_date, db_max_date = db.dashboard.get_earliest_and_latest_workout_dates(selected_people_ids)
min_date = min_date or db_min_date
max_date = max_date or db_max_date
if not selected_exercise_ids and htmx.trigger_name != 'exercise_id':
selected_exercise_ids = [e['exercise_id'] for e in exercises]
selected_exercise_ids = db.dashboard.list_of_performed_exercise_ids(selected_people_ids, min_date, max_date)
people = db.dashboard.get_people_with_selection(selected_people_ids)
exercises = db.dashboard.get_exercises_with_selection(selected_people_ids, min_date, max_date, selected_exercise_ids)
tags = db.get_tags_for_dashboard()
dashboard = db.dashboard.get(selected_people_ids, min_date, max_date, selected_exercise_ids)
min_date = convert_str_to_date(request.args.get(
'min_date'), '%Y-%m-%d') or min([t['StartDate'] for t in all_topsets])
max_date = convert_str_to_date(request.args.get(
'max_date'), '%Y-%m-%d') or max([t['StartDate'] for t in all_topsets])
people_and_exercise_rep_maxes = get_people_and_exercise_rep_maxes(
all_topsets, selected_person_ids, selected_exercise_ids, min_date, max_date)
# Render the appropriate response for HTMX or full page
render_args = {
**dashboard,
"people": people,
"exercises": exercises,
"tags": tags,
"selected_people_ids": selected_people_ids,
"max_date": max_date,
"min_date": min_date,
"selected_exercise_ids": selected_exercise_ids
}
if htmx:
return render_block(app.jinja_env, 'dashboard.html', 'content', model=people_and_exercise_rep_maxes, people=people, exercises=exercises, min_date=min_date, max_date=max_date, selected_person_ids=selected_person_ids, selected_exercise_ids=selected_exercise_ids, tags=tags)
return render_template('dashboard.html', model=people_and_exercise_rep_maxes, people=people, exercises=exercises, min_date=min_date, max_date=max_date, selected_person_ids=selected_person_ids, selected_exercise_ids=selected_exercise_ids, tags=tags)
return render_block(app.jinja_env, 'dashboard.html', 'content', **render_args)
return render_template('dashboard.html', **render_args)
@ app.route("/person/list", methods=['GET'])
@@ -238,8 +246,8 @@ def delete_person(person_id):
@ app.route("/person/<int:person_id>/edit_form", methods=['GET'])
def get_person_edit_form(person_id):
person = db.get_person(person_id)
return render_template('partials/person.html', person_id=person_id, name=person['PersonName'], is_edit=True)
name = db.get_person_name(person_id)
return render_template('partials/person.html', person_id=person_id, name=name, is_edit=True)
@ app.route("/person/<int:person_id>/name", methods=['PUT'])
@@ -251,8 +259,8 @@ def update_person_name(person_id):
@ app.route("/person/<int:person_id>/name", methods=['GET'])
def get_person_name(person_id):
person = db.get_person(person_id)
return render_template('partials/person.html', person_id=person_id, name=person['PersonName'])
name = db.get_person_name(person_id)
return render_template('partials/person.html', person_id=person_id, name=name)
@ app.route("/exercise", methods=['POST'])
@@ -634,7 +642,7 @@ def my_utility_processor():
def list_to_string(list):
return [str(i) for i in list]
return dict(is_selected_page=is_selected_page, get_first_element_from_list_with_matching_attribute=get_first_element_from_list_with_matching_attribute, in_list=in_list, strftime=strftime, datetime=datetime, timedelta=timedelta, relativedelta=relativedelta, first_and_last_visible_days_in_month=first_and_last_visible_days_in_month, list_to_string=list_to_string, quote=quote)
return dict(is_selected_page=is_selected_page, get_first_element_from_list_with_matching_attribute=get_first_element_from_list_with_matching_attribute, in_list=in_list, strftime=strftime, datetime=datetime, timedelta=timedelta, relativedelta=relativedelta, list_to_string=list_to_string, quote=quote)
if __name__ == '__main__':