diff --git a/app.py b/app.py index 3023dbf..44db8dd 100644 --- a/app.py +++ b/app.py @@ -11,6 +11,7 @@ from utils import flatten, get_date_info, get_people_and_exercise_rep_maxes, con from flask_htmx import HTMX import minify_html from urllib.parse import urlparse, unquote, quote +import random app = Flask(__name__) app.config.from_pyfile('config.py') @@ -79,63 +80,66 @@ def get_person(person_id): (min_date, max_date) = get_earliest_and_latest_workout_date(person) - min_date = convert_str_to_date(request.args.get( - 'min_date'), '%Y-%m-%d') or min_date - max_date = convert_str_to_date(request.args.get( - 'max_date'), '%Y-%m-%d') or max_date + 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) - graph_axis = request.args.getlist('graph_axis') or [] + graph_axis = request.args.getlist('graph_axis') + + selected_exercise_ids = request.args.getlist('exercise_id', type=int) + all_exercise_ids_for_person = [e['ExerciseId'] + for e in person['Exercises']] - selected_exercise_ids = [int(i) - for i in request.args.getlist('exercise_id')] if not selected_exercise_ids and htmx.trigger_name != 'exercise_id': - selected_exercise_ids = [e['ExerciseId'] for e in person['Exercises']] + 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] - active_exercise_ids = get_exercise_ids_from_workouts(person['Workouts']) - # 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 active_exercise_ids, person['Exercises']) + lambda e: e['ExerciseId'] in selected_exercise_ids, person['Exercises']) person['FilteredExercises'] = list(filtered_exercises) - # New feature to plot reps and weights over time - topsets_with_start_date = flatten([(p['StartDate'], p['TopSets']) - for p in person['Workouts']]) - topsets = flatten([[{**t, 'StartDate': start_date} for t in topsets] - for (start_date, topsets) in topsets_with_start_date]) + if graph_axis: + # New feature to plot reps and weights over time + topsets_with_start_date = flatten([(p['StartDate'], p['TopSets']) + for p in person['Workouts']]) + topsets = flatten([[{**t, 'StartDate': start_date} for t in topsets] + for (start_date, topsets) in topsets_with_start_date]) - exercise_graph_view_models = [] - for exercise_id in active_exercise_ids: - topsets_for_exercise = [ - t for t in topsets if t['ExerciseId'] == exercise_id] - if topsets_for_exercise: - repitions = [t['Repetitions'] for t in topsets_for_exercise] - weights = [t['Weight'] for t in topsets_for_exercise] - estimated_one_rep_max = [t['Estimated1RM'] - for t in topsets_for_exercise] - start_dates = [t['StartDate'].strftime( - "%Y-%m-%d") for t in topsets_for_exercise] - exercise_view_model = { - 'ExerciseId': exercise_id, - 'ExerciseName': topsets_for_exercise[0]['ExerciseName'], - 'Repetitions': repitions[::-1], - 'Weights': weights[::-1], - 'Estimated1RM': estimated_one_rep_max[::-1], - 'StartDates': start_dates[::-1] - } - exercise_graph_view_models.append(exercise_view_model) - person['ExerciseGraphs'] = exercise_graph_view_models + exercise_graph_view_models = [] + for exercise_id in selected_exercise_ids: + topsets_for_exercise = [ + t for t in topsets if t['ExerciseId'] == exercise_id] + if topsets_for_exercise: + repitions = [t['Repetitions'] for t in topsets_for_exercise] + weights = [t['Weight'] for t in topsets_for_exercise] + estimated_one_rep_max = [t['Estimated1RM'] + for t in topsets_for_exercise] + start_dates = [t['StartDate'].strftime( + "%Y-%m-%d") for t in topsets_for_exercise] + exercise_view_model = { + 'ExerciseId': exercise_id, + 'ExerciseName': topsets_for_exercise[0]['ExerciseName'], + 'Repetitions': repitions[::-1], + 'Weights': weights[::-1], + 'Estimated1RM': estimated_one_rep_max[::-1], + 'StartDates': start_dates[::-1] + } + exercise_graph_view_models.append(exercise_view_model) + person['ExerciseGraphs'] = exercise_graph_view_models + else: + person['ExerciseGraphs'] = [] if htmx: - return render_block(app.jinja_env, 'person.html', 'content', person=person, selected_exercise_ids=active_exercise_ids, max_date=max_date, min_date=min_date, tags=tags, graph_axis=graph_axis), 200, {"HX-Trigger": "updatedPeople"} + 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, graph_axis=graph_axis), 200, {"HX-Trigger": "updatedPeople"} - return render_template('person.html', person=person, selected_exercise_ids=active_exercise_ids, max_date=max_date, min_date=min_date, tags=tags, graph_axis=graph_axis), 200, {"HX-Trigger": "updatedPeople"} + return render_template('person.html', person=person, selected_exercise_ids=selected_exercise_ids, max_date=max_date, min_date=min_date, tags=tags, graph_axis=graph_axis), 200, {"HX-Trigger": "updatedPeople"} @ app.route("/person//calendar") @@ -400,6 +404,16 @@ def create_new_tag_for_workout(person_id, workout_id): return render_template('partials/workout_tags_list.html', workout_tags=workout_tags) +# # TODO: Remove me, just for testing +# @ app.route("/sparkline", methods=['GET']) +# def get_sparkline(): +# width = request.args.get('width', 400, type=int) +# height = request.args.get('height', 200, type=int) +# number_of_points = request.args.get('number_of_points', 50, type=int) +# points = [random.randint(1, 100) for _ in range(number_of_points)] +# return render_template('partials/sparkline.html', width=width, height=height, points=points) + + @app.teardown_appcontext def closeConnection(exception): db.close_connection()