Self hosted FLOSS fitness/workout, nutrition and weight tracker
Show selected units of ingredients on nutritional plan
Roland Geideraedfe8e85045
diff --git a/wger/manager/static/js/workout-manager.js b/wger/manager/static/js/workout-manager.jsindex e96ec55ec..e2f67f837 100644--- a/wger/manager/static/js/workout-manager.js+++ b/wger/manager/static/js/workout-manager.js@@ -363,6 +363,7 @@ function init_ingredient_autocompleter()// After clicking on a result set the value of the hidden field$('#id_ingredient').val(ui.item.id);+ $('#exercise_name').html(ui.item.label);}});}diff --git a/wger/nutrition/models.py b/wger/nutrition/models.pyindex b7e075201..c88e9f6c1 100644--- a/wger/nutrition/models.py+++ b/wger/nutrition/models.py@@ -72,30 +72,36 @@ class NutritionPlan(models.Model):for meal in self.meal_set.select_related():for item in meal.mealitem_set.select_related():- # Don't proceed if the ingredient was input by hand or has freetext units- 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-- if item.ingredient.carbohydrates_sugar:- nutritional_info['carbohydrates_sugar'] += \- item.ingredient.carbohydrates_sugar * \- item.amount_gramm / 100-- nutritional_info['fat'] += item.ingredient.fat * item.amount_gramm / 100- if item.ingredient.fat_saturated:- nutritional_info['fat_saturated'] += item.ingredient.fat_saturated * \- item.amount_gramm / 100-- if item.ingredient.fibres:- nutritional_info['fibres'] += item.ingredient.fibres * \- item.amount_gramm / 100-- if item.ingredient.sodium:- nutritional_info['sodium'] += item.ingredient.sodium * \- item.amount_gramm / 100+ # Calculate the base weight of the item+ if item.weight_type == MEALITEM_WEIGHT_GRAM:+ item_weight = item.amount_gramm+ else:+ item_weight = (item.amount_gramm *+ float(item.weight_unit.amount) *+ item.weight_unit.gramm)++ nutritional_info['energy'] += item.ingredient.energy * item_weight / 100+ nutritional_info['protein'] += item.ingredient.protein * item_weight / 100+ nutritional_info['carbohydrates'] += item.ingredient.carbohydrates * \+ item_weight / 100++ if item.ingredient.carbohydrates_sugar:+ nutritional_info['carbohydrates_sugar'] += \+ item.ingredient.carbohydrates_sugar * \+ item_weight / 100++ nutritional_info['fat'] += item.ingredient.fat * item_weight / 100+ if item.ingredient.fat_saturated:+ nutritional_info['fat_saturated'] += item.ingredient.fat_saturated * \+ item_weight / 100++ if item.ingredient.fibres:+ nutritional_info['fibres'] += item.ingredient.fibres * \+ item_weight / 100++ if item.ingredient.sodium:+ nutritional_info['sodium'] += item.ingredient.sodium * \+ item_weight / 100return nutritional_infodef get_owner_object(self):diff --git a/wger/nutrition/templates/edit_meal_item.html b/wger/nutrition/templates/edit_meal_item.htmlindex ea7a45772..990a2ef65 100644--- a/wger/nutrition/templates/edit_meal_item.html+++ b/wger/nutrition/templates/edit_meal_item.html@@ -21,25 +21,18 @@ $(document).ready(function() {{% block content %}--<h2>{% trans "Edit meal item" %}</h2>-<form action="{% url 'wger.nutrition.views.ingredient.edit_meal_item' plan.id meal.id meal_item.id%}{% if js_active %}?js=on{% endif %}"method="post"class="ym-form">{% csrf_token %}{{ form.non_field_errors }}<table>- {% yaml_form_field form.amount_gramm %}{% if js_active %}- <input type="hidden" id="id_ingredient" name="ingredient" value="{{ingredient}}">- {% else %}- {% yaml_form_field form.amount_gramm %}- {% yaml_form_field form.ingredient %}- {% endif %}<tr><td style="text-align:right;">+ <label for="id_ingredient_searchfield">{% trans "Ingredient name" %}+ </label></td><td><div class="ym-fbox-text">@@ -48,8 +41,18 @@ $(document).ready(function() {name="ingredient_searchfield"value="{{ingredient_searchfield}}"></div>+ <input type="hidden" id="id_ingredient" name="ingredient" value="{{ingredient}}">+ <div id="exercise_name" class="ym-fbox-text"></div></td></tr>+ {% else %}+ {% yaml_form_field form.ingredient "ym-fbox-select" %}+ {% endif %}+++ {% yaml_form_field form.amount_gramm %}+ {% yaml_form_field form.weight_unit "ym-fbox-select" %}+ {% yaml_form_field form.weight_type "ym-fbox-select"%}<tr><td colspan="2"><input type="submit" value="{% trans 'Save' %}" id="form-save">diff --git a/wger/nutrition/templates/view_nutrition_plan.html b/wger/nutrition/templates/view_nutrition_plan.htmlindex b991370c1..da2b7fb47 100644--- a/wger/nutrition/templates/view_nutrition_plan.html+++ b/wger/nutrition/templates/view_nutrition_plan.html@@ -93,14 +93,14 @@ $(document).ready(function() {<ul>{% for item in meal.mealitem_set.select_related %}<li>- {% if item.amount_gramm %}- {{ item.amount_gramm }}g+ {{ item.amount_gramm }}+ {% if item.weight_type == MEALITEM_WEIGHT_GRAM %}+ g{% else %}- {{ item.amount_freetext }}+ {{ item.weight_unit.unit.name }}{% endif %}-{{ item.ingredient.name }}-+<span class="editoptions"><a href="{% url 'wger.nutrition.views.ingredient.delete_meal_item' item.id %}"title="{% trans 'Delete' %}"diff --git a/wger/nutrition/views/ingredient.py b/wger/nutrition/views/ingredient.pyindex 7af295a53..9a877e461 100644--- a/wger/nutrition/views/ingredient.py+++ b/wger/nutrition/views/ingredient.py@@ -52,8 +52,6 @@ from reportlab.lib import colorsfrom wger.manager.utils import load_languagefrom wger.manager.utils import load_ingredient_languages-from wger.nutrition.views.meal import MealItemForm-from wger.workout_manager import get_versionfrom wger.workout_manager.generic_views import YamlFormMixinfrom wger.workout_manager.generic_views import YamlDeleteMixin@@ -66,6 +64,12 @@ logger = logging.getLogger('workout_manager.custom')# ************************# Meal ingredient functions# ************************+class MealItemForm(ModelForm):+ class Meta:+ model = MealItem+ exclude = ('meal', 'order')++@login_requireddef delete_meal_item(request, item_id):'''diff --git a/wger/nutrition/views/meal.py b/wger/nutrition/views/meal.pyindex fcd78d6d5..084e5ba3a 100644--- a/wger/nutrition/views/meal.py+++ b/wger/nutrition/views/meal.py@@ -46,12 +46,6 @@ class MealForm(ModelForm):exclude = ('plan', 'order')-class MealItemForm(ModelForm):- class Meta:- model = MealItem- exclude = ('meal', 'order')--class MealCreateView(YamlFormMixin, CreateView):'''Generic view to add a new meal to a nutrition plandiff --git a/wger/nutrition/views/plan.py b/wger/nutrition/views/plan.pyindex edd1b74d9..92bfe988f 100644--- a/wger/nutrition/views/plan.py+++ b/wger/nutrition/views/plan.py@@ -32,6 +32,8 @@ from django.views.generic import DeleteViewfrom django.views.generic import UpdateViewfrom wger.nutrition.models import NutritionPlan+from wger.nutrition.models import MEALITEM_WEIGHT_GRAM+from wger.nutrition.models import MEALITEM_WEIGHT_UNITfrom reportlab.lib.styles import getSampleStyleSheetfrom reportlab.lib.pagesizes import A4, cm@@ -120,6 +122,8 @@ def view(request, id):# Load the language and pass it to the templatelanguage = load_language()template_data['language'] = language+ template_data['MEALITEM_WEIGHT_GRAM'] = MEALITEM_WEIGHT_GRAM+ template_data['MEALITEM_WEIGHT_UNIT'] = MEALITEM_WEIGHT_UNIT# Get the nutrional info@@ -233,7 +237,12 @@ def export_pdf(request, id):P = Paragraph('<para>%s</para>' % item.ingredient.name,styleSheet["Normal"])- data.append(["%sg" % item.amount_gramm, P])++ if item.weight_type == MEALITEM_WEIGHT_GRAM:+ unit_name = 'g'+ else:+ unit_name = ' ' + item.weight_unit.unit.name+ data.append(["{0}{1}".format(item.amount_gramm, unit_name), P])# Set general table styles