-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
507 lines (393 loc) · 18.6 KB
/
models.py
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
from django.forms.models import model_to_dict
import numpy as np
import copy
import datetime
from django import utils
def calories_burned_walking(summary):
user = UserProfile.objects.filter(user_id=summary.user_id).first()
weight = WeightHistory.objects.filter(user_id=summary.user_id).last()
# height and weight are required to use this method
if user.height and weight:
# calculate the total distance
distance = 0
for exercise in summary.workoutdetail_set.all():
distance += exercise.distance
kmh = distance / summary.duration
mps = kmh / 3.6
# handle case with zero weight
if weight.weight == 0:
if user.gender == "M":
weight.weight = 85
else:
weight.weight = 70
calories_burned = (0.035 * weight.weight) + ((mps ** 2) / user.height) * (0.029) * (weight.weight)
else:
calories_burned = calories_by_mets(summary)
return calories_burned
def calories_burned_strength(summary):
# get maximum heart rate
user = UserProfile.objects.filter(user_id=summary.user_id).first()
# if no heartrate was specified we will estimate it
if summary.avg_heartrate == 0:
if user.gender == "M":
max_hr = (202 - (.55 * user.age))
else:
max_hr = (216 - (1.09 * user.age))
# figure out the heart rate
hr_multiplier = 0.5 + (summary.intensity * .075)
hr_for_intensity = max_hr * hr_multiplier
else:
hr_for_intensity = summary.avg_heartrate
weight = WeightHistory.objects.filter(user_id=summary.user_id).last()
# handle case with zero weight
if weight.weight == 0:
if user.gender == "M":
weight.weight = 85
else:
weight.weight = 70
if user.gender == "M":
calories_burned = ( (user.age * .2017) - (weight.weight * 0.09036) + (hr_for_intensity * 0.6309) - 55.0969 ) * summary.duration / 4.184
else:
calories_burned = ((user.age * 0.074) - (weight.weight * 0.05741) + (hr_for_intensity * 0.4472) - 20.4022) * summary.duration / 4.184
return calories_burned
def calories_burned_cardio(summary):
user = UserProfile.objects.filter(user_id=summary.user_id).first()
# if no heartrate was specified we will estimate it
if summary.avg_heartrate == 0:
if user.gender == "M":
max_hr = (202 - (.55 * user.age))
else:
max_hr = (216 - (1.09 * user.age))
# figure out the heart rate
hr_multiplier = 0.55 + (summary.intensity * .075)
hr_for_intensity = max_hr * hr_multiplier
# else we use the entered heart rate
else:
hr_for_intensity = summary.avg_heartrate
weight = WeightHistory.objects.filter(user_id=summary.user_id).last()
# handle case with zero weight
if weight.weight == 0:
if user.gender == "M":
weight.weight = 85
else:
weight.weight = 70
if user.gender == "M":
calories_burned = ((-55.0969 + (0.6309 * hr_for_intensity) + (0.1988 * weight.weight) + (0.2017 * user.age))/4.184) * summary.duration
else:
calories_burned = ((-20.4022 + (0.4472 * hr_for_intensity) - (0.1263 * weight.weight) + (0.074 * user.age))/4.184) * summary.duration
return calories_burned
def calories_for_exercise(exercise):
# get the mets for the exercise
if exercise.intensity == 0:
mets = exercise.exercise.low_mets / 2
elif exercise.intensity == 1:
mets = exercise.exercise.low_mets
elif exercise.intensity == 2:
mets = exercise.exercise.med_mets
elif exercise.intensity == 3:
mets = exercise.exercise.high_mets
# weight the mets by the duration or the number of total reps
if exercise.reps != 0 and exercise.sets != 0:
weight = exercise.reps * exercise.sets
elif exercise.duration != 0:
weight = exercise.duration
else:
weight = 1
return mets, weight
def calories_by_mets(summary):
# check if there are exercises associated with the workout
exercise_set = summary.workoutdetail_set
# if the summary has exercises we can use the mets to calculate the calories
if exercise_set.first() != None:
# get the info we need
user = UserProfile.objects.filter(user_id=summary.user_id).first()
base_mets = user.get_base_mets()
# calculate the mets as an average of the mets for each
# exercise at it's intensity
exercise_count = 0
mets_total = 0
for exercise in exercise_set.all():
exercise_mets, weight = calories_for_exercise(exercise)
mets_total += exercise_mets * weight
exercise_count += weight
mets = mets_total / exercise_count
calories_burned = mets * summary.duration * base_mets
# else we will use a different method
else:
if summary.type_id == 2:
calories_burned = calories_burned_strength(summary)
elif summary.type_id == 5:
calories_burned = calories_burned_walking(summary)
elif summary.type_id == 1:
calories_burned = calories_burned_cardio(summary)
else:
calories_burned = calories_burned_cardio(summary)
return calories_burned
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name="workout_user", on_delete=models.CASCADE)
gender = models.CharField(max_length=1, choices=(('M', 'Male'), ('F', 'Female')), default="M")
daily_target = models.IntegerField(default=30)
height = models.FloatField(default=177)
units = models.CharField(max_length=5, choices=(("cm", "cm"), ("in", "in")), default="cm")
unit_type = models.CharField(max_length=10, choices=(("metric", "metric"), ("imp", "imperial")), default="metric")
age = models.IntegerField(default=40)
birthdate = models.DateField(null=True, blank=True)
def __unicode__(self):
return self.user.first_name
def get_base_mets(self):
weight = WeightHistory.objects.filter(user_id=self.user_id).last()
base_mets = 3.5 * weight.weight / 200
return base_mets
def save(self, *args, **kwargs):
# calculate the age for the birthdate
today = datetime.date.today()
self.age = today.year - self.birthdate.year - ((today.month, today.day) < (self.birthdate.month, self.birthdate.day))
# save the relationship as normal
super(UserProfile, self).save(*args, **kwargs)
class BodyAreas(models.Model):
name = models.CharField(max_length=50)
order = models.IntegerField(default=0)
def __unicode__(self):
return self.name
def __str__(self):
return self.name
class Meta:
ordering = ['name']
verbose_name_plural = "Body Areas"
class WeightHistory(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
datetime = models.DateTimeField()
weight = models.FloatField(default=0)
units = models.CharField(max_length=5, choices=(("kg","kg"), ("lbs", "lbs")), default="kg")
bodyfat = models.FloatField(default=0)
class Meta:
ordering = ['datetime']
verbose_name = "Weight History"
verbose_name_plural = "Weight Histories"
class MuscleGroup(models.Model):
name = models.CharField(max_length=50)
color = models.CharField(max_length=12)
parent = models.ForeignKey("MuscleGroup", on_delete=models.DO_NOTHING, blank=True, null=True)
area = models.ForeignKey(BodyAreas, on_delete=models.DO_NOTHING, blank=True, null=True)
type = models.ForeignKey("ExerciseType", on_delete=models.DO_NOTHING, blank=True, null=True)
display_in_list = models.IntegerField(default=1)
super_group = models.IntegerField(default=0)
def __str__(self):
return self.name
def __unicode__(self):
return self.name
class Meta:
ordering = ['name']
class ExerciseType(models.Model):
name = models.CharField(max_length=50)
order = models.IntegerField(default=0)
def __unicode__(self):
return self.name
def __str__(self):
return self.name
class Meta:
ordering = ['order']
class Exercise(models.Model):
name = models.CharField(max_length=100)
main_group = models.ForeignKey(MuscleGroup, on_delete=models.CASCADE, related_name="main_group", blank=True, null=True)
group = models.ManyToManyField(MuscleGroup)
low_mets = models.FloatField(default=3)
med_mets = models.FloatField(default=5)
high_mets = models.FloatField(default=7)
type = models.ForeignKey(ExerciseType, on_delete=models.CASCADE, default=None, null=True)
approved = models.IntegerField(default=1)
def __str__(self):
return self.name
def __unicode__(self):
return self.name
class Meta:
ordering = ['name']
class WorkoutDetail(models.Model):
workout = models.ForeignKey("WorkoutSummary", on_delete=models.CASCADE)
exercise = models.ForeignKey(Exercise, on_delete=models.CASCADE)
reps = models.IntegerField(default=0)
sets = models.IntegerField(default=0)
weight = models.FloatField(default=0)
units = models.CharField(max_length=5, choices=(("kg", "kg"), ("lbs", "lbs")), default="kg")
duration = models.FloatField(default=0)
intensity = models.IntegerField(choices=((0, 'Very Low'), (1, 'Low'), (2, 'Moderate'), (3, 'High')), default=2)
calories = models.FloatField(default=0)
distance = models.FloatField(default=0)
def save(self, *args, **kwargs):
# calculate the calories burned during this exercise session
exercise_mets, _ = calories_for_exercise(self)
user = UserProfile.objects.filter(user_id=self.workout.user_id).first()
base_mets = user.get_base_mets()
calories_burned = exercise_mets * self.duration * base_mets
self.calories = calories_burned
# save the relationship as normal
super(WorkoutDetail, self).save(*args, **kwargs)
# update the parent summary with the new caloires
self.workout.save()
def __str__(self):
return str(self.workout.start) + " - " + self.workout.group.name + " - " + self.exercise.name
def __unicode__(self):
return str(self.workout.start) + " - " + self.workout.group.name + " - " + self.exercise.name
class Meta:
pass
# Create your models here.
class WorkoutSummary(models.Model):
start = models.DateTimeField(default=utils.timezone.now)
duration = models.IntegerField(default=0)
type = models.ForeignKey(ExerciseType, on_delete=models.DO_NOTHING)
group = models.ForeignKey(MuscleGroup, on_delete=models.DO_NOTHING)
calories = models.IntegerField(default=0, blank=True, null=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
intensity = models.IntegerField(choices=((0, 'Very Low'), (1, 'Low'), (2, 'Moderate'), (3, 'High')), default=2)
calculated_calories = models.IntegerField(default=0)
avg_heartrate = models.IntegerField(default=0)
notes = models.TextField(blank=True, null=True)
def save(self, *args, **kwargs):
# if no duration was entered use the sum of the durations for the exercises
if self.duration == 0:
print("No duration!")
calculated_duration = 0
exercises = self.workoutdetail_set.all()
for exercise in exercises:
calculated_duration += exercise.duration
self.duration = calculated_duration
# calculate the calories burned during this exercise sesssion
self.calculated_calories = calories_by_mets(self)
self.calories = self.calculated_calories
# save the relationship as normal
super(WorkoutSummary, self).save(*args, **kwargs)
@staticmethod
def strength_training_history(user, end_date=None, start_date=None, group=None):
if start_date is None:
start_date = (datetime.datetime.now() - datetime.timedelta(days=15)).date()
if end_date is None:
end_date = datetime.datetime.now().date()
# we already added a day in the view, no need to add another
# end_date = (end_date + datetime.timedelta(days=1))
upper_body = False
workouts = WorkoutSummary.objects.filter(user=user).filter(type_id=2).filter(start__gte=start_date).filter(start__lte=end_date).order_by("-start")
if group is not None and group != "Upper Body":
workouts = workouts.filter(group__name=group)
elif group == "Upper Body":
upper_body = True
workouts = workouts.order_by("start")
workout_dict = {}
groups = {}
dates = []
blanks = []
# create our list of dates - we will show all dates, not just those where exercise was performed
current_date = start_date
while(current_date <= end_date):
date = str(current_date.year) + "-" + str(current_date.month).zfill(2) + "-" + str(current_date.day).zfill(2)
dates.append(date)
current_date = (current_date + datetime.timedelta(days=1))
blanks.append(None)
# remove the last day since that is the day after the end of the period
dates.pop()
# handle case for upper body
if upper_body:
child_groups = MuscleGroup.objects.filter(parent__name="Upper Body")
groups = {}
for group in child_groups:
groups[group.name] = group.color
for workout in workouts:
group = workout.group.name
if group not in workout_dict:
if not upper_body:
workout_dict[group] = {'avg_weight': copy.copy(blanks), 'max_weight': copy.copy(blanks), 'total_weight': copy.copy(blanks),'total_reps': copy.copy(blanks), 'total_sets': copy.copy(blanks)}
groups[group] = workout.group.color
else:
if group in groups:
workout_dict[group] = {'avg_weight': copy.copy(blanks), 'max_weight': copy.copy(blanks),
'total_weight': copy.copy(blanks), 'total_reps': copy.copy(blanks),
'total_sets': copy.copy(blanks)}
for workout in workouts:
group = workout.group.name
if group in groups:
exercises = workout.workoutdetail_set.all()
date = str(workout.start.year) + "-" + str(workout.start.month).zfill(2) + "-" + str(workout.start.day).zfill(2)
total_weight = 0
total_sets = 0
total_reps = 0
weights = []
for exercise in exercises:
weights.append(exercise.weight)
total_weight += (exercise.reps * exercise.sets * exercise.weight)
total_sets += exercise.sets
total_reps += exercise.sets * exercise.reps
idx = dates.index(date)
if len(weights):
workout_dict[group]['max_weight'][idx] = np.max(weights)
workout_dict[group]['avg_weight'][idx] = round(np.mean(weights), 2)
workout_dict[group]['total_weight'][idx] = total_weight
workout_dict[group]['total_sets'][idx] = total_sets
workout_dict[group]['total_reps'][idx] = total_reps
return workout_dict, groups, dates
@staticmethod
def summary_breakdown(user, end_date=None, start_date=None):
if start_date is None:
start_date = (datetime.datetime.now() - datetime.timedelta(days=7)).date()
if end_date is None:
end_date = (datetime.datetime.now() + datetime.timedelta(days=1)).date()
end_date = (end_date + datetime.timedelta(days=1))
# get the number of days to be displayed
n_days = (end_date - start_date).days
workouts = WorkoutSummary.objects.filter(user=user).filter(start__gte=start_date).filter(start__lte=end_date).order_by("-start")
workout_dict = {}
for workout in workouts:
if workout.type.name not in workout_dict:
workout_dict[workout.type.name] = {'minutes' : [workout.duration], 'calories': [workout.calories]}
else:
workout_dict[workout.type.name]['minutes'].append(workout.duration)
workout_dict[workout.type.name]['calories'].append(workout.calories)
return workout_dict
@staticmethod
def workouts_by_day(user, end_date=None, start_date=None):
if start_date is None:
start_date = (datetime.datetime.now() - datetime.timedelta(days=7)).date()
if end_date is None:
end_date = datetime.datetime.now().date()
end_date = (end_date + datetime.timedelta(days=1))
workouts = WorkoutSummary.objects.filter(user=user).filter(start__gte=start_date).filter(start__lte=end_date)#.exclude(group_id=24)
workout_dict = {}
for workout in workouts:
date = str(workout.start.year) + "-" + str(workout.start.month).zfill(2) + "-" + str(workout.start.day).zfill(2)
if date not in workout_dict:
workout_dict[date] = [workout]
else:
workout_dict[date].append(workout)
return workout_dict
@staticmethod
def summary_by_day(user, end_date=None, start_date=None):
if start_date is None:
start_date = (datetime.datetime.now() - datetime.timedelta(days=7)).date()
if end_date is None:
end_date = (datetime.datetime.now() + datetime.timedelta(days=1)).date()
end_date = (end_date + datetime.timedelta(days=1))
workouts = WorkoutSummary.objects.filter(user=user).filter(start__gte=start_date).filter(start__lte=end_date).order_by("-start")
dates = []
calories = []
minutes = []
workout_dict = {}
for workout in workouts:
date = str(workout.start.year) + "-" + str(workout.start.month).zfill(2) + "-" + str(workout.start.day).zfill(2)
if date not in dates:
calories.append(workout.calories)
minutes.append(workout.duration)
else:
calories[-1] += workout.calories
minutes[-1] += workout.duration
if date not in dates:
dates.append(date)
return { 'dates': dates, 'calories': calories, 'minutes': minutes }
def __str__(self):
return str(self.start) + " - " + self.group.name
def __unicode__(self):
return str(self.start) + " - " + self.group.name
class Meta:
ordering = ['-start']
verbose_name_plural = "Workout Summaries"