summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstilbruch <stilbruch@protonmail.com>2022-04-24 19:45:07 -0500
committerstilbruch <stilbruch@protonmail.com>2022-04-24 19:45:07 -0500
commit0bcfd4a9a26d66af8a44ff36686b54ad2572d9e1 (patch)
treeba6d035a835c2451427a46d29b30c85898dd398d
parent0dd74e2b672053e3296bc814a36988ba8463854f (diff)
downloadStrengthy-0bcfd4a9a26d66af8a44ff36686b54ad2572d9e1.tar.xz
Strengthy-0bcfd4a9a26d66af8a44ff36686b54ad2572d9e1.zip
Fix workout creation and editing
-rw-r--r--app/forms/workout.py2
-rw-r--r--app/routes/workout.py45
-rw-r--r--app/templates/workout/create.html22
3 files changed, 46 insertions, 23 deletions
diff --git a/app/forms/workout.py b/app/forms/workout.py
index 5537114..ec9eb03 100644
--- a/app/forms/workout.py
+++ b/app/forms/workout.py
@@ -12,7 +12,7 @@ from wtforms.validators import DataRequired, Email, Optional
# /workout/create
class ExerciseCreateForm(Form):
# TODO no exercise_*, breaks workout_edit endpoint
- id = HiddenField("id", [DataRequired()])
+ id = HiddenField("id", [Optional()])
name = StringField("name", [DataRequired()])
sets = IntegerField("sets", [DataRequired()])
units = IntegerField("units", [DataRequired()])
diff --git a/app/routes/workout.py b/app/routes/workout.py
index 0c6116c..996105c 100644
--- a/app/routes/workout.py
+++ b/app/routes/workout.py
@@ -27,7 +27,6 @@ def workout_create():
else:
flash("Workout with this name already exists", "danger")
else:
- print(form)
print(form.errors)
return render_template("workout/create.html", form=form)
@@ -50,21 +49,35 @@ def workout_edit():
form = WorkoutCreateForm()
if form.validate_on_submit():
- # Form has been submitted, write changes FIXME
+ # Form has been submitted, write changes
for entry in form.exercises.entries:
- # Get the specified exercise TODO needs hidden id field
- exercise = Exercise.query.filter_by(
- workout_id=workout.id, id=int(entry.data["id"])
- ).first()
- if not exercise:
- continue
-
- # Update exercise
- exercise.name = entry.data["name"]
- exercise.sets = entry.data["sets"]
- exercise.units = entry.data["units"]
- exercise.type = entry.data["type"]
+ id = entry.data["id"]
+ name = entry.data["name"]
+ sets = entry.data["sets"]
+ units = entry.data["units"]
+ type = entry.data["type"]
+
+ # Is this a new exercise or an old one
+ if id:
+ # Get the specified exercise
+ exercise = Exercise.query.filter_by(
+ workout_id=workout.id, id=int(id)
+ ).first()
+
+ if not exercise:
+ continue
+
+ # Update exercise
+ exercise.name = name
+ exercise.sets = sets
+ exercise.units = units
+ exercise.type = type
+ else:
+ # Create new exercise
+ workout.exercises.append(Exercise(name, sets, units, type))
+
+ # FIXME: support deleting exercises
# Write changes to database
db.session.commit()
@@ -76,9 +89,7 @@ def workout_edit():
for exercise in workout.exercises:
form.exercises.append_entry(exercise)
- return render_template(
- "workout/create.html", form=form, title=f'Edit Workout "{workout.name}"'
- )
+ return render_template("workout/create.html", form=form, workout=workout)
@app.route("/workout/record", methods=["GET", "POST"])
diff --git a/app/templates/workout/create.html b/app/templates/workout/create.html
index a84b457..1f6d136 100644
--- a/app/templates/workout/create.html
+++ b/app/templates/workout/create.html
@@ -1,6 +1,12 @@
{% extends 'base/form.html' %}
-{% block title %}Create Workout{% endblock %}
+{% block title %}
+ {% if workout %}
+ Edit {{ workout.name }}
+ {% else %}
+ Create Workout
+ {% endif %}
+{% endblock %}
{% block form %}
<div class="field is-horizontal">
@@ -18,7 +24,9 @@
<div id="rows" class="field">
{% for entry in form.exercises.entries %}
- {{ entry['id']() }}
+ {% if workout %}
+ {{ entry['id']() }}
+ {% endif %}
<div class="field is-horizontal">
<div class="field-body">
<div class="field">
@@ -73,10 +81,14 @@
<div class="field is-horizontal is-grouped is-grouped-right">
<button class="button is-success ">
- <span class="icon is-small">
- <i class="fa fa-check"></i>
+ <span class="icon is-small"><i class="fa fa-check"></i></span>
+ <span>
+ {% if workout %}
+ Save Changes
+ {% else %}
+ Create Workout
+ {% endif %}
</span>
- <span>Create Workout</span>
</button>
</div>