From 6b741e50d0166ac3904dd249d800410c6787a34b Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Mon, 27 Jan 2025 01:17:23 +1100 Subject: [PATCH] Swtich from person list to new overview page --- app.py | 45 +------- decorators.py | 2 +- templates/calendar.html | 2 +- templates/partials/tags.html | 2 +- templates/person.html | 186 --------------------------------- templates/person_overview.html | 4 +- utils.py | 23 ---- 7 files changed, 8 insertions(+), 256 deletions(-) delete mode 100644 templates/person.html diff --git a/app.py b/app.py index 39d10d0..493d3e6 100644 --- a/app.py +++ b/app.py @@ -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//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//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)) diff --git a/decorators.py b/decorators.py index f1cb80a..c7d6d0a 100644 --- a/decorators.py +++ b/decorators.py @@ -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 diff --git a/templates/calendar.html b/templates/calendar.html index b5e0c14..f88c66b 100644 --- a/templates/calendar.html +++ b/templates/calendar.html @@ -46,7 +46,7 @@ - + diff --git a/templates/partials/tags.html b/templates/partials/tags.html index c6cf986..a899155 100644 --- a/templates/partials/tags.html +++ b/templates/partials/tags.html @@ -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') } }) diff --git a/templates/person.html b/templates/person.html deleted file mode 100644 index 1124367..0000000 --- a/templates/person.html +++ /dev/null @@ -1,186 +0,0 @@ -{% extends 'base.html' %} - -{% block content %} - -
- {% for graph in person['PersonGraphs'] %} -
- {{ render_partial('partials/svg_line_graph.html', **graph) }} -
- {% endfor %} -
- -
-
- -
-
-

{{ person['PersonName'] }}

- List of workouts -
-
-
- -
-
-
- -
-
-
- - -
-
-
- -
-
- -
- -
-
-
- -
-
- -
- -
-
-
- - {{ render_partial('partials/tags.html',person_id=person['PersonId'], tags=tags) }} - -
-
-
-
- - {% if person['Workouts']|length > 0 %} - - - - - {% for e in person['FilteredExercises'] %} - - {% endfor %} - - - - - {% for w in person['Workouts'] %} - - - - {% for e in person['FilteredExercises'] %} - - {% endfor %} - - {% endfor %} - - -
- Date - - {{ e['ExerciseName'] }} -
- {{ w['StartDate'] | strftime("%b %d %Y") }} - - {% 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 %} -
- {% endif %} - - {% if person['Workouts']|length == 0 %} - - {% endif %} - -
-
-
-
- -
-
- -
- {% for graph in person['ExerciseProgressGraphs'] %} - {{ render_partial('partials/sparkline.html', **graph['ExerciseProgressGraph']) }} - {% endfor %} -
- -{{ render_partial('partials/stats.html', stats=person['Stats']) }} - - - -{% endblock %} \ No newline at end of file diff --git a/templates/person_overview.html b/templates/person_overview.html index 53653df..2661dc0 100644 --- a/templates/person_overview.html +++ b/templates/person_overview.html @@ -22,8 +22,8 @@ class="h-10 invisible"> - - + + diff --git a/utils.py b/utils.py index 630b892..81ebbc9 100644 --- a/utils.py +++ b/utils.py @@ -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]