From 605d84f8bb7dcc9ce544310b5d015675980720cc Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Sat, 19 Nov 2022 22:48:24 +1100 Subject: [PATCH] Convert exercise form into htmx driven --- app.py | 83 +++++++++++++++++++++++++++++++++++++++-- db.py | 14 ++++++- static/css/style.css | 4 ++ templates/base.html | 2 + templates/settings.html | 19 +++++----- 5 files changed, 106 insertions(+), 16 deletions(-) create mode 100644 static/css/style.css diff --git a/app.py b/app.py index 0f6106f..ee669f7 100644 --- a/app.py +++ b/app.py @@ -104,14 +104,89 @@ def delete_person(person_id): @ app.route("/exercise", methods=['POST']) def create_exercise(): name = request.form.get("name") - db.create_exercise(name) - return redirect(url_for('settings')) + new_exercise_id = db.create_exercise(name) + return f""" + + + {name} + + + + Edit + + + Remove + + + + """ -@ app.route("/exercise//delete", methods=['GET', 'POST']) +@ app.route("/exercise/", methods=['GET']) +def get_exercise(exercise_id): + exercise = db.get_exercise(exercise_id) + return f""" + + + {exercise['Name']} + + + + Edit + + + Remove + + + + """ + + +@ app.route("/exercise//edit_form", methods=['GET']) +def get_exercise_edit_form(exercise_id): + exercise = db.get_exercise(exercise_id) + return f""" + + + + + + + Update + + + Cancel + + + + """ + + +@ app.route("/exercise//update", methods=['PUT']) +def update_exercise(exercise_id): + new_name = request.form.get('name') + db.update_exercise(exercise_id, new_name) + return f""" + + + {new_name} + + + + Edit + + + Remove + + + + """ + + +@ app.route("/exercise//delete", methods=['DELETE']) def delete_exercise(exercise_id): db.delete_exercise(exercise_id) - return redirect(url_for('settings')) + return "" @ app.route("/settings") diff --git a/db.py b/db.py index 7653cae..dcbf04d 100644 --- a/db.py +++ b/db.py @@ -36,14 +36,24 @@ class DataBase(): 'SELECT ExerciseId AS "ExerciseId", Name AS "Name" FROM Exercise') return [{"ExerciseId": e['ExerciseId'], "Name": e['Name']} for e in exercises] + def get_exercise(self, exercise_id): + exercise = self.execute( + 'SELECT ExerciseId AS "ExerciseId", Name AS "Name" FROM Exercise WHERE ExerciseId=%s LIMIT 1', [exercise_id], one=True) + return exercise + def create_exercise(self, name): - self.execute('INSERT INTO Exercise (Name) VALUES (%s)', - [name], commit=True) + new_exercise = self.execute('INSERT INTO Exercise (Name) VALUES (%s) RETURNING ExerciseId AS "ExerciseId"', + [name], commit=True, one=True) + return new_exercise['ExerciseId'] def delete_exercise(self, exercise_id): self.execute('DELETE FROM Exercise WHERE ExerciseId=%s', [ exercise_id], commit=True) + def update_exercise(self, exercise_id, name): + self.execute('UPDATE Exercise SET Name=%s WHERE ExerciseId=%s', [ + name, exercise_id], commit=True) + def get_people(self): people = self.execute( 'SELECT PersonId AS "PersonId", Name AS "Name" FROM Person') diff --git a/static/css/style.css b/static/css/style.css new file mode 100644 index 0000000..66b26e0 --- /dev/null +++ b/static/css/style.css @@ -0,0 +1,4 @@ +tr.htmx-swapping td { + opacity: 0; + transition: opacity 0.5s ease-out; +} \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index cfda831..2f4c1e0 100644 --- a/templates/base.html +++ b/templates/base.html @@ -11,6 +11,8 @@ + +