From 5ee299ac8ce5da4457544ce06e134e18f784622a Mon Sep 17 00:00:00 2001 From: James Turk Date: Fri, 10 Apr 2015 12:28:25 -0400 Subject: [PATCH] add some new settings --- common.py | 20 +++++++++ lifting/templatetags/lifting.py | 11 ++--- lifting/utils.py | 11 ----- .../migrations/0002_auto_20150410_1627.py | 41 +++++++++++++++++++ profiles/models.py | 23 ++++++++++- 5 files changed, 85 insertions(+), 21 deletions(-) create mode 100644 common.py create mode 100644 profiles/migrations/0002_auto_20150410_1627.py diff --git a/common.py b/common.py new file mode 100644 index 0000000..1904ebc --- /dev/null +++ b/common.py @@ -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"))) diff --git a/lifting/templatetags/lifting.py b/lifting/templatetags/lifting.py index 1ff48f6..5b7f82e 100644 --- a/lifting/templatetags/lifting.py +++ b/lifting/templatetags/lifting.py @@ -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 diff --git a/lifting/utils.py b/lifting/utils.py index 4f37083..e69de29 100644 --- a/lifting/utils.py +++ b/lifting/utils.py @@ -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 diff --git a/profiles/migrations/0002_auto_20150410_1627.py b/profiles/migrations/0002_auto_20150410_1627.py new file mode 100644 index 0000000..6be8450 --- /dev/null +++ b/profiles/migrations/0002_auto_20150410_1627.py @@ -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), + ), + ] diff --git a/profiles/models.py b/profiles/models.py index 70d1a61..f3f0284 100644 --- a/profiles/models.py +++ b/profiles/models.py @@ -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)