Filter exercises to render (needs refactor)

This commit is contained in:
Peter Stockings
2022-11-23 21:40:06 +11:00
parent ff430dc956
commit 1181937b6d
3 changed files with 136 additions and 96 deletions

19
app.py
View File

@@ -32,6 +32,18 @@ def get_person(person_id):
return render_template('person.html', person=person)
@app.route("/person/<int:person_id>/exercise_filter", methods=['POST'])
@ validate_person
def filter_exercises_for_person(person_id):
selected_exercise_ids = [int(i)
for i in request.form.getlist('exercise_id')]
person = db.get_person(person_id)
filtered_exercises = filter(
lambda e: e['ExerciseId'] in selected_exercise_ids, person['Exercises'])
person['FilteredExercises'] = list(filtered_exercises)
return render_template('partials/page/person.html', person=person, is_filtered=True, selected_exercise_ids=selected_exercise_ids)
@ app.route("/person/<int:person_id>/workout", methods=['POST'])
@ validate_person
def create_workout(person_id):
@@ -213,7 +225,12 @@ def my_utility_processor():
return element
return None
return dict(get_list_of_people_and_workout_count=get_list_of_people_and_workout_count, is_selected_page=is_selected_page, get_first_element_from_list_with_matching_attribute=get_first_element_from_list_with_matching_attribute)
def is_checked(val, checked_vals):
if not checked_vals:
return 'checked'
return 'checked' if val in checked_vals else ''
return dict(get_list_of_people_and_workout_count=get_list_of_people_and_workout_count, is_selected_page=is_selected_page, get_first_element_from_list_with_matching_attribute=get_first_element_from_list_with_matching_attribute, is_checked=is_checked)
if __name__ == '__main__':

View File

@@ -0,0 +1,116 @@
<div id="person-view">
<div class="flex justify-center">
<div class="bg-white shadow rounded-lg p-4 sm:w-full xl:p-8 md:w-full lg:w-10/12 xl:w-8/12 2xl:w-6/12">
<div class="mb-4 flex items-center justify-between">
<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>
<form action="{{ url_for('create_workout', person_id=person['PersonId']) }}" method="post">
<button
class="sm:inline-flex text-white bg-cyan-600 hover:bg-cyan-700 focus:ring-4 focus:ring-cyan-200 font-medium rounded-lg text-sm px-5 py-2.5 text-center items-center mt-6">New
workout</button>
</form>
</div>
</div>
<div class="flex">
{% set exercise_list = person['FilteredExercises'] if is_filtered else person['Exercises'] %}
{% for e in person['Exercises'] %}
<div class="flex items-center mr-4"
hx-post="{{ url_for('filter_exercises_for_person', person_id=person['PersonId']) }}"
hx-include="[name='exercise_id']" hx-target="#person-view" hx-swap="outerHTML">
<input {{ is_checked(e['ExerciseId'], selected_exercise_ids) }} type="checkbox" name="exercise_id"
value="{{ e['ExerciseId'] }}"
class="w-4 h-4 text-blue-600 bg-gray-100 rounded border-gray-300 focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600">
<label for="inline-checked-checkbox"
class="ml-2 text-sm font-medium text-gray-900 dark:text-gray-300">{{ e['ExerciseName']
}}</label>
</div>
{% endfor %}
</div>
<div class="flex flex-col mt-8">
<div class="overflow-x-auto rounded-lg">
<div class="align-middle inline-block min-w-full">
<div class="shadow overflow-hidden sm:rounded-lg">
{% if person['Workouts']|length > 0 %}
<table class="min-w-full 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 exercise_list %}
<th scope="col"
class="p-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
{{ e['ExerciseName'] }}
</th>
{% endfor %}
<th scope="col"
class="p-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider w-8">
</th>
</tr>
</thead>
<tbody class="bg-white">
{% for w in person['Workouts'] %}
<tr>
<td class="p-4 whitespace-nowrap text-sm font-normal text-gray-500">
{{ w['StartDate'] }}
</td>
{% for e in exercise_list %}
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
{% set topset_exercise =
get_first_element_from_list_with_matching_attribute(w['TopSets'],
'ExerciseId',
e['ExerciseId']) %}
{% if topset_exercise %}
{{ topset_exercise['Repetitions'] }} x {{ topset_exercise['Weight'] }}kg
{% endif %}
</td>
{% endfor %}
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
<a href="{{ url_for('get_workout' ,person_id=person['PersonId'], workout_id=w['WorkoutId']) }}"
class="text-sm font-medium text-cyan-600 hover:bg-gray-100 rounded-lg inline-flex items-center p-2 cursor-pointer">
Edit
</a>
<form
action="{{ url_for('delete_workout', person_id=person['PersonId'], workout_id=w['WorkoutId']) }}"
method="delete" class="inline">
<button
class="text-sm font-medium text-cyan-600 hover:bg-gray-100 rounded-lg inline-flex items-center p-2 cursor-pointer"
type="submit">Delete</button>
</form>
</td>
</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>
{{ render_partial('partials/stats.html', stats=person['Stats']) }}
</div>

View File

@@ -1,101 +1,8 @@
{% extends 'base.html' %}
{% block content %}
<div class="flex justify-center">
<div class="bg-white shadow rounded-lg p-4 sm:w-full xl:p-8 md:w-full lg:w-10/12 xl:w-8/12 2xl:w-6/12">
<div class="mb-4 flex items-center justify-between">
<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>
<form action="{{ url_for('create_workout', person_id=person['PersonId']) }}" method="post">
<button
class="sm:inline-flex text-white bg-cyan-600 hover:bg-cyan-700 focus:ring-4 focus:ring-cyan-200 font-medium rounded-lg text-sm px-5 py-2.5 text-center items-center mt-6">New
workout</button>
</form>
</div>
</div>
<div class="flex flex-col mt-8">
<div class="overflow-x-auto rounded-lg">
<div class="align-middle inline-block min-w-full">
<div class="shadow overflow-hidden sm:rounded-lg">
{% if person['Workouts']|length > 0 %}
<table class="min-w-full 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['Exercises'] %}
<th scope="col"
class="p-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
{{ e['ExerciseName'] }}
</th>
{% endfor %}
<th scope="col"
class="p-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider w-8">
</th>
</tr>
</thead>
<tbody class="bg-white">
{% for w in person['Workouts'] %}
<tr>
<td class="p-4 whitespace-nowrap text-sm font-normal text-gray-500">
{{ w['StartDate'] }}
</td>
{% for e in person['Exercises'] %}
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
{% set topset_exercise =
get_first_element_from_list_with_matching_attribute(w['TopSets'], 'ExerciseId',
e['ExerciseId']) %}
{% if topset_exercise %}
{{ topset_exercise['Repetitions'] }} x {{ topset_exercise['Weight'] }}kg
{% endif %}
</td>
{% endfor %}
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
<a href="{{ url_for('get_workout' ,person_id=person['PersonId'], workout_id=w['WorkoutId']) }}"
class="text-sm font-medium text-cyan-600 hover:bg-gray-100 rounded-lg inline-flex items-center p-2 cursor-pointer">
Edit
</a>
<form
action="{{ url_for('delete_workout', person_id=person['PersonId'], workout_id=w['WorkoutId']) }}"
method="delete" class="inline">
<button
class="text-sm font-medium text-cyan-600 hover:bg-gray-100 rounded-lg inline-flex items-center p-2 cursor-pointer"
type="submit">Delete</button>
</form>
</td>
</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>
{{ render_partial('partials/stats.html', stats=person['Stats']) }}
{{ render_partial('partials/page/person.html',
person=person, is_filtered=False) }}
{% endblock %}