diff --git a/app.py b/app.py index 47ab2f0..4f0601c 100644 --- a/app.py +++ b/app.py @@ -1,3 +1,4 @@ +from datetime import datetime import os from flask import Flask, render_template, redirect, request, url_for import jinja_partials @@ -33,25 +34,39 @@ def get_person_list(): @ app.route("/person/") @ validate_person def get_person(person_id): - selected_exercise_ids = [int(i) - for i in request.args.getlist('exercise_id')] person = db.get_person(person_id) + max_date = request.args.get( + 'max_date') or datetime.strptime(max(person['Workouts'], key=lambda x: datetime.strptime(x['StartDate'], '%b %d %Y'))['StartDate'], '%b %d %Y').strftime('%Y-%m-%d') + min_date = request.args.get( + 'min_date') or datetime.strptime(min(person['Workouts'], key=lambda x: datetime.strptime(x['StartDate'], '%b %d %Y'))['StartDate'], '%b %d %Y').strftime('%Y-%m-%d') + + selected_exercise_ids = [int(i) + for i in request.args.getlist('exercise_id')] or [e['ExerciseId'] for e in person['Exercises']] + + 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 + + person['Workouts'] = [filter_workout_topsets(workout, selected_exercise_ids) for workout in person['Workouts'] if datetime.strptime( + workout['StartDate'], '%b %d %Y').strftime('%Y-%m-%d') <= max_date and datetime.strptime(workout['StartDate'], '%b %d %Y').strftime('%Y-%m-%d') >= min_date] + if selected_exercise_ids: filtered_exercises = filter( lambda e: e['ExerciseId'] in selected_exercise_ids, person['Exercises']) person['FilteredExercises'] = list(filtered_exercises) if htmx: return render_template('partials/page/person.html', - person=person, is_filtered=True, selected_exercise_ids=selected_exercise_ids), 200, {"HX-Trigger": "updatedPeople"} + person=person, is_filtered=True, selected_exercise_ids=selected_exercise_ids, max_date=max_date, min_date=min_date), 200, {"HX-Trigger": "updatedPeople"} - return render_template('person.html', person=person, is_filtered=True, selected_exercise_ids=selected_exercise_ids) + return render_template('person.html', person=person, is_filtered=True, selected_exercise_ids=selected_exercise_ids, max_date=max_date, min_date=min_date) if htmx: return render_template('partials/page/person.html', - person=person, is_filtered=False), 200, {"HX-Trigger": "updatedPeople"} + person=person, is_filtered=False, max_date=max_date, min_date=min_date), 200, {"HX-Trigger": "updatedPeople"} - return render_template('person.html', person=person) + return render_template('person.html', person=person, max_date=max_date, min_date=min_date) @ app.route("/person//workout", methods=['POST']) diff --git a/templates/partials/page/person.html b/templates/partials/page/person.html index 354cbc8..077c135 100644 --- a/templates/partials/page/person.html +++ b/templates/partials/page/person.html @@ -14,12 +14,14 @@ +
{% set exercise_list = person['FilteredExercises'] if is_filtered else person['Exercises'] %} {% for e in person['Exercises'] %}
+ hx-include="[name='exercise_id'],[name='min_date'],[name='max_date']" hx-target="#container" + hx-push-url="true"> @@ -30,6 +32,51 @@ {% endfor %}
+
+
+ +
+
+ +
+ +
+
+
+ +
+
+ +
+ +
+
+
+
diff --git a/templates/person.html b/templates/person.html index a15fb9b..7886867 100644 --- a/templates/person.html +++ b/templates/person.html @@ -3,6 +3,7 @@ {% block content %} {{ render_partial('partials/page/person.html', -person=person, is_filtered=is_filtered or False, selected_exercise_ids=selected_exercise_ids) }} +person=person, is_filtered=is_filtered or False, selected_exercise_ids=selected_exercise_ids, max_date=max_date, +min_date=min_date) }} {% endblock %} \ No newline at end of file