From 586285028845c8ed9a2e859f1266c233b8278311 Mon Sep 17 00:00:00 2001 From: stilbruch Date: Sun, 24 Apr 2022 17:59:31 -0500 Subject: Fix backend for workout editing --- app/forms/workout.py | 17 ++++++++++++----- app/routes/workout.py | 22 ++++++++++++++++++---- app/tables/workout.py | 20 +++++++------------- app/templates/workout/create.html | 1 + app/templates/workout/record.html | 14 ++++++++++++-- 5 files changed, 50 insertions(+), 24 deletions(-) (limited to 'app') diff --git a/app/forms/workout.py b/app/forms/workout.py index a5614eb..cf9f635 100644 --- a/app/forms/workout.py +++ b/app/forms/workout.py @@ -1,11 +1,19 @@ from flask_wtf import FlaskForm, Form -from wtforms import FieldList, FormField, IntegerField, SelectField, StringField +from wtforms import ( + FieldList, + FormField, + HiddenField, + IntegerField, + SelectField, + StringField, +) from wtforms.validators import DataRequired, Email # /workout/create class ExerciseCreateForm(Form): # TODO no exercise_*, breaks workout_edit endpoint - name = StringField("name", [DataRequired()], name="name") + id = HiddenField("id", [DataRequired()]) + name = StringField("name", [DataRequired()]) sets = IntegerField("sets", [DataRequired()]) units = IntegerField("units", [DataRequired()]) type = SelectField( @@ -20,14 +28,13 @@ class WorkoutCreateForm(FlaskForm): # /workout/record class SetForm(Form): - lbs = IntegerField("lbs", [DataRequired()]) - reps = IntegerField("reps", [DataRequired()]) + lbs = IntegerField("lbs") + reps = IntegerField("reps") class ExerciseRecordForm(Form): sets = FieldList(FormField(SetForm)) -# Actual forms class WorkoutRecordForm(FlaskForm): exercises = FieldList(FormField(ExerciseRecordForm)) diff --git a/app/routes/workout.py b/app/routes/workout.py index a929a6c..ffdacb9 100644 --- a/app/routes/workout.py +++ b/app/routes/workout.py @@ -2,7 +2,7 @@ from app import app, db from flask import render_template, redirect, request, url_for, flash from flask_login import current_user, login_required from forms import WorkoutCreateForm, WorkoutRecordForm -from tables import User, Workout +from tables import Exercise, User, Workout @app.route("/workout/create", methods=["GET", "POST"]) @@ -46,10 +46,23 @@ def workout_edit(): form = WorkoutCreateForm() if form.validate_on_submit(): - # Form has been submitted, write changes + # Form has been submitted, write changes FIXME + 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() - workout.name = form.name.data - # TODO: Add exercise changes + if not exercise: # NOTE error? + continue + + # Update exercise + exercise.name = entry.data["name"] + exercise.sets = entry.data["sets"] + exercise.units = entry.data["units"] + exercise.type = entry.data["type"] + + print(exercise) # Write changes to database db.session.commit() @@ -83,6 +96,7 @@ def workout_record(): return redirect(url_for("home")) if form.validate_on_submit(): + # Form has been submitted and is valid FIXME print(form) return render_template("workout/record.html", workout=workout, form=form) diff --git a/app/tables/workout.py b/app/tables/workout.py index 75faf6b..f9c2f93 100644 --- a/app/tables/workout.py +++ b/app/tables/workout.py @@ -2,11 +2,6 @@ from app import db, login_manager import enum -class ExerciseType(enum.Enum): - TIME = "Seconds" - REPS = "Reps" - - # Represents an individual exercise class Exercise(db.Model): __tablename__ = "exercises" @@ -14,7 +9,7 @@ class Exercise(db.Model): name = db.Column(db.String(100), nullable=False) sets = db.Column(db.Integer) units = db.Column(db.Integer) - type = db.Column(db.Enum(ExerciseType)) + type = db.Column(db.Enum("reps", "time"), nullable=False) # Workout Relationship workout_id = db.Column(db.Integer, db.ForeignKey("workouts.id")) @@ -42,15 +37,14 @@ class Workout(db.Model): self.name = name self.user_id = user.id - # Create exercises for exercise in exercises: - type = ( - ExerciseType.TIME - if exercise["exercise_type"] == "time" - else ExerciseType.REPS - ) self.exercises.append( - Exercise(exercise["name"], exercise["sets"], exercise["units"], type) + Exercise( + exercise["name"], + exercise["sets"], + exercise["units"], + exercise["type"], + ) ) def __repr__(self): diff --git a/app/templates/workout/create.html b/app/templates/workout/create.html index 292aaa8..333aca6 100644 --- a/app/templates/workout/create.html +++ b/app/templates/workout/create.html @@ -18,6 +18,7 @@
{% for entry in form.exercises.entries %} + {{ entry['id']() }}
diff --git a/app/templates/workout/record.html b/app/templates/workout/record.html index d1662e3..d7e29f8 100644 --- a/app/templates/workout/record.html +++ b/app/templates/workout/record.html @@ -4,6 +4,7 @@ {% block form %} {% for exercise in workout.exercises %} +{% set outer_loop = loop %}

{{ exercise.name }}

@@ -17,8 +18,8 @@ {% for i in range(exercise.sets) %} - - + +
{{ i + 1 }} @@ -42,6 +43,15 @@ {% endfor %} +
+ +
+ {% endblock %} -- cgit v1.2.3