diff --git a/inventory/models.py b/inventory/models.py index 6ad588c..578d0f4 100644 --- a/inventory/models.py +++ b/inventory/models.py @@ -1,5 +1,5 @@ from django.db import models -from common import to_lb +from common import remove_exponent, to_lb class Bar(models.Model): @@ -7,7 +7,8 @@ class Bar(models.Model): 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) + return '{} ({}kg / {}lb)'.format(self.name, remove_exponent(self.weight_kg), + self.weight_lb) @property def weight_lb(self): @@ -17,6 +18,10 @@ class Bar(models.Model): class Lift(models.Model): name = models.CharField(max_length=200) + @property + def display_name(self): + return self.name + def __str__(self): return self.name diff --git a/lifting/templatetags/lifting.py b/lifting/templatetags/lifting.py index 79f17d4..01ca005 100644 --- a/lifting/templatetags/lifting.py +++ b/lifting/templatetags/lifting.py @@ -1,10 +1,13 @@ from django import template from django.template.defaultfilters import stringfilter -from common import to_lb +from common import to_lb, remove_exponent register = template.Library() +register.filter('decimal', remove_exponent) + + class MassNode(template.Node): def __init__(self, weight): self.weight = template.Variable(weight) diff --git a/lifting/urls.py b/lifting/urls.py index 691968e..82732dc 100644 --- a/lifting/urls.py +++ b/lifting/urls.py @@ -10,5 +10,5 @@ urlpatterns = [ url(r'^lifts/$', views.lift_list, name='lift-list'), url(r'^lifts/(?P\d+)/$', views.by_lift, name='lift-detail'), - url(r'^edit/$', views.edit_profile, name='edit-profile'), + url(r'^options/$', views.edit_options, name='edit-options'), ] diff --git a/lifting/views.py b/lifting/views.py index 04a20fa..b216f05 100644 --- a/lifting/views.py +++ b/lifting/views.py @@ -1,8 +1,9 @@ +import calendar import datetime +import decimal import os import tempfile -import calendar -from collections import defaultdict +from collections import defaultdict, Counter from django.shortcuts import render from django import forms @@ -10,8 +11,9 @@ from django.contrib.auth.decorators import login_required from django.views.generic import dates from django.db.models import Count, Max +from inventory.models import Lift, Bar from .models import Set -from inventory.models import Lift +from .forms import LiftingOptionsForm @login_required @@ -83,7 +85,25 @@ def by_lift(request, lift_id): @login_required -def edit_profile(request): - form = request.user.profile +def edit_options(request): + lifting_options = request.user.lifting_options - return render(request, 'profiles/edit.html', {'form': form}) + if request.method == 'POST': + print(request.POST) + plates = [] + for weight, number in zip(request.POST.getlist('plate_weight'), + request.POST.getlist('plate_number')): + if weight and number: + plates += [decimal.Decimal(weight)] * int(number) + + lifting_options.plate_pairs = plates + lifting_options.default_bar_id = int(request.POST.get('barbell')) + lifting_options.lifting_units = request.POST.get('lifting_units') + lifting_options.save() + + bars = Bar.objects.all() + plates = sorted(Counter(lifting_options.plate_pairs).items()) + + return render(request, 'profiles/edit.html', {'lifting_options': lifting_options, + 'bars': bars, 'plates': plates, + }) diff --git a/templates/profiles/edit.html b/templates/profiles/edit.html index b2f5942..2ae5cf1 100644 --- a/templates/profiles/edit.html +++ b/templates/profiles/edit.html @@ -1,7 +1,11 @@ {% extends "base.html" %} +{% load lifting %} {% block content %} +
+{% csrf_token %} +
@@ -37,22 +41,21 @@
- lbs + lb
- kg + kg
+ {% for bar in bars %}
- Men's Olympic (44lbs / 20kg) -
-
- Women's Olympic (33lbs / 15kg) + {{bar}}
+ {% endfor %}
@@ -63,37 +66,28 @@ Weight# of Pairs + {% for weight, n in plates %} - - + + + {% endfor %} - - - - - - - - - - - - - - - - - - + +
+
+
+
+ +
{% endblock %}