Add ability to dynamically add/remove users & exercises

This commit is contained in:
Peter Stockings
2022-09-17 13:15:29 +10:00
parent 9c0e43e7a8
commit 3005dd18bd
3 changed files with 66 additions and 51 deletions

30
app.py
View File

@@ -91,10 +91,38 @@ def delete_topset(person_id, workout_id, topset_id):
return redirect(url_for('get_workout', person_id=person_id, workout_id=workout_id))
@ app.route("/person", methods=['POST'])
def create_person():
name = request.form.get("name")
db.create_person(name)
return redirect(url_for('settings'))
@ app.route("/person/<int:person_id>/delete", methods=['GET', 'POST'])
def delete_person(person_id):
db.delete_person(person_id)
return redirect(url_for('settings'))
@ app.route("/exercise", methods=['POST'])
def create_exercise():
name = request.form.get("name")
db.create_exercise(name)
return redirect(url_for('settings'))
@ app.route("/exercise/<int:exercise_id>/delete", methods=['GET', 'POST'])
def delete_exercise(exercise_id):
db.delete_exercise(exercise_id)
return redirect(url_for('settings'))
@ app.route("/settings")
@ swag_from('swagger/dashboard.yml')
def settings():
return render_template('settings.html')
people = db.get_people()
exercises = db.get_exercises()
return render_template('settings.html', people=people, exercises=exercises)
@ app.context_processor

25
db.py
View File

@@ -43,11 +43,36 @@ class DataBase():
'SELECT ExerciseId AS "ExerciseId", Name AS "Name" FROM Exercise')
return [{"ExerciseId": e['ExerciseId'], "Name": e['Name']} for e in exercises]
def create_exercise(self, name):
self.execute('INSERT INTO Exercise (Name) VALUES (%s)',
[name], commit=True)
def delete_exercise(self, exercise_id):
self.execute('DELETE FROM Exercise WHERE ExerciseId=%s', [
exercise_id], commit=True)
def get_people(self):
people = self.execute(
'SELECT PersonId AS "PersonId", Name AS "Name" FROM Person')
return people
def get_person(self, person_id):
person = self.execute(
'SELECT PersonId AS "PersonId" FROM Person WHERE PersonId=%s LIMIT 1', [person_id], one=True)
return person
def create_person(self, name):
self.execute('INSERT INTO Person (Name) VALUES (%s)',
[name], commit=True)
def delete_person(self, person_id):
self.execute('DELETE FROM TopSet WHERE WorkoutId IN (SELECT WorkoutId FROM Workout WHERE PersonId=%s)', [
person_id], commit=True)
self.execute('DELETE FROM Workout WHERE PersonId=%s',
[person_id], commit=True)
self.execute('DELETE FROM Person WHERE PersonId=%s',
[person_id], commit=True)
def get_workout(self, person_id, workout_id):
workout = self.execute('SELECT W.WorkoutId AS "WorkoutId" FROM Person P, Workout W WHERE P.PersonId=W.PersonId AND P.PersonId=%s AND W.WorkoutId=%s LIMIT 1', [
person_id, workout_id], one=True)

View File

@@ -31,34 +31,22 @@
</tr>
</thead>
<tbody class="bg-white">
{% for p in people %}
<tr>
<td class="p-4 whitespace-nowrap text-sm font-normal text-gray-500">
1
{{ loop.index }}
</td>
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
Gabe
{{ p['Name'] }}
</td>
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
<a href="/person/2/workout/10"
class="text-sm font-medium text-cyan-600 hover:bg-gray-100 rounded-lg inline-flex items-center p-2">
Remove
</a>
</td>
</tr>
<tr>
<td class="p-4 whitespace-nowrap text-sm font-normal text-gray-500">
2
</td>
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
Michael
</td>
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
<a href="/person/2/workout/10"
<a href="{{ url_for('delete_person', person_id=p['PersonId']) }}"
class="text-sm font-medium text-cyan-600 hover:bg-gray-100 rounded-lg inline-flex items-center p-2">
Remove
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
@@ -66,7 +54,7 @@
</div>
</div>
<form class="w-full mt-3" action="/user" method="post">
<form class="w-full mt-3" action="{{ url_for('create_person') }}" method="post">
<div class="flex flex-wrap -mx-3 mb-2">
<div class="grow px-3">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-city">
@@ -123,48 +111,22 @@
</tr>
</thead>
<tbody class="bg-white">
{% for e in exercises %}
<tr>
<td class="p-4 whitespace-nowrap text-sm font-normal text-gray-500">
1
{{ loop.index }}
</td>
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
Squat
{{ e['Name'] }}
</td>
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
<a href="/person/2/workout/10"
class="text-sm font-medium text-cyan-600 hover:bg-gray-100 rounded-lg inline-flex items-center p-2">
Remove
</a>
</td>
</tr>
<tr>
<td class="p-4 whitespace-nowrap text-sm font-normal text-gray-500">
1
</td>
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
Deadlift
</td>
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
<a href="/person/2/workout/10"
class="text-sm font-medium text-cyan-600 hover:bg-gray-100 rounded-lg inline-flex items-center p-2">
Remove
</a>
</td>
</tr>
<tr>
<td class="p-4 whitespace-nowrap text-sm font-normal text-gray-500">
1
</td>
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
Bench
</td>
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
<a href="/person/2/workout/10"
<a href="{{ url_for('delete_exercise', exercise_id=e['ExerciseId']) }}"
class="text-sm font-medium text-cyan-600 hover:bg-gray-100 rounded-lg inline-flex items-center p-2">
Remove
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
@@ -172,7 +134,7 @@
</div>
</div>
<form class="w-full mt-3" action="/user" method="post">
<form class="w-full mt-3" action="{{ url_for('create_exercise') }}" method="post">
<div class="flex flex-wrap -mx-3 mb-2">
<div class="grow px-3">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-city">