Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Automatically save the weight entered when calculating the BMI

Roland Geider88fd5154ea66
diff --git a/wger/manager/models.py b/wger/manager/models.py
index 204668260..e14fe64d1 100644
--- a/wger/manager/models.py
+++ b/wger/manager/models.py
@@ -681,6 +681,16 @@ by the US Department of Agriculture. It is extremely complete, with around
'''
return u"Profile for user {0}".format(self.user)
+ def calculate_bmi(self):
+ '''
+ Calculates the user's BMI
+
+ Formula: weight/height^2
+ - weight in kg
+ - height in m
+ '''
+ return self.weight / (self.height/100.0 * self.height/100.0)
+
def calculate_basal_metabolic_rate(self, formula=1):
'''
Calculates the basal metabolic rate.
diff --git a/wger/nutrition/templates/bmi/form.html b/wger/nutrition/templates/bmi/form.html
index e03c1b99f..b253fef31 100644
--- a/wger/nutrition/templates/bmi/form.html
+++ b/wger/nutrition/templates/bmi/form.html
@@ -15,7 +15,6 @@ div.table > div {
div.table > div > div {
display:table-cell;
padding-left: 1em;
- /*width:50%;*/
}
div.bmi-legend {
width:1em;
@@ -88,61 +87,16 @@ $(document).ready(function() {
<div id="bmi-result-container" style="display:hidden;">
<h3>{% trans 'Results' %}</h3>
- <p>{% trans 'You BMI is: ' %} <span id="bmi-result-value">-/-</span></p>
+ <p>{% trans 'You BMI is: ' %} <strong><span id="bmi-result-value">{{user.userprofile.calculate_bmi|floatformat:2}}</span></strong></p>
</div>
-<div class="ym-grid linearize-level-1">
- <div class="ym-g75 ym-gl">
- <div class="ym-gbox-left">
- <img width="479"
- height="300"
- title="{% trans 'BMI' %}"
- alt="{% trans 'BMI' %}"
- src="{% static 'images/bmi.svg' %}"
- style="margin-top:3em;"></img>
- </div>
- </div>
- <div class="ym-g25 ym-gl">
- <div class="ym-gbox-left">
- <h3>{% trans "Legend" %}</h3>
- <div class="table">
- <div>
- <div><div class="bmi-legend" style="background-color:#800000;">&nbsp;</div></div>
- <div>{% trans "Adipositas Grad III" %}</div>
- </div>
- <div>
- <div><div class="bmi-legend" style="background-color:#ff0000;">&nbsp;</div></div>
- <div>{% trans "Adipositas Grad II" %}</div>
- </div>
- <div>
- <div><div class="bmi-legend" style="background-color:#ff7f2a;">&nbsp;</div></div>
- <div>{% trans "Adipositas Grad I" %}</div>
- </div>
- <div>
- <div><div class="bmi-legend" style="background-color:#ffff00;">&nbsp;</div></div>
- <div>{% trans "Übergewicht" %}</div>
- </div>
- <div>
- <div><div class="bmi-legend" style="background-color:#00ff00;">&nbsp;</div></div>
- <div>{% trans "Normalgewicht" %}</div>
- </div>
- <div>
- <div><div class="bmi-legend" style="background-color:#00ffff;">&nbsp;</div></div>
- <div>{% trans "Leichtes Untergewicht" %}</div>
- </div>
- <div>
- <div><div class="bmi-legend" style="background-color:#0000ff;">&nbsp;</div></div>
- <div>{% trans "Mäßiges Untergewicht" %}</div>
- </div>
- <div>
- <div><div class="bmi-legend" style="background-color:#000080;">&nbsp;</div></div>
- <div>{% trans "Starkes Untergewicht" %}</div>
- </div>
- </div>
- </div>
- </div>
-</div>
+<img width="479"
+ height="300"
+ title="{% trans 'BMI' %}"
+ alt="{% trans 'BMI' %}"
+ src="{% static 'images/bmi.svg' %}"
+ style="margin-top:3em;"></img>
@@ -150,35 +104,35 @@ $(document).ready(function() {
<div class="table">
<div>
<div><div class="bmi-legend" style="background-color:#800000;">&nbsp;</div></div>
- <div>{% trans "Adipositas Grad III" %}</div>
+ <div>{% trans "Adipositas III" %}</div>
</div>
<div>
- <div><div class="bmi-legend" style="background-color:#ff0000;">&nbsp;</div></div>
- <div>{% trans "Adipositas Grad II" %}</div>
+ <div><div class="bmi-legend" style="background-color:#ff0000;">&nbsp;</div></div>
+ <div>{% trans "Adipositas II" %}</div>
</div>
<div>
- <div><div class="bmi-legend" style="background-color:#ff7f2a;">&nbsp;</div></div>
- <div>{% trans "Adipositas Grad I" %}</div>
+ <div><div class="bmi-legend" style="background-color:#ff7f2a;">&nbsp;</div></div>
+ <div>{% trans "Adipositas I" %}</div>
</div>
<div>
- <div><div class="bmi-legend" style="background-color:#ffff00;">&nbsp;</div></div>
- <div>{% trans "Übergewicht" %}</div>
+ <div><div class="bmi-legend" style="background-color:#ffff00;">&nbsp;</div></div>
+ <div>{% trans "Overweight" %}</div>
</div>
<div>
- <div><div class="bmi-legend" style="background-color:#00ff00;">&nbsp;</div></div>
- <div>{% trans "Normalgewicht" %}</div>
+ <div><div class="bmi-legend" style="background-color:#00ff00;">&nbsp;</div></div>
+ <div>{% trans "Normal weight" %}</div>
</div>
<div>
- <div><div class="bmi-legend" style="background-color:#00ffff;">&nbsp;</div></div>
- <div>{% trans "Leichtes Untergewicht" %}</div>
+ <div><div class="bmi-legend" style="background-color:#00ffff;">&nbsp;</div></div>
+ <div>{% trans "Slight underweight" %}</div>
</div>
<div>
- <div><div class="bmi-legend" style="background-color:#0000ff;">&nbsp;</div></div>
- <div>{% trans "Mäßiges Untergewicht" %}</div>
+ <div><div class="bmi-legend" style="background-color:#0000ff;">&nbsp;</div></div>
+ <div>{% trans "Moderate underweight" %}</div>
</div>
<div>
- <div><div class="bmi-legend" style="background-color:#000080;">&nbsp;</div></div>
- <div>{% trans "Starkes Untergewicht" %}</div>
+ <div><div class="bmi-legend" style="background-color:#000080;">&nbsp;</div></div>
+ <div>{% trans "Strong underweight" %}</div>
</div>
</div>
@@ -188,4 +142,12 @@ $(document).ready(function() {
<!--
Side bar
-->
-{% block sidebar %}{% if sidebar %}{% include sidebar %}{% endif %}{% endblock %}
+{% block sidebar %}
+<h3>Info</h3>
+<p>
+{% blocktrans %}Use the form to calculate your BMI (Body Mass Index).
+If you have entered data in the weight section, the last entry will
+be used automatically. Otherwise the weight you enter here will be saved
+in a new entry.{% endblocktrans %}
+</p>
+{% endblock %}
diff --git a/wger/nutrition/templates/rate/form.html b/wger/nutrition/templates/rate/form.html
index 2e3f0e9bd..662b3310c 100644
--- a/wger/nutrition/templates/rate/form.html
+++ b/wger/nutrition/templates/rate/form.html
@@ -80,7 +80,7 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer et augue sem.
Nullam bibendum tortor a lacus luctus tempor. Suspendisse auctor tristique
metus, non ultricies quam.
</p>
- <p>{% trans 'You basic metabolic rate is: ' %} <span id="bmr-result-value">-/-</span></p>
+ <p>{% trans 'You basic metabolic rate is: ' %} <strong><span id="bmr-result-value">{{user.userprofile.calculate_basal_metabolic_rate|floatformat:2}}</span></strong></p>
</div>
@@ -118,7 +118,7 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer et augue sem.
Nullam bibendum tortor a lacus luctus tempor. Suspendisse auctor tristique
metus, non ultricies quam.
</p>
- <p>{% trans 'Factor for daily activities: ' %} <span id="activities-result-value">-/-</span></p>
+ <p>{% trans 'Factor for daily activities: ' %} <strong><span id="activities-result-value">{{user.userprofile.calculate_activities|floatformat:2}}</span></strong></p>
</div>
diff --git a/wger/nutrition/views/bmi.py b/wger/nutrition/views/bmi.py
index e40c9a796..4e988fbfc 100644
--- a/wger/nutrition/views/bmi.py
+++ b/wger/nutrition/views/bmi.py
@@ -15,6 +15,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
+import datetime
import logging
import json
@@ -25,6 +26,7 @@ from django.shortcuts import render_to_response
from django.utils.translation import ugettext as _
from wger.nutrition.forms import BmiForm
+from wger.weight.models import WeightEntry
logger = logging.getLogger('wger.custom')
@@ -40,7 +42,9 @@ def view(request):
'''
context = {}
- context['form'] = BmiForm()
+ form_data = {'height': request.user.userprofile.height,
+ 'weight': request.user.userprofile.weight}
+ context['form'] = BmiForm(initial=form_data)
return render_to_response('bmi/form.html',
context,
context_instance=RequestContext(request))
@@ -53,14 +57,22 @@ def calculate(request):
data = []
- form = BmiForm(request.POST)
+ form = BmiForm(request.POST, instance=request.user.userprofile)
if form.is_valid():
- height = form.cleaned_data['height'] / float(100)
- result = {'bmi': '{0:.2f}'.format(float(80) / (height * height))}
+ form.save()
+
+ # Create a new weight entry as needed
+ if (not WeightEntry.objects.all().exists()
+ or (datetime.date.today() - WeightEntry.objects.all().latest().creation_date > datetime.timedelta(7))):
+ entry = WeightEntry()
+ entry.weight = form.cleaned_data['weight']
+ entry.user = request.user
+ entry.creation_date = datetime.date.today()
+ entry.save()
+
+ bmi = request.user.userprofile.calculate_bmi()
+ result = {'bmi': '{0:.2f}'.format(bmi)}
data = json.dumps(result)
- else:
- logger.debug(form.errors)
- # Return the results to the server
- mimetype = 'application/json'
- return HttpResponse(data, mimetype)
+ # Return the results to the client
+ return HttpResponse(data, 'application/json')
diff --git a/wger/weight/models.py b/wger/weight/models.py
index b54257bd6..96081de35 100644
--- a/wger/weight/models.py
+++ b/wger/weight/models.py
@@ -25,7 +25,8 @@ class WeightEntry(models.Model):
'''
Model for a weight point
'''
- creation_date = models.DateField(verbose_name=_('Date'))
+ creation_date = models.DateField(verbose_name=_('Date'),
+ unique=True)
weight = models.FloatField(verbose_name=_('Weight'))
user = models.ForeignKey(User,
verbose_name=_('User'),