Self hosted FLOSS fitness/workout, nutrition and weight tracker
Some improvements to nutrition application
Roland Geider232980917949
diff --git a/nutrition/models.py b/nutrition/models.pyindex 1e837185e..a593072e1 100644--- a/nutrition/models.py+++ b/nutrition/models.py@@ -42,6 +42,7 @@ class Meal(models.Model):plan = models.ForeignKey(NutritionPlan, verbose_name = _('Nutrition plan'))order = models.IntegerField(max_length = 1, blank = True, verbose_name = _('Order'))+ time = models.TimeField(null = True, blank = True, verbose_name = _('Time (approx)'))def __unicode__(self):"""Return a more human-readable representationdiff --git a/nutrition/templates/edit_meal.html b/nutrition/templates/edit_meal.htmlindex d18ad94ca..ebc604321 100644--- a/nutrition/templates/edit_meal.html+++ b/nutrition/templates/edit_meal.html@@ -9,15 +9,13 @@<h2>{% trans "Edit meal" %}</h2>-<form action="{% url nutrition.views.edit_meal_item plan.id meal.id meal_item.id%}"+<form action="{% url nutrition.views.edit_meal plan.id meal.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 %}+ {% include "form_snippet.html" with field=form.time %}<tr><td><input type="submit" value="{% trans "Save" %}" /></td><td></td>diff --git a/nutrition/templates/edit_meal_item.html b/nutrition/templates/edit_meal_item.htmlnew file mode 100644index 000000000..d18ad94ca--- /dev/null+++ b/nutrition/templates/edit_meal_item.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/view_nutrition_plan.html b/nutrition/templates/view_nutrition_plan.htmlindex a993d8cf9..7bc5b6144 100644--- a/nutrition/templates/view_nutrition_plan.html+++ b/nutrition/templates/view_nutrition_plan.html@@ -5,6 +5,19 @@{% block title %}{% trans "Details for nutrition plan" %}{% endblock %}+{% block header %}+<script src="{% static 'js/jquery-ui.js' %}"></script>+<script src="{% static 'js/workout-manager.js' %}"></script>+<script>+$(document).ready(function() {+ // Init the modal dialog to edit the exercise+ form_modal_dialog();+});+</script>+{% endblock %}+++{% block content %}<h2>{% trans "Details for nutrition plan" %}</h2>@@ -19,7 +32,26 @@{% 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>+ {{ forloop.counter }}+ {% if meal.time %}<br>{{meal.time|time:"H:i"}}{% endif %}++ <div class="editoptions">+ <a href="{% url nutrition.views.delete_meal meal.id %}"+ title="{% trans 'Delete' %}">+ <img src="{% static 'images/icons/trash.svg' %}"+ width="16"+ height="16"+ alt="{% trans 'Delete' %}">+ </a>+ <a href="{% url nutrition.views.edit_meal plan.id meal.id %}"+ title="{% trans 'Edit' %}"+ class="modal-dialog">+ <img src="{% static 'images/icons/edit.svg' %}"+ width="16"+ height="16"+ alt="{% trans 'Edit' %}">+ </a>+ </div></td><td><ul>@@ -32,8 +64,39 @@{% endif %}{{ item.ingredient }}++ <span class="editoptions">+ <a href="{% url nutrition.views.delete_meal_item item.id %}"+ title="{% trans 'Delete' %}">+ <img src="{% static 'images/icons/trash.svg' %}"+ width="16"+ height="16"+ alt="{% trans 'Delete' %}">+ </a>+ <a href="{% url nutrition.views.edit_meal_item plan.id meal.id item.id %}"+ title="{% trans 'Edit' %}"+ class="modal-dialog">+ <img src="{% static 'images/icons/edit.svg' %}"+ width="16"+ height="16"+ alt="{% trans 'Edit' %}">+ </a>+ </span></li>+ {% empty %}+ <div class="box warning">+ {% trans "No items for this meal." %} {% trans "Use link to add one." %}+ </div>{% endfor %}+ <a href="{% url nutrition.views.edit_meal_item plan.id meal.id None %}"+ title="{% trans 'Add' %}"+ class="modal-dialog">+ <img src="{% static 'images/icons/edit.svg' %}"+ width="16"+ height="16"+ alt="{% trans 'Add' %}">+ {% trans "Add item to meal" %}+ </a></ul></td></tr>@@ -47,6 +110,6 @@</tbody></table>-<a href="{% url nutrition.views.edit_meal plan.id None %}">{% trans "Add a new meal" %}</a>+<a href="{% url nutrition.views.add_meal plan.id %}">{% trans "Add a new meal" %}</a>{% endblock %}diff --git a/nutrition/urls.py b/nutrition/urls.pyindex fff06bee2..6690aac09 100644--- a/nutrition/urls.py+++ b/nutrition/urls.py@@ -5,7 +5,10 @@ urlpatterns = patterns('nutrition.views',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+)/add/meal/$', 'add_meal'),url(r'^nutrition/(?P<id>\d+)/edit/meal/(?P<meal_id>\d+)/item/(?P<item_id>\w*)$', 'edit_meal_item'),+ url(r'^nutrition/delete/meal/item/(?P<item_id>\d+)$', 'delete_meal_item'),+ url(r'^nutrition/delete/meal/(?P<id>\d+)$', 'delete_meal'),#url(r'^nutrition/export_pdf/$', 'export_pdf'),diff --git a/nutrition/views.py b/nutrition/views.pyindex cedf7c8d2..19fd8656b 100644--- a/nutrition/views.py+++ b/nutrition/views.py@@ -45,16 +45,6 @@ def overview(request):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_requireddef add(request):"""Add a new nutrition plan and redirect to its page@@ -82,8 +72,63 @@ def view(request, id):template_data,context_instance=RequestContext(request))+# ************************+# Meal functions+# ************************++class MealForm(ModelForm):+ class Meta:+ model = Meal+ exclude=('plan',)++class MealItemForm(ModelForm):+ class Meta:+ model = MealItem+ exclude=('meal', 'order')+@login_required-def edit_meal(request, id, meal_id=None):+def edit_meal(request, id, meal_id):+ """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()+++ # Process request+ if request.method == 'POST':+ meal_form = MealForm(request.POST, instance=meal)++ # If the data is valid, save and redirect+ if meal_form.is_valid():+ meal_item = meal_form.save(commit=False)+ meal_item.order = 1+ meal_item.save()++ return HttpResponseRedirect('/nutrition/%s/view/' % id)+ else:+ meal_form = MealForm(instance=meal)+ template_data['form'] = meal_form+++ return render_to_response('edit_meal.html',+ template_data,+ context_instance=RequestContext(request))++++@login_required+def add_meal(request, id, meal_id=None):"""Form to add a meal to a plan"""template_data = {}@@ -99,7 +144,44 @@ def edit_meal(request, id, meal_id=None):meal.save()return HttpResponseRedirect('/nutrition/%s/view/' % plan.id)++@login_required+def delete_meal(request, id):+ """Deletes the meal with the given ID+ """++ # Load the meal+ meal = get_object_or_404(Meal, pk=id)+ plan = meal.plan+ # Only delete if the user is the owner+ if plan.user == request.user:+ meal.delete()+ return HttpResponseRedirect('/nutrition/%s/view/' % plan.id)+ else:+ return HttpResponseForbidden()++++# ************************+# Meal ingredient functions+# ************************++@login_required+def delete_meal_item(request, item_id):+ """Deletes the meal ingredient with the given ID+ """++ # Load the item+ item = get_object_or_404(MealItem, pk=item_id)+ plan = item.meal.plan++ # Only delete if the user is the owner+ if plan.user == request.user:+ item.delete()+ return HttpResponseRedirect('/nutrition/%s/view/' % plan.id)+ else:+ return HttpResponseForbidden()@login_requireddef edit_meal_item(request, id, meal_id, item_id=None):@@ -146,6 +228,6 @@ def edit_meal_item(request, id, meal_id, item_id=None):meal_form = MealItemForm(instance=meal_item)template_data['form'] = meal_form- return render_to_response('edit_meal.html',+ return render_to_response('edit_meal_item.html',template_data,context_instance=RequestContext(request))\ No newline at end of file