add some new settings

This commit is contained in:
James Turk 2015-04-10 12:28:25 -04:00
parent 2a9bf4dc2b
commit 5ee299ac8c
5 changed files with 85 additions and 21 deletions

20
common.py Normal file
View File

@ -0,0 +1,20 @@
import decimal
def remove_exponent(d):
return d.quantize(decimal.Decimal(1)) if d == d.to_integral() else d.normalize()
def round_to(number, increment, up_threshold=0.95):
""" round number to nearest multiple of increment """
ratio = number/increment
whole = ratio.to_integral(rounding=decimal.ROUND_DOWN)
dec = ratio - whole
if dec >= up_threshold:
whole += 1
return whole * increment
def to_lb(weight_kg):
return remove_exponent(round_to(weight_kg * decimal.Decimal("2.2046"),
decimal.Decimal("0.125")))

View File

@ -1,13 +1,9 @@
import decimal
from django import template
from django.template.defaultfilters import stringfilter
from lifting.utils import round_to
from common import to_lb
register = template.Library()
def remove_exponent(d):
return d.quantize(decimal.Decimal(1)) if d == d.to_integral() else d.normalize()
class MassNode(template.Node):
def __init__(self, weight):
@ -17,8 +13,7 @@ class MassNode(template.Node):
try:
weight_kg = self.weight.resolve(context)
if context['user'].profile.lifting_units == 'i':
return remove_exponent(round_to(weight_kg * decimal.Decimal("2.2046"),
decimal.Decimal("0.125")))
return to_lb(weight_kg)
else:
return remove_exponent(weight_kg)
except template.VariableDoesNotExist:
@ -26,7 +21,7 @@ class MassNode(template.Node):
class MassLabelNode(template.Node):
def render(self, context):
return {'i': 'lbs', 'm': 'kg'}[context['user'].profile.lifting_units]
return {'i': 'lb', 'm': 'kg'}[context['user'].profile.lifting_units]
@register.tag

View File

@ -1,11 +0,0 @@
import decimal
def round_to(number, increment, up_threshold=0.95):
""" round number to nearest multiple of increment """
ratio = number/increment
whole = ratio.to_integral(rounding=decimal.ROUND_DOWN)
dec = ratio - whole
if dec >= up_threshold:
whole += 1
return whole * increment

View File

@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
from django.conf import settings
import django.contrib.postgres.fields
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('profiles', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Bar',
fields=[
('id', models.AutoField(auto_created=True, serialize=False, verbose_name='ID', primary_key=True)),
('name', models.CharField(max_length=100)),
('weight_kg', models.DecimalField(max_digits=7, decimal_places=3)),
('user', models.OneToOneField(related_name='bar', to=settings.AUTH_USER_MODEL)),
],
),
migrations.AddField(
model_name='profile',
name='plate_pairs',
field=django.contrib.postgres.fields.ArrayField(default=['45', '45', '25', '10', '5', '5', '2.5', '1.25'], base_field=models.DecimalField(max_digits=7, decimal_places=3), size=None),
),
migrations.AlterField(
model_name='profile',
name='lifting_units',
field=models.CharField(default='i', choices=[('m', 'Metric (kg)'), ('i', 'Imperial (lb)')], max_length=1),
),
migrations.AlterField(
model_name='profile',
name='user',
field=models.OneToOneField(related_name='profile', to=settings.AUTH_USER_MODEL),
),
]

View File

@ -1,12 +1,31 @@
from django.db import models
from django.contrib.auth.models import User
from django.contrib.postgres.fields import ArrayField
from common import to_lb
UNITS = (
('m', 'Metric (kg)'),
('i', 'Imperial (lbs)'),
('i', 'Imperial (lb)'),
)
class Profile(models.Model):
user = models.OneToOneField(User, related_name='profile')
lifting_units = models.CharField(max_length=1, choices=UNITS)
lifting_units = models.CharField(max_length=1, choices=UNITS, default='i')
plate_pairs = ArrayField(models.DecimalField(max_digits=7, decimal_places=3),
default=['45','45','25','10','5','5','2.5','1.25'])
class Bar(models.Model):
user = models.OneToOneField(User, related_name='bar')
name = models.CharField(max_length=100)
weight_kg = models.DecimalField(max_digits=7, decimal_places=3)
def __str__(self):
return '{} ({}lb / {}kg)'.format(self.name, self.weight_kg, self.weight_lb)
@property
def weight_lb(self):
return to_lb(self.weight_kg)