Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add a very simple nutrition diary overview for a day

Roland Geider79003d9be710
diff --git a/wger/nutrition/migrations/0003_auto_20160515_1523.py b/wger/nutrition/migrations/0003_auto_20160515_1523.py
new file mode 100644
index 000000000..6576fdb3a
--- /dev/null
+++ b/wger/nutrition/migrations/0003_auto_20160515_1523.py
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('nutrition', '0002_auto_20160510_1140'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='ingredient',
+ name='status',
+ field=models.CharField(editable=False, default='1', max_length=2, choices=[('1', 'Pending'), ('2', 'Accepted'), ('3', 'Declined'), ('4', 'Submitted by administrator'), ('5', 'System ingredient')]),
+ ),
+ migrations.AlterField(
+ model_name='logitem',
+ name='comment',
+ field=models.TextField(blank=True, null=True, verbose_name='Comment'),
+ ),
+ ]
diff --git a/wger/nutrition/models.py b/wger/nutrition/models.py
index 287cc1596..8ce6066cd 100644
--- a/wger/nutrition/models.py
+++ b/wger/nutrition/models.py
@@ -14,7 +14,9 @@
#
# 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 logging
+import datetime
from decimal import Decimal
from django.db import models
@@ -198,6 +200,28 @@ class NutritionPlan(models.Model):
else:
return 4
+ def get_logged_values(self, date=datetime.date.today):
+ '''
+ Sums the nutritional info of the items logged for the given date
+ '''
+ use_metric = self.user.userprofile.use_metric
+ unit = 'kg' if use_metric else 'lb'
+ result = {'energy': 0,
+ 'protein': 0,
+ 'carbohydrates': 0,
+ 'carbohydrates_sugar': 0,
+ 'fat': 0,
+ 'fat_saturated': 0,
+ 'fibres': 0,
+ 'sodium': 0}
+
+ # Perform the sums
+ for item in self.logitem_set.select_related():
+ values = item.get_nutritional_values(use_metric=use_metric)
+ for key in result.keys():
+ result[key] += values[key]
+ return result
+
@python_2_unicode_compatible
class Ingredient(AbstractLicenseModel, models.Model):
@@ -712,7 +736,8 @@ class LogItem(BaseMealItem, models.Model):
'''
comment = models.TextField(verbose_name=_('Comment'),
- blank=True)
+ blank=True,
+ null=True)
'''
Comment field, for additional information
'''
@@ -744,7 +769,7 @@ class LogItem(BaseMealItem, models.Model):
'''
Return a more human-readable representation
'''
- return u"Diary entry {0}g ingredient {1}".format(self.amount, self.ingredient_id)
+ return u"Diary entry for {}, plan {}".format(self.datetime, self.plan.pk)
def get_owner_object(self):
'''
diff --git a/wger/nutrition/templates/log/overview.html b/wger/nutrition/templates/log/overview.html
new file mode 100644
index 000000000..a7ac187aa
--- /dev/null
+++ b/wger/nutrition/templates/log/overview.html
@@ -0,0 +1,93 @@
+{% extends "base.html" %}
+{% load i18n staticfiles wger_extras django_bootstrap_breadcrumbs %}
+
+{# #}
+{# Title #}
+{# #}
+{% block title %}{% trans "Nutrition diary" %} - {{ date }}{% endblock %}
+
+
+
+{# #}
+{# Breadcrumbs #}
+{# #}
+{% block breadcrumbs %}
+ {{ block.super }}
+
+ {% breadcrumb "Nutrition plans" "nutrition:plan:overview" %}
+ {% breadcrumb plan plan.get_absolute_url %}
+ {% breadcrumb "Nutrition diary" "nutrition:log:overview" plan.pk %}
+{% endblock %}
+
+
+{# #}
+{# Content #}
+{# #}
+{% block content %}
+ <table class="table table-condensed">
+ <thead>
+ <tr>
+ <th>{% trans "Macronutrients" %}</th>
+ <th class="align-right">{% trans "Planned" %}</th>
+ <th class="align-right">{% trans "Logged" %}</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{% trans "Energy" %}</td>
+ <td class="align-right">{{ nutritional_data.total.energy|floatformat }} {% trans "kcal" %}</td>
+ <td class="align-right">{{ log_values.energy|floatformat }} {% trans "kcal" %}</td>
+ </tr>
+ <tr>
+ <td>{% trans "Protein" %}</td>
+ <td class="align-right">{{ nutritional_data.total.protein|floatformat }} {% trans_weight_unit 'g' owner_user %}</td>
+ <td class="align-right">{{ log_values.protein|floatformat }} {% trans_weight_unit 'g' owner_user %}</td>
+ </tr>
+ <tr>
+ <td>{% trans "Carbohydrates" %}</td>
+ <td class="align-right">{{ nutritional_data.total.carbohydrates|floatformat }} {% trans_weight_unit 'g' owner_user %}</td>
+ <td class="align-right">{{ log_values.carbohydrates|floatformat }} {% trans_weight_unit 'g' owner_user %}</td>
+ </tr>
+ <tr>
+ <td style="padding-left: 2em;">{% trans "Sugar content in carbohydrates" %}</td>
+ <td class="align-right">{{ nutritional_data.total.carbohydrates_sugar|floatformat }} {% trans_weight_unit 'g' owner_user %}</td>
+ <td class="align-right">{{ log_values.carbohydrates_sugar|floatformat }} {% trans_weight_unit 'g' owner_user %}</td>
+ </tr>
+ <tr>
+ <td>{% trans "Fat" %}</td>
+ <td class="align-right">{{ nutritional_data.total.fat|floatformat }} {% trans_weight_unit 'g' owner_user %}</td>
+ <td class="align-right">{{ log_values.fat|floatformat }} {% trans_weight_unit 'g' owner_user %}</td>
+ </tr>
+ <tr>
+ <td style="padding-left: 2em;">{% trans "Saturated fat content in fats" %}</td>
+ <td class="align-right">{{ nutritional_data.total.fat_saturated|floatformat }} {% trans_weight_unit 'g' owner_user %}</td>
+ <td class="align-right">{{ log_values.fat_saturated|floatformat }} {% trans_weight_unit 'g' owner_user %}</td>
+ </tr>
+
+
+ <tr>
+ <th colspan="3">{% trans "Others" %}</th>
+ </tr>
+ <tr>
+ <td>{% trans "Fibres" %}</td>
+ <td class="align-right">{{ nutritional_data.total.fibres|floatformat }} {% trans_weight_unit 'g' owner_user %}</td>
+ <td class="align-right">{{ log_values.fibres|floatformat }} {% trans_weight_unit 'g' owner_user %}</td>
+ </tr>
+ <tr>
+ <td>{% trans "Sodium" %}</td>
+ <td class="align-right">{{ nutritional_data.total.sodium|floatformat }} {% trans_weight_unit 'g' owner_user %}</td>
+ <td class="align-right">{{ log_values.sodium|floatformat }} {% trans_weight_unit 'g' owner_user %}</td>
+ </tr>
+ </tbody>
+</table>
+ {% for log in logs %}
+ {{ log }}
+ {% endfor %}
+{% endblock %}
+
+
+{# #}
+{# Side bar #}
+{# #}
+{% block sidebar%}
+{% endblock %}
diff --git a/wger/nutrition/templates/mobile/plan/view.html b/wger/nutrition/templates/mobile/plan/view.html
index 0419cde2f..90e004d6f 100644
--- a/wger/nutrition/templates/mobile/plan/view.html
+++ b/wger/nutrition/templates/mobile/plan/view.html
@@ -263,6 +263,9 @@ $(document).ready(function() {
{# Side bar #}
{# #}
{% block sidebar %}
+
+
+
<h4>{% trans "Energy" %}</h4>
<table id="nutritional-data-energy" class="table">
<thead>
diff --git a/wger/nutrition/templates/plan/view.html b/wger/nutrition/templates/plan/view.html
index b52470195..9d3022987 100644
--- a/wger/nutrition/templates/plan/view.html
+++ b/wger/nutrition/templates/plan/view.html
@@ -268,6 +268,8 @@ function custom_modal_init()
{# Side bar #}
{# #}
{% block sidebar %}
+<a href="{% url 'nutrition:log:overview' plan.pk %}">{% trans "Nutrition diary" %}</a>
+
<h4>{% trans "Energy" %}</h4>
<table class="table table-condensed" id="nutritional-data-energy">
<thead>
@@ -283,7 +285,8 @@ function custom_modal_init()
<tr style="background-color:{% if total == 1 %}#8ae234;{% elif total == 2 %}#fce94f;{% elif total == 3 %}#fcaf3e;{% elif total > 3 %}#ef2929;{% endif %}">
<td>{% trans "Goal" %}</td>
<td class="align-right">
- <strong>{{owner_user.userprofile.calories}} {% trans "kcal" %}</strong></td>
+ <strong>{{owner_user.userprofile.calories}} {% trans "kcal" %}</strong>
+ </td>
</tr>
{% endwith %}
{% endif %}
diff --git a/wger/nutrition/urls.py b/wger/nutrition/urls.py
index d768fc3f4..a61ab7b6a 100644
--- a/wger/nutrition/urls.py
+++ b/wger/nutrition/urls.py
@@ -30,7 +30,8 @@ from wger.nutrition.views import (
meal,
meal_item,
unit,
- unit_ingredient
+ unit_ingredient,
+ log
)
# sub patterns for nutritional plans
@@ -180,6 +181,16 @@ patterns_calories = [
name='activities'), # JS
]
+# sub patterns for calories dairy
+patterns_diary = [
+ url(r'^overview/(?P<pk>\d+)/(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})$',
+ log.overview,
+ name='overview'),
+ url(r'^overview/(?P<pk>\d+)$',
+ log.overview,
+ name='overview'),
+]
+
urlpatterns = [
url(r'^', include(patterns_plan, namespace="plan")),
@@ -190,4 +201,5 @@ urlpatterns = [
url(r'^unit-to-ingredient/', include(patterns_unit_ingredient, namespace="unit_ingredient")),
url(r'^calculator/bmi/', include(patterns_bmi, namespace="bmi")),
url(r'^calculator/calories/', include(patterns_calories, namespace="calories")),
+ url(r'^diary/', include(patterns_diary, namespace="log")),
]
diff --git a/wger/nutrition/views/log.py b/wger/nutrition/views/log.py
new file mode 100644
index 000000000..e2857c97e
--- /dev/null
+++ b/wger/nutrition/views/log.py
@@ -0,0 +1,52 @@
+# -*- coding: utf-8 -*-
+
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+import datetime
+import logging
+
+from django.shortcuts import render, get_object_or_404
+from django.contrib.auth.decorators import login_required
+
+from wger.nutrition.models import LogItem, NutritionPlan
+
+logger = logging.getLogger(__name__)
+
+
+@login_required
+def overview(request, pk, date=None):
+ '''
+ Shows an overview of the log for the given date
+ '''
+ context = {}
+
+ if not date:
+ date = datetime.date.today()
+
+ plan = get_object_or_404(NutritionPlan, pk=pk)
+ print(plan.get_logged_values(date))
+
+ # TODO: in django 1.9 use __date=date here
+ # logs = LogItem.objects.filter(datetime__year=date.year,
+ # datetime__month=date.month,
+ # datetime__day=date.day,
+ # plan=pk)
+ # context['logs'] = logs
+ context['plan'] = plan
+ context['date'] = date
+ context['log_values'] = plan.get_logged_values(date)
+ context['nutritional_data'] = plan.get_nutritional_values()
+
+ return render(request, 'log/overview.html', context)