Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Use an external table to store ingredients with nutritional values (protein, etc.)

Roland Geiderf32b77eb5998
diff --git a/nutrition/models.py b/nutrition/models.py
index 75510dc8b..475fed792 100644
--- a/nutrition/models.py
+++ b/nutrition/models.py
@@ -39,6 +39,45 @@ class NutritionPlan(models.Model):
"""
return "Nutrition plan for %s, %s" % (self.user, self.creation_date)
+class Ingredient(models.Model):
+ """ An ingredient, with some approximate nutrition values
+ """
+
+ name = models.CharField(max_length=100,
+ verbose_name = _('Name'),)
+ energy = models.IntegerField(blank = True,
+ verbose_name = _('Energy'),
+ help_text = _('In kcal per 100g'))
+
+ protein = models.FloatField(blank = True,
+ verbose_name = _('Protein'),
+ help_text = _('In g per 100g of product'))
+
+ carbohydrates = models.FloatField(blank = True,
+ verbose_name = _('Carbohydrates'),
+ help_text = _('In g per 100g of product'))
+
+ fat = models.FloatField(blank = True,
+ verbose_name = _('Fat'),
+ help_text = _('In g per 100g of product'))
+
+ fibres = models.FloatField(blank = True,
+ verbose_name = _('Fibres'),
+ help_text = _('In g per 100g of product'))
+
+ fibres = models.FloatField(blank = True,
+ verbose_name = _('Fibres'),
+ help_text = _('In g per 100g of product'))
+
+ natrium = models.FloatField(blank = True,
+ verbose_name = _('Natrium'),
+ help_text = _('In g per 100g of product'))
+
+ def __unicode__(self):
+ """Return a more human-readable representation
+ """
+ return "%s" % (self.name, )
+
class Meal(models.Model):
""" A meal
@@ -65,7 +104,11 @@ class MealItem(models.Model):
blank = True,
verbose_name = _('Amount (freetext)'),
help_text = _('The amount in another unit, e.g. "a spoonfull" or "3 pieces"'))
- ingredient = models.CharField(max_length=100, verbose_name = _('Ingredient'))
+ ingredient_freetext = models.CharField(max_length=100,
+ blank = True,
+ verbose_name = _('Ingredient'),
+ help_text = _('If the ingredient is not in the system, enter the name here'))
+ ingredient = models.ForeignKey(Ingredient, verbose_name = _('Ingredient'))
def __unicode__(self):
"""Return a more human-readable representation
diff --git a/nutrition/templates/edit_meal_item.html b/nutrition/templates/edit_meal_item.html
index d18ad94ca..a5d50ced2 100644
--- a/nutrition/templates/edit_meal_item.html
+++ b/nutrition/templates/edit_meal_item.html
@@ -18,6 +18,7 @@
{% include "form_snippet.html" with field=form.amount_gramm %}
{% include "form_snippet.html" with field=form.amount_freetext %}
{% include "form_snippet.html" with field=form.ingredient %}
+ {% include "form_snippet.html" with field=form.ingredient_freetext %}
<tr>
<td><input type="submit" value="{% trans "Save" %}" /></td>
<td></td>
diff --git a/nutrition/templates/view_nutrition_plan.html b/nutrition/templates/view_nutrition_plan.html
index 69067d78c..cbe654b18 100644
--- a/nutrition/templates/view_nutrition_plan.html
+++ b/nutrition/templates/view_nutrition_plan.html
@@ -67,7 +67,7 @@ $(document).ready(function() {
{{ item.amount_freetext }}
{% endif %}
- {{ item.ingredient }}
+ {{ item.ingredient.name }}
<span class="editoptions">
<a href="{% url nutrition.views.delete_meal_item item.id %}"
@@ -124,6 +124,29 @@ $(document).ready(function() {
{% trans "Add a new meal" %}
</a>
+
+<table>
+ <thead>
+ <tr>
+ <th colspan="2">{% trans "Nutritional data" %}</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{% trans "Energy" %}</td>
+ <td>{{nutritonal_data.energy}} kcal</td>
+ </tr>
+ <tr>
+ <td>{% trans "Protein" %}</td>
+ <td>{{nutritonal_data.protein}} g</td>
+ </tr>
+ <tr>
+ <td>{% trans "Carbohydrates" %}</td>
+ <td>{{nutritonal_data.carbohydrates}} g</td>
+ </tr>
+ </tbody>
+</table>
+
{% endblock %}
diff --git a/nutrition/views.py b/nutrition/views.py
index e4b5e7d56..831dedf3e 100644
--- a/nutrition/views.py
+++ b/nutrition/views.py
@@ -73,6 +73,19 @@ def view(request, id):
plan = get_object_or_404(NutritionPlan, pk=id, user=request.user)
template_data['plan'] = plan
+ # Sum the nutrional info
+ nutritional_info = {'energy': 0,
+ 'protein': 0,
+ 'carbohydrates': 0}
+ for meal in plan.meal_set.select_related():
+ for item in meal.mealitem_set.select_related():
+ if item.ingredient and item.amount_gramm:
+ nutritional_info['energy'] += item.ingredient.energy * item.amount_gramm / 100
+ nutritional_info['protein'] += item.ingredient.protein * item.amount_gramm / 100
+ nutritional_info['carbohydrates'] += item.ingredient.carbohydrates * item.amount_gramm / 100
+ template_data['nutritonal_data'] = nutritional_info
+
+
return render_to_response('view_nutrition_plan.html',
template_data,
context_instance=RequestContext(request))