Self hosted FLOSS fitness/workout, nutrition and weight tracker
Commit first version of nutrition application
Roland Geider62abe1f8bcc4
diff --git a/manager/templates/base.html b/manager/templates/base.htmlindex bc8fec7d0..85c664c16 100644--- a/manager/templates/base.html+++ b/manager/templates/base.html@@ -49,6 +49,7 @@<li {% if active_tab == 'workout' %}class="active"{% endif %}><a href="/">{% trans "Workouts" %}</a></li><li {% if active_tab == 'exercises' %}class="active"{% endif %}><a href="/exercise/overview/">{% trans "Exercises" %}</a></li><li {% if active_tab == 'weight' %}class="active"{% endif %}><a href="/weight/overview/">{% trans "Weight" %} </a></li>+ <li {% if active_tab == 'nutrition' %}class="active"{% endif %}><a href="/nutrition/overview/">{% trans "Nutrition" %} </a></li></ul>diff --git a/nutrition/__init__.py b/nutrition/__init__.pynew file mode 100644index 000000000..e69de29bbdiff --git a/nutrition/models.py b/nutrition/models.pynew file mode 100644index 000000000..1e837185e--- /dev/null+++ b/nutrition/models.py@@ -0,0 +1,68 @@+# This file is part of Workout Manager.+#+# 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.+#+# 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+# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.+++from django.db import models++from exercises.models import Language++from django.contrib.auth.models import User+from django.utils.translation import ugettext as _+++class NutritionPlan(models.Model):+ """ A nutrition plan+ """++ user = models.ForeignKey(User, verbose_name = _('User'))+ language = models.ForeignKey(Language, verbose_name = _('Language'))+ creation_date = models.DateField(_('Creation date'), auto_now_add=True)++ def __unicode__(self):+ """Return a more human-readable representation+ """+ return "Nutrition plan for %s, %s" % (self.user, self.creation_date)+++class Meal(models.Model):+ """ A meal+ """++ plan = models.ForeignKey(NutritionPlan, verbose_name = _('Nutrition plan'))+ order = models.IntegerField(max_length = 1, blank = True, verbose_name = _('Order'))++ def __unicode__(self):+ """Return a more human-readable representation+ """+ return "%s Meal" % (self.order,)+++class MealItem(models.Model):+ """ An item (component) of a meal+ """++ meal = models.ForeignKey(Meal, verbose_name = _('Nutrition plan'))+ order = models.IntegerField(max_length = 1, blank = True, verbose_name = _('Order'))+ amount_gramm = models.IntegerField(max_length=4, blank = True, verbose_name = _('Amount in gramms'))+ amount_freetext = models.CharField(max_length=100,+ 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'))++ def __unicode__(self):+ """Return a more human-readable representation+ """+ return "%s %s %s" % (self.order, self.amount_gramm or self.amount_freetext, self.ingredient)\ No newline at end of filediff --git a/nutrition/templates/edit_meal.html b/nutrition/templates/edit_meal.htmlnew file mode 100644index 000000000..d18ad94ca--- /dev/null+++ b/nutrition/templates/edit_meal.html@@ -0,0 +1,30 @@+{% extends "base.html" %}+{% load i18n %}+{% load staticfiles %}++{% block title %}{% trans "Edit meal" %}{% endblock %}+++{% block content %}++<h2>{% trans "Edit meal" %}</h2>++<form action="{% url nutrition.views.edit_meal_item plan.id meal.id meal_item.id%}"+ method="post"+ class="ym-form">+{% csrf_token %}+{{ form.non_field_errors }}+<table>+ {% 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 %}+ <tr>+ <td><input type="submit" value="{% trans "Save" %}" /></td>+ <td></td>+ </tr>+</table>+</form>++++{% endblock %}diff --git a/nutrition/templates/nutrition_overview.html b/nutrition/templates/nutrition_overview.htmlnew file mode 100644index 000000000..adc690025--- /dev/null+++ b/nutrition/templates/nutrition_overview.html@@ -0,0 +1,27 @@+{% extends "base.html" %}+{% load i18n %}+{% load staticfiles %}++{% block title %}{% trans "Nutrition overview" %}{% endblock %}+++{% block content %}++<h2>{% trans "Your nutrition plans" %}</h2>++<ul>+{% for plan in plans %}+ <li>+ <a href="{% url nutrition.views.view plan.id %}">{{ plan }}</a>+ </li>+{% empty %}+ <li>+ <div class="box warning">+ {% trans "No nutrition plans." %} {% trans "Use link to add one." %}+ </div>+ </li>+{% endfor %}+</ul>++<a href="{% url nutrition.views.add %}">{% trans "Add nutrition plan" %}</a>+{% endblock %}diff --git a/nutrition/templates/view_nutrition_plan.html b/nutrition/templates/view_nutrition_plan.htmlnew file mode 100644index 000000000..a993d8cf9--- /dev/null+++ b/nutrition/templates/view_nutrition_plan.html@@ -0,0 +1,52 @@+{% extends "base.html" %}+{% load i18n %}+{% load staticfiles %}++{% block title %}{% trans "Details for nutrition plan" %}{% endblock %}+++{% block content %}++<h2>{% trans "Details for nutrition plan" %}</h2>++<table>+ <thead>+ <tr>+ <th colspan="2">header</th>+ </tr>+ </thead>+ <tbody>+{% for meal in plan.meal_set.select_related %}+ <tr>+ <td>+ <a href="{% url nutrition.views.edit_meal_item plan.id meal.id None %}">{{ forloop.counter }}</a>+ </td>+ <td>+ <ul>+ {% for item in meal.mealitem_set.select_related %}+ <li>+ {% if item.amount_gramm %}+ {{ item.amount_gramm }} gr.+ {% else %}+ {{ item.amount_freetext }}+ {% endif %}++ {{ item.ingredient }}+ </li>+ {% endfor %}+ </ul>+ </td>+ </tr>++{% empty %}+ <div class="box warning">+ {% trans "No meals for this plan." %} {% trans "Use link to add one." %}+ </div>++{% endfor %}+ </tbody>+</table>++<a href="{% url nutrition.views.edit_meal plan.id None %}">{% trans "Add a new meal" %}</a>++{% endblock %}diff --git a/nutrition/tests.py b/nutrition/tests.pynew file mode 100644index 000000000..501deb776--- /dev/null+++ b/nutrition/tests.py@@ -0,0 +1,16 @@+"""+This file demonstrates writing tests using the unittest module. These will pass+when you run "manage.py test".++Replace this with more appropriate tests for your application.+"""++from django.test import TestCase+++class SimpleTest(TestCase):+ def test_basic_addition(self):+ """+ Tests that 1 + 1 always equals 2.+ """+ self.assertEqual(1 + 1, 2)diff --git a/nutrition/urls.py b/nutrition/urls.pynew file mode 100644index 000000000..fff06bee2--- /dev/null+++ b/nutrition/urls.py@@ -0,0 +1,12 @@+from django.conf.urls import patterns, include, url++urlpatterns = patterns('nutrition.views',+ url(r'^nutrition/overview/$', 'overview'),+ url(r'^nutrition/add/$', 'add'),+ url(r'^nutrition/(?P<id>\d+)/view/$', 'view'),+ url(r'^nutrition/(?P<id>\d+)/edit/meal/(?P<meal_id>\w*)$', 'edit_meal'),+ url(r'^nutrition/(?P<id>\d+)/edit/meal/(?P<meal_id>\d+)/item/(?P<item_id>\w*)$', 'edit_meal_item'),++ #url(r'^nutrition/export_pdf/$', 'export_pdf'),++)diff --git a/nutrition/views.py b/nutrition/views.pynew file mode 100644index 000000000..cedf7c8d2--- /dev/null+++ b/nutrition/views.py@@ -0,0 +1,151 @@+# This file is part of Workout Manager.+#+# 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.+#+# 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 logging++from django.template import RequestContext+from django.shortcuts import render_to_response+from django.shortcuts import get_object_or_404+from django.http import HttpResponseRedirect+from django.http import HttpResponseForbidden+from django.core.context_processors import csrf+from django.contrib.auth.decorators import login_required+from django.forms import ModelForm+++from nutrition.models import NutritionPlan+from nutrition.models import Meal+from nutrition.models import MealItem++from exercises.views import load_language++logger = logging.getLogger('workout_manager.custom')+++@login_required+def overview(request):+ template_data = {}+ template_data.update(csrf(request))+ template_data['active_tab'] = 'nutrition'++ plans = NutritionPlan.objects.filter(user = request.user)+ template_data['plans'] = plans++ return render_to_response('nutrition_overview.html',+ template_data,+ context_instance=RequestContext(request))++class MealForm(ModelForm):+ class Meta:+ model = Meal+ exclude=('plan',)++class MealItemForm(ModelForm):+ class Meta:+ model = MealItem+ exclude=('meal', 'order')++@login_required+def add(request):+ """Add a new nutrition plan and redirect to its page+ """++ plan = NutritionPlan()+ plan.user = request.user+ plan.language = load_language()+ plan.save()++ return HttpResponseRedirect('/nutrition/%s/view/' % plan.id)+++@login_required+def view(request, id):+ """Show the nutrition plan with the given ID+ """+ template_data = {}+ template_data['active_tab'] = 'nutrition'++ plan = get_object_or_404(NutritionPlan, pk=id, user=request.user)+ template_data['plan'] = plan++ return render_to_response('view_nutrition_plan.html',+ template_data,+ context_instance=RequestContext(request))++@login_required+def edit_meal(request, id, meal_id=None):+ """Form to add a meal to a plan+ """+ template_data = {}+ template_data['active_tab'] = 'nutrition'++ # Load the plan+ plan = get_object_or_404(NutritionPlan, pk=id, user=request.user)+ template_data['plan'] = plan++ meal = Meal()+ meal.plan = plan+ meal.order = 1+ meal.save()++ return HttpResponseRedirect('/nutrition/%s/view/' % plan.id)+++@login_required+def edit_meal_item(request, id, meal_id, item_id=None):+ """Form to add a meal to a plan+ """+ template_data = {}+ template_data['active_tab'] = 'nutrition'++ # Load the plan+ plan = get_object_or_404(NutritionPlan, pk=id, user=request.user)+ template_data['plan'] = plan++ # Load the meal+ meal = get_object_or_404(Meal, pk=meal_id)+ template_data['meal'] = meal++ if meal.plan != plan:+ return HttpResponseForbidden()+++ # Load the meal item+ if not item_id or item_id == 'None':+ meal_item = MealItem()+ else:+ meal_item = get_object_or_404(MealItem, pk=item_id)++ template_data['meal_item'] = meal_item++++ # Process request+ if request.method == 'POST':+ meal_form = MealItemForm(request.POST, instance=meal_item)++ # If the data is valid, save and redirect+ if meal_form.is_valid():+ meal_item = meal_form.save(commit=False)+ meal_item.meal = meal+ meal_item.order = 1+ meal_item.save()++ return HttpResponseRedirect('/nutrition/%s/view/' % id)+ else:+ meal_form = MealItemForm(instance=meal_item)+ template_data['form'] = meal_form++ return render_to_response('edit_meal.html',+ template_data,+ context_instance=RequestContext(request))\ No newline at end of filediff --git a/workout_manager/settings.sample.py b/workout_manager/settings.sample.pyindex ea874f362..14f9e6ca1 100644--- a/workout_manager/settings.sample.py+++ b/workout_manager/settings.sample.py@@ -133,6 +133,7 @@ INSTALLED_APPS = ('manager','weight','exercises',+ 'nutrition',)# A sample logging configuration. The only tangible loggingdiff --git a/workout_manager/urls.py b/workout_manager/urls.pyindex 652e42bff..83fc21580 100644--- a/workout_manager/urls.py+++ b/workout_manager/urls.py@@ -8,4 +8,5 @@ urlpatterns = patterns('',url(r'^', include('manager.urls')),url(r'^', include('exercises.urls')),url(r'^', include('weight.urls')),+ url(r'^', include('nutrition.urls')),)\ No newline at end of file