From aa1e7efff952db93297f4e4e18b7a15bfce5fb0e Mon Sep 17 00:00:00 2001 From: James Turk Date: Tue, 7 Apr 2015 17:33:05 -0400 Subject: [PATCH] all lifts view --- lifting/models.py | 3 +++ lifting/templatetags/lifting.py | 17 ++++++++++++-- lifting/urls.py | 2 ++ lifting/views.py | 17 +++++++++++++- templates/lifting/lift_list.html | 39 ++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 templates/lifting/lift_list.html diff --git a/lifting/models.py b/lifting/models.py index 1d69a2a..ee33358 100644 --- a/lifting/models.py +++ b/lifting/models.py @@ -11,6 +11,9 @@ SET_TYPES = ( class Exercise(models.Model): names = ArrayField(models.CharField(max_length=200)) + def display_name(self): + return self.names[0].title() + def __str__(self): return ', '.join(self.names) diff --git a/lifting/templatetags/lifting.py b/lifting/templatetags/lifting.py index abf98be..1ff48f6 100644 --- a/lifting/templatetags/lifting.py +++ b/lifting/templatetags/lifting.py @@ -5,6 +5,9 @@ from lifting.utils import round_to 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): @@ -14,12 +17,17 @@ class MassNode(template.Node): try: weight_kg = self.weight.resolve(context) if context['user'].profile.lifting_units == 'i': - return round_to(weight_kg * decimal.Decimal("2.2046"), decimal.Decimal("0.125")) + return remove_exponent(round_to(weight_kg * decimal.Decimal("2.2046"), + decimal.Decimal("0.125"))) else: - return weight_kg + return remove_exponent(weight_kg) except template.VariableDoesNotExist: return '' +class MassLabelNode(template.Node): + def render(self, context): + return {'i': 'lbs', 'm': 'kg'}[context['user'].profile.lifting_units] + @register.tag def mass_unit(parser, token): @@ -30,3 +38,8 @@ def mass_unit(parser, token): "{!r} tag requires exactly one argument".format(tag_name) ) return MassNode(weight) + + +@register.tag +def mass_label(parser, token): + return MassLabelNode() diff --git a/lifting/urls.py b/lifting/urls.py index 85e6d63..73f4685 100644 --- a/lifting/urls.py +++ b/lifting/urls.py @@ -7,6 +7,8 @@ urlpatterns = [ name='lifting-month'), url(r'^(?P\d{4})/(?P\d{1,2})/(?P\d{1,2})/$', views.day_lifts, name='lifting-day'), + url(r'^lifts/$', views.lift_list), + url(r'^lifts/(?P\d+)/$', views.by_lift), url(r'^fitnotes/$', views.fitnotes_upload), ] diff --git a/lifting/views.py b/lifting/views.py index b336086..f32ea02 100644 --- a/lifting/views.py +++ b/lifting/views.py @@ -8,9 +8,10 @@ from django.shortcuts import render from django import forms from django.contrib.auth.decorators import login_required from django.views.generic import dates +from django.db.models import Count, Max from . import importers -from .models import Set +from .models import Set, Exercise @login_required @@ -65,6 +66,20 @@ def day_lifts(request, year, month, day): }) +@login_required +def lift_list(request): + lifts = Exercise.objects.filter(sets__user=request.user).annotate( + total=Count('sets'), max_kg=Max('sets__weight_kg'), + last_date=Max('sets__date'), + ) + return render(request, 'lifting/lift_list.html', {'lifts': lifts}) + + +@login_required +def by_lift(request, lift_id): + pass + + class FitnotesUploadForm(forms.Form): file = forms.FileField() diff --git a/templates/lifting/lift_list.html b/templates/lifting/lift_list.html new file mode 100644 index 0000000..5c56df6 --- /dev/null +++ b/templates/lifting/lift_list.html @@ -0,0 +1,39 @@ +{% extends "base.html" %} +{% load lifting %} + +{% block content %} + +
+
+

All Lifts

+
+
+ +
+
+ + + + + + + + + + + + {% for lift in lifts %} + + + + + + + + {% endfor %} + +
ExerciseTotal SetsLast SetMax WeightEstimated 1RM
{{lift.display_name}}{{lift.total}}{{lift.last_date}}{% mass_unit lift.max_kg %} {% mass_label %}
+
+
+ +{% endblock %}