summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstilbruch <stilbruch@protonmail.com>2022-04-24 17:59:31 -0500
committerstilbruch <stilbruch@protonmail.com>2022-04-24 17:59:31 -0500
commit586285028845c8ed9a2e859f1266c233b8278311 (patch)
tree8a19025a0f2c9e1c2c9e93fce027fdfd02d5e821
parent7c9c647f0e22c42bcce8845f5d49808014d39d96 (diff)
downloadStrengthy-586285028845c8ed9a2e859f1266c233b8278311.tar.xz
Strengthy-586285028845c8ed9a2e859f1266c233b8278311.zip
Fix backend for workout editing
-rw-r--r--app/forms/workout.py17
-rw-r--r--app/routes/workout.py22
-rw-r--r--app/tables/workout.py20
-rw-r--r--app/templates/workout/create.html1
-rw-r--r--app/templates/workout/record.html14
5 files changed, 50 insertions, 24 deletions
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 @@
<div id="rows" class="field">
{% for entry in form.exercises.entries %}
+ {{ entry['id']() }}
<div class="field is-horizontal">
<div class="field-body">
<div class="field">
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 %}
<div>
<h1 id="{{ exercise.name }}" class="title is-4 has-text-black">{{ exercise.name }}</h1>
<table class="table is-fullwidth is-hoverable">
@@ -17,8 +18,8 @@
{% for i in range(exercise.sets) %}
<tr>
<th>{{ i + 1 }}</th>
- <td><input class="input" type="number"></td>
- <td><input class="input" type="number"></td>
+ <td><input class="input" name="exercises-{{ outer_loop.index0 }}-sets-{{ i }}-lbs" type="number"></td>
+ <td><input class="input" name="exercises-{{ outer_loop.index0 }}-sets-{{ i }}-reps" type="number"></td>
<td>
<a class="button" onClick="onClickSetCheck(this)">
<span class="icon is-small">
@@ -42,6 +43,15 @@
</div>
{% endfor %}
+<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>
+ <span>Finish Workout</span>
+ </button>
+</div>
+
<script async type="text/javascript" src="/static/js/record.js"></script>
{% endblock %}