1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
from app import app, db
from datetime import datetime
from flask import render_template, redirect, request, url_for, flash
from flask_login import current_user, login_required
from forms import WorkoutCreateForm, WorkoutRecordForm, WorkoutEditForm
from tables import Exercise, User, Workout, WorkoutRecord, SetRecord
@app.route("/workout/create", methods=["GET", "POST"])
@login_required
def workout_create():
form = WorkoutCreateForm()
name = form.name.data
if form.validate_on_submit():
# Make sure the user doesn't already have a workout with this name
workout = Workout.query.filter_by(
user_id=current_user.id, name=name).first()
if not workout:
# TODO: add exercises
workout = Workout(
current_user, name, [e.data for e in form.exercises.entries]
)
db.session.add(workout)
db.session.commit()
return redirect(url_for("home"))
else:
flash("Workout with this name already exists", "danger")
else:
print(form.errors)
return render_template("workout/create.html", form=form)
@app.route("/workout/edit/<workout_id>", methods=["GET", "POST"])
@login_required
def workout_edit(workout_id=None):
# Validate Id
workout = Workout.query.filter_by(
id=int(workout_id), user_id=current_user.id
).first()
if not workout:
return redirect(url_for("home"))
form = WorkoutEditForm()
if form.validate_on_submit():
# Form has been submitted, write changes
for entry in form.exercises.entries:
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 name or (not sets and not units)):
workout.exercises.remove(exercise)
continue
if not exercise:
continue
# Update exercise
exercise.name = name
exercise.sets = sets
exercise.units = units
exercise.type = type
else:
# Create new exercise
if (name and sets and units and type):
workout.exercises.append(Exercise(name, sets, units, type))
# Write changes to database
db.session.commit()
return redirect(url_for("home"))
else:
form.name.data = workout.name
form.exercises.pop_entry() # TODO: better way to do this?
for exercise in workout.exercises:
form.exercises.append_entry(exercise)
return render_template("workout/create.html", form=form, workout=workout)
@app.route("/workout/record")
@login_required
def workout_record_select():
return render_template("workout/record_select.html")
@app.route("/workout/record/<workout_id>", methods=["GET", "POST"])
@login_required
def workout_record(workout_id=None):
form = WorkoutRecordForm()
# Matching workout required
workout = Workout.query.filter_by(
id=int(workout_id), user_id=current_user.id
).first()
if not workout:
return redirect(url_for("home"))
if form.validate_on_submit():
# Form has been submitted and is valid
workout_record = WorkoutRecord(workout, datetime.now())
# Interate over form exercise entries
for ee in form.exercises.entries:
id = ee.data["id"]
# And over that exercise's sets
for se in ee.sets.entries:
lbs = se.data["lbs"]
units = se.data["units"]
if lbs and units:
# Add the set to the workout record
workout_record.sets.append(
SetRecord(lbs=lbs, reps=units, exercise_id=id)
)
db.session.add(workout_record)
db.session.commit()
return redirect(url_for("home"))
else:
print(form.errors)
# Populate form with data
for exercise in workout.exercises:
form.exercises.append_entry(
{"id": exercise.id, "sets": [{}] * exercise.sets}
)
return render_template("workout/record.html", workout=workout, form=form)
@app.route("/workout/history/<record_id>")
@login_required
def workout_history(record_id=None):
query = (
db.session.query(SetRecord)
.join(WorkoutRecord)
.filter(
WorkoutRecord.id == SetRecord.workout_record_id,
WorkoutRecord.user_id == current_user.id,
WorkoutRecord.id == record_id,
)
.order_by(SetRecord.exercise_id)
.all()
)
if not query:
return redirect(url_for("home"))
# Condense into format we want
# TODO: this should be handled by an ExerciseRecord class, but I don't have time for it
exercises = {}
for set in query:
if set.exercise_id in exercises:
exercises[set.exercise_id]["sets"].append(set)
else:
exercises[set.exercise_id] = {
"sets": [set],
"name": set.exercise.name,
"id": int(set.exercise.id),
}
return render_template(
"workout/history.html",
exercises=exercises.values(),
workout_record=query[0].workout_record,
)
|