2015-04-13 03:46:28 +00:00
|
|
|
from decimal import Decimal
|
2015-03-31 21:25:19 +00:00
|
|
|
from django.db import models
|
2015-04-01 21:57:30 +00:00
|
|
|
from django.contrib.auth.models import User
|
2015-04-01 20:29:45 +00:00
|
|
|
from django.contrib.postgres.fields import ArrayField
|
2015-04-10 21:30:35 +00:00
|
|
|
from inventory.models import Lift, Bar
|
2015-03-31 21:25:19 +00:00
|
|
|
|
|
|
|
SET_TYPES = (
|
|
|
|
('warmup', 'Warmup'),
|
|
|
|
('planned', 'Planned'),
|
|
|
|
)
|
|
|
|
|
2015-04-10 18:19:12 +00:00
|
|
|
UNITS = (
|
|
|
|
('m', 'Metric (kg)'),
|
|
|
|
('i', 'Imperial (lb)'),
|
|
|
|
)
|
2015-03-31 21:25:19 +00:00
|
|
|
|
|
|
|
|
2015-04-10 18:19:12 +00:00
|
|
|
class LiftingOptions(models.Model):
|
|
|
|
user = models.OneToOneField(User, related_name='lifting_options')
|
2015-04-07 21:33:05 +00:00
|
|
|
|
2015-04-10 18:19:12 +00:00
|
|
|
lifting_units = models.CharField(max_length=1, choices=UNITS, default='i')
|
2015-04-12 17:40:03 +00:00
|
|
|
default_bar = models.ForeignKey(Bar, default=1)
|
2015-04-10 18:19:12 +00:00
|
|
|
plate_pairs = ArrayField(models.DecimalField(max_digits=7, decimal_places=3),
|
|
|
|
default=['45','45','25','10','5','5','2.5','1.25'])
|
2015-04-01 18:43:47 +00:00
|
|
|
|
2015-04-13 03:46:28 +00:00
|
|
|
def plates_for_weight(self, weight):
|
|
|
|
side = []
|
2015-04-13 03:59:21 +00:00
|
|
|
w = Decimal(weight)
|
|
|
|
if self.lifting_units == 'i':
|
|
|
|
w -= self.default_bar.weight_lb
|
|
|
|
else:
|
|
|
|
w -= self.default_bar.weight_kg
|
|
|
|
initial_weight = w
|
2015-04-13 03:46:28 +00:00
|
|
|
available = sorted(self.plate_pairs, reverse=True)
|
|
|
|
while w and available:
|
|
|
|
plate = available.pop(0)
|
|
|
|
if plate * 2 <= w:
|
|
|
|
w -= plate * 2
|
|
|
|
side.append(plate)
|
|
|
|
if sum(side) * 2 != initial_weight:
|
|
|
|
raise ValueError('remaining weight {}'.format(initial_weight - sum(side) * 2))
|
|
|
|
return side
|
2015-03-31 21:25:19 +00:00
|
|
|
|
2015-06-16 21:03:04 +00:00
|
|
|
|
|
|
|
class LiftOptions(models.Model):
|
|
|
|
user = models.ForeignKey(User, related_name='lift_options')
|
|
|
|
lift = models.ForeignKey(Lift, related_name='options')
|
|
|
|
|
|
|
|
bar = models.ForeignKey(Bar, null=True)
|
|
|
|
extra_start_weight = models.DecimalField(max_digits=7, decimal_places=3, default=0)
|
|
|
|
increment = models.DecimalField(max_digits=5, decimal_places=3, default=5)
|
|
|
|
|
|
|
|
|
2015-03-31 21:25:19 +00:00
|
|
|
class Set(models.Model):
|
2015-04-01 21:57:30 +00:00
|
|
|
user = models.ForeignKey(User, related_name='sets')
|
2015-03-31 21:25:19 +00:00
|
|
|
date = models.DateField()
|
2015-04-10 18:19:12 +00:00
|
|
|
lift = models.ForeignKey(Lift, related_name='sets')
|
2015-03-31 21:25:19 +00:00
|
|
|
weight_kg = models.DecimalField(max_digits=7, decimal_places=3)
|
|
|
|
reps = models.PositiveIntegerField()
|
|
|
|
source = models.CharField(max_length=100)
|
2015-04-02 22:24:34 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2015-04-10 18:19:12 +00:00
|
|
|
return '{} - {} @ {}kg - {}'.format(self.lift, self.reps, self.weight_kg, self.date)
|