Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add setting to make nutritional plans aware of the daily calories

Roland Geiderecd594684959
diff --git a/wger/nutrition/migrations/0008_auto__add_field_nutritionplan_has_goal_calories__chg_field_ingredient_.py b/wger/nutrition/migrations/0008_auto__add_field_nutritionplan_has_goal_calories__chg_field_ingredient_.py
new file mode 100644
index 000000000..afc32f171
--- /dev/null
+++ b/wger/nutrition/migrations/0008_auto__add_field_nutritionplan_has_goal_calories__chg_field_ingredient_.py
@@ -0,0 +1,130 @@
+# -*- coding: utf-8 -*-
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+ # Adding field 'NutritionPlan.has_goal_calories'
+ db.add_column(u'nutrition_nutritionplan', 'has_goal_calories',
+ self.gf('django.db.models.fields.BooleanField')(default=False),
+ keep_default=False)
+
+
+ # Changing field 'Ingredient.update_date'
+ db.alter_column(u'nutrition_ingredient', 'update_date', self.gf('django.db.models.fields.DateField')(auto_now=True))
+
+ def backwards(self, orm):
+ # Deleting field 'NutritionPlan.has_goal_calories'
+ db.delete_column(u'nutrition_nutritionplan', 'has_goal_calories')
+
+
+ # Changing field 'Ingredient.update_date'
+ db.alter_column(u'nutrition_ingredient', 'update_date', self.gf('django.db.models.fields.DateField')())
+
+ models = {
+ u'auth.group': {
+ 'Meta': {'object_name': 'Group'},
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
+ 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
+ },
+ u'auth.permission': {
+ 'Meta': {'ordering': "(u'content_type__app_label', u'content_type__model', u'codename')", 'unique_together': "((u'content_type', u'codename'),)", 'object_name': 'Permission'},
+ 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+ },
+ u'auth.user': {
+ 'Meta': {'object_name': 'User'},
+ 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+ 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+ 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+ 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
+ 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
+ },
+ u'contenttypes.contenttype': {
+ 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+ 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+ },
+ u'exercises.language': {
+ 'Meta': {'object_name': 'Language'},
+ 'full_name': ('django.db.models.fields.CharField', [], {'max_length': '30'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'short_name': ('django.db.models.fields.CharField', [], {'max_length': '2'})
+ },
+ u'nutrition.ingredient': {
+ 'Meta': {'ordering': "['name']", 'object_name': 'Ingredient'},
+ 'carbohydrates': ('django.db.models.fields.DecimalField', [], {'max_digits': '6', 'decimal_places': '3'}),
+ 'carbohydrates_sugar': ('django.db.models.fields.DecimalField', [], {'null': 'True', 'max_digits': '6', 'decimal_places': '3', 'blank': 'True'}),
+ 'creation_date': ('django.db.models.fields.DateField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'energy': ('django.db.models.fields.IntegerField', [], {}),
+ 'fat': ('django.db.models.fields.DecimalField', [], {'max_digits': '6', 'decimal_places': '3'}),
+ 'fat_saturated': ('django.db.models.fields.DecimalField', [], {'null': 'True', 'max_digits': '6', 'decimal_places': '3', 'blank': 'True'}),
+ 'fibres': ('django.db.models.fields.DecimalField', [], {'null': 'True', 'max_digits': '6', 'decimal_places': '3', 'blank': 'True'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'language': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['exercises.Language']"}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '200'}),
+ 'protein': ('django.db.models.fields.DecimalField', [], {'max_digits': '6', 'decimal_places': '3'}),
+ 'sodium': ('django.db.models.fields.DecimalField', [], {'null': 'True', 'max_digits': '6', 'decimal_places': '3', 'blank': 'True'}),
+ 'status': ('django.db.models.fields.CharField', [], {'default': "'1'", 'max_length': '2'}),
+ 'update_date': ('django.db.models.fields.DateField', [], {'auto_now': 'True', 'blank': 'True'}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']", 'null': 'True', 'blank': 'True'})
+ },
+ u'nutrition.ingredientweightunit': {
+ 'Meta': {'object_name': 'IngredientWeightUnit'},
+ 'amount': ('django.db.models.fields.DecimalField', [], {'default': '1', 'max_digits': '5', 'decimal_places': '2'}),
+ 'gramm': ('django.db.models.fields.IntegerField', [], {}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'ingredient': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['nutrition.Ingredient']"}),
+ 'unit': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['nutrition.WeightUnit']"})
+ },
+ u'nutrition.meal': {
+ 'Meta': {'ordering': "['time']", 'object_name': 'Meal'},
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'order': ('django.db.models.fields.IntegerField', [], {'max_length': '1', 'blank': 'True'}),
+ 'plan': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['nutrition.NutritionPlan']"}),
+ 'time': ('django.db.models.fields.TimeField', [], {'null': 'True', 'blank': 'True'})
+ },
+ u'nutrition.mealitem': {
+ 'Meta': {'object_name': 'MealItem'},
+ 'amount': ('django.db.models.fields.DecimalField', [], {'max_digits': '6', 'decimal_places': '2'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'ingredient': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['nutrition.Ingredient']"}),
+ 'meal': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['nutrition.Meal']"}),
+ 'order': ('django.db.models.fields.IntegerField', [], {'max_length': '1', 'blank': 'True'}),
+ 'weight_unit': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['nutrition.IngredientWeightUnit']", 'null': 'True', 'blank': 'True'})
+ },
+ u'nutrition.nutritionplan': {
+ 'Meta': {'ordering': "['-creation_date']", 'object_name': 'NutritionPlan'},
+ 'creation_date': ('django.db.models.fields.DateField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'description': ('django.db.models.fields.TextField', [], {'max_length': '2000', 'blank': 'True'}),
+ 'has_goal_calories': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'language': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['exercises.Language']"}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"})
+ },
+ u'nutrition.weightunit': {
+ 'Meta': {'ordering': "['name']", 'object_name': 'WeightUnit'},
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'language': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['exercises.Language']"}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '200'})
+ }
+ }
+
+ complete_apps = ['nutrition']
\ No newline at end of file
diff --git a/wger/nutrition/models.py b/wger/nutrition/models.py
index 8aaa5cd7d..b35cee589 100644
--- a/wger/nutrition/models.py
+++ b/wger/nutrition/models.py
@@ -62,6 +62,13 @@ class NutritionPlan(models.Model):
verbose_name=_('Description'),
help_text=_('A description of the goal of the plan, e.g. '
'"Gain mass" or "Prepare for summer"'))
+ has_goal_calories = models.BooleanField(verbose_name=_('Use daily calories'),
+ default=False,
+ help_text=_("Tick the box if you want to mark this "
+ "plan as having a goal amount of calories. "
+ "You can use the calculator or enter the "
+ "yourself."))
+ '''A flag indicating whether the plan has a goal amount of calories'''
def __unicode__(self):
'''
@@ -116,15 +123,16 @@ class NutritionPlan(models.Model):
goal_calories = self.user.userprofile.calories
actual_calories = self.get_nutritional_values()['energy']
- # Within 10%
- if (actual_calories < goal_calories * 1.10) and (actual_calories > goal_calories * 0.9):
+ # Within 3%
+ if (actual_calories < goal_calories * 1.03) and (actual_calories > goal_calories * 0.97):
return 1
- # within 20%
- elif (actual_calories < goal_calories * 1.20) and (actual_calories > goal_calories * 0.8):
+ # within 7%
+ elif (actual_calories < goal_calories * 1.07) and (actual_calories > goal_calories * 0.93):
return 2
- # within 30%
- elif (actual_calories < goal_calories * 1.30) and (actual_calories > goal_calories * 0.7):
+ # within 10%
+ elif (actual_calories < goal_calories * 1.10) and (actual_calories > goal_calories * 0.9):
return 3
+ # even more
else:
return 4
diff --git a/wger/nutrition/templates/plan/view.html b/wger/nutrition/templates/plan/view.html
index 771f25ff3..d5f670684 100644
--- a/wger/nutrition/templates/plan/view.html
+++ b/wger/nutrition/templates/plan/view.html
@@ -167,46 +167,67 @@ $(document).ready(function() {
{% block sidebar %}
<h3>{% trans "Nutritional data" %}</h3>
-<table id="nutritional-data-macronutrients">
+<table id="nutritional-data-energy">
<thead>
<tr>
<th colspan="2">
- {% trans "Macronutrients" %}
+ {% trans "Energy" %}
</th>
</tr>
</thead>
<tbody>
<tr>
<td>{% trans "Energy" %}</td>
- <td>{{nutritional_data.energy|floatformat}} kcal</td>
+ <td class="align-right">{{nutritional_data.energy|floatformat}} kcal</td>
</tr>
- {% if user.userprofile.calories %}
+ {% if user.userprofile.calories and plan.has_goal_calories %}
{% with total=plan.get_calories_approximation %}
- <tr style="background-color:{% if total == 1 %}#8ae234;{% elif total == 2 %}#fce94f;{% elif total == 3 %}#fcaf3e;{% elif total > 3 %}#ef2929;{% endif %}">
- <td>{{total}}</td>
- <td>{{user.userprofile.calories}} kcal</td>
+ <tr>
+ <td>{% trans "Goal" %}</td>
+ <td class="align-right" style="color:{% if total == 1 %}#8ae234;{% elif total == 2 %}#fce94f;{% elif total == 3 %}#fcaf3e;{% elif total > 3 %}#ef2929;{% endif %}">
+ <strong>{{user.userprofile.calories}} kcal</strong></td>
</tr>
{% endwith %}
{% endif %}
+ </tbody>
+</table>
+
+
+{% if plan.has_goal_calories %}
+ <p>{% blocktrans %}You have selected that this nutritional plan has a
+ goal amount of calories. Use the calculator or enter the value yourself.{% endblocktrans %}
+ <a href="{% url 'calories-calculator' %}">{% trans "That's done here" %}</a>
+ </p>
+{% endif %}
+
+<table id="nutritional-data-macronutrients">
+ <thead>
+ <tr>
+ <th colspan="2">
+ {% trans "Macronutrients" %}
+ </th>
+ </tr>
+ </thead>
+ <tbody>
<tr>
<td>{% trans "Protein" %}</td>
- <td>{{nutritional_data.protein|floatformat}} g</td>
+ <td class="align-right">{{nutritional_data.protein|floatformat}} g</td>
</tr>
<tr>
<td>{% trans "Carbohydrates" %}</td>
- <td>{{nutritional_data.carbohydrates|floatformat}} g</td>
+ <td class="align-right">{{nutritional_data.carbohydrates|floatformat}} g</td>
</tr>
<tr>
<td style="padding-left: 2em;">{% trans "Sugar content in carbohydrates" %}</td>
- <td>{{nutritional_data.carbohydrates_sugar|floatformat}} g</td>
+ <td class="align-right">{{nutritional_data.carbohydrates_sugar|floatformat}} g</td>
</tr>
<tr>
<td>{% trans "Fat" %}</td>
- <td>{{nutritional_data.fat|floatformat}} g</td>
+ <td class="align-right">{{nutritional_data.fat|floatformat}} g</td>
</tr>
<tr>
<td style="padding-left: 2em;">{% trans "Saturated fat content in fats" %}</td>
- <td>{{nutritional_data.fat_saturated|floatformat}} g</td>
+ <td class="align-right">{{nutritional_data.fat_saturated|floatformat}} g</td>
</tr>
</tbody>
</table>
@@ -222,11 +243,11 @@ $(document).ready(function() {
<tbody>
<tr>
<td>{% trans "Fibres" %}</td>
- <td>{{nutritional_data.fibres|floatformat}} g</td>
+ <td class="align-right">{{nutritional_data.fibres|floatformat}} g</td>
</tr>
<tr>
<td>{% trans "Sodium" %}</td>
- <td>{{nutritional_data.sodium|floatformat}} g</td>
+ <td class="align-right">{{nutritional_data.sodium|floatformat}} g</td>
</tr>
</tbody>
</table>
diff --git a/wger/nutrition/tests/test_plan.py b/wger/nutrition/tests/test_plan.py
index 4928dbac0..b776f88f3 100644
--- a/wger/nutrition/tests/test_plan.py
+++ b/wger/nutrition/tests/test_plan.py
@@ -13,11 +13,12 @@
# 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/>.
-from wger.nutrition.models import NutritionPlan
+from django.core.urlresolvers import reverse
+from wger.nutrition.models import NutritionPlan
from wger.manager.tests.testcase import WorkoutManagerDeleteTestCase
from wger.manager.tests.testcase import WorkoutManagerEditTestCase
-from wger.manager.tests.testcase import WorkoutManagerAddTestCase
+from wger.manager.tests.testcase import WorkoutManagerTestCase
class DeletePlanTestCase(WorkoutManagerDeleteTestCase):
@@ -39,3 +40,40 @@ class EditPlanTestCase(WorkoutManagerEditTestCase):
url = 'nutrition-edit'
pk = 2
data = {'description': 'My new description'}
+
+
+class PlanDailyCaloriesTestCase(WorkoutManagerTestCase):
+ '''
+ Tests the handling of the daily calories in the plan page
+ '''
+
+ def test_overview_no_calories(self):
+ '''
+ Tests the overview page with no daily calories set
+ '''
+
+ self.user_login('test')
+
+ # Can't find goal calories text
+ response = self.client.get(reverse('nutrition-view', kwargs={'id': 1}))
+ self.assertFalse(response.context['plan'].has_goal_calories)
+
+ self.assertEqual(response.status_code, 200)
+ self.assertNotContains(response, 'goal amount of calories')
+
+ def test_overview_calories(self):
+ '''
+ Tests the overview page with no daily calories set
+ '''
+
+ # Plan has daily calories goal
+ self.user_login('test')
+ plan = NutritionPlan.objects.get(pk=1)
+ plan.has_goal_calories = True
+ plan.save()
+
+ # Can find goal calories text
+ response = self.client.get(reverse('nutrition-view', kwargs={'id': 1}))
+ self.assertTrue(response.context['plan'].has_goal_calories)
+ self.assertEqual(response.status_code, 200)
+ self.assertContains(response, 'goal amount of calories')
diff --git a/wger/nutrition/urls.py b/wger/nutrition/urls.py
index 6763a4c4d..3bfa4e701 100644
--- a/wger/nutrition/urls.py
+++ b/wger/nutrition/urls.py
@@ -15,7 +15,9 @@ urlpatterns = patterns('wger.nutrition.views',
'plan.overview',
name='nutrition-overview'),
url(r'^add/$', 'plan.add'),
- url(r'^(?P<id>\d+)/view/$', 'plan.view'),
+ url(r'^(?P<id>\d+)/view/$',
+ 'plan.view',
+ name='nutrition-view'),
url(r'^(?P<pk>\d+)/copy/$',
'plan.copy',
name='nutrition-copy'),
diff --git a/wger/nutrition/views/plan.py b/wger/nutrition/views/plan.py
index c84f604bf..ecab04430 100644
--- a/wger/nutrition/views/plan.py
+++ b/wger/nutrition/views/plan.py
@@ -25,7 +25,6 @@ from django.core.context_processors import csrf
from django.core.urlresolvers import reverse
from django.core.urlresolvers import reverse_lazy
from django.contrib.auth.decorators import login_required
-from django.forms import ModelForm
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy
@@ -36,7 +35,6 @@ from wger.nutrition.models import NutritionPlan
from wger.nutrition.models import MEALITEM_WEIGHT_GRAM
from wger.nutrition.models import MEALITEM_WEIGHT_UNIT
-from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.pagesizes import A4, cm
from reportlab.platypus import Paragraph, SimpleDocTemplate, Table
from reportlab.lib import colors