Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add methods and templates to view and edit ingredients
Roland Geider56338b557819
diff --git a/nutrition/models.py b/nutrition/models.pyindex 475fed792..95ddfe54a 100644--- a/nutrition/models.py+++ b/nutrition/models.py@@ -45,15 +45,18 @@ class Ingredient(models.Model):name = models.CharField(max_length=100,verbose_name = _('Name'),)- energy = models.IntegerField(blank = True,++ #language = models.ForeignKey(Language, verbose_name = _('Language'))++ energy = models.IntegerField(verbose_name = _('Energy'),help_text = _('In kcal per 100g'))- protein = models.FloatField(blank = True,+ protein = models.FloatField(verbose_name = _('Protein'),help_text = _('In g per 100g of product'))- carbohydrates = models.FloatField(blank = True,+ carbohydrates = models.FloatField(verbose_name = _('Carbohydrates'),help_text = _('In g per 100g of product'))@@ -62,16 +65,14 @@ class Ingredient(models.Model):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'))+ null = 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'))+ null = True,+ verbose_name = _('Natrium'),+ help_text = _('In g per 100g of product'))def __unicode__(self):"""Return a more human-readable representationdiff --git a/nutrition/templates/edit_ingredient.html b/nutrition/templates/edit_ingredient.htmlnew file mode 100644index 000000000..cb007db3c--- /dev/null+++ b/nutrition/templates/edit_ingredient.html@@ -0,0 +1,34 @@+{% 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.ingredient_edit ingredient.id %}"+ method="post"+ class="ym-form">+{% csrf_token %}+{{ form.non_field_errors }}+<table>+ {% include "form_snippet.html" with field=form.name %}+ {% include "form_snippet.html" with field=form.energy %}+ {% include "form_snippet.html" with field=form.protein %}+ {% include "form_snippet.html" with field=form.carbohydrates %}+ {% include "form_snippet.html" with field=form.fat %}+ {% include "form_snippet.html" with field=form.fibres %}+ {% include "form_snippet.html" with field=form.natrium %}+ <tr>+ <td><input type="submit" value="{% trans "Save" %}" /></td>+ <td></td>+ </tr>+</table>+</form>++++{% endblock %}diff --git a/nutrition/templates/ingredient_overview.html b/nutrition/templates/ingredient_overview.htmlnew file mode 100644index 000000000..9a6f667f0--- /dev/null+++ b/nutrition/templates/ingredient_overview.html@@ -0,0 +1,38 @@+{% extends "base.html" %}+{% load i18n %}+{% load staticfiles %}++{% block title %}{% trans "Ingredient overview" %}{% 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 "Ingredient overview" %}</h2>++<ul>+{% for ingredient in ingredients %}+ <li>+ <a href="{% url nutrition.views.ingredient_view ingredient.id %}">{{ ingredient.name }}</a>+ </li>+{% empty %}+ <li>+ <div class="box warning">+ {% trans "No ingredients." %} {% trans "Use link to add one." %}+ </div>+ </li>+{% endfor %}+</ul>++<a href="{% url nutrition.views.ingredient_edit None %}"+ class="modal-dialog">{% trans "Add ingredient" %}</a>+{% endblock %}diff --git a/nutrition/templates/view_ingredient.html b/nutrition/templates/view_ingredient.htmlnew file mode 100644index 000000000..b910aa001--- /dev/null+++ b/nutrition/templates/view_ingredient.html@@ -0,0 +1,99 @@+{% extends "base.html" %}+{% load i18n %}+{% load staticfiles %}++{% block title %}{{ingredient.name}}{% 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>{{ingredient.name}}</h2>+++<table>+ <thead>+ <tr>+ <th>+ {{ingredient.name}}+ </th>+ <th>+ <div class="editoptions">++ </div>+ </th>+ </tr>+ </thead>+ <tbody>+ <tr>+ <td>{% trans "Energy" %}</td>+ <td>{{ingredient.energy}} kcal</td>+ </tr>+ <tr>+ <td>{% trans "Protein" %}</td>+ <td>{{ingredient.protein}} g</td>+ </tr>+ <tr>+ <td>{% trans "Carbohydrates" %}</td>+ <td>{{ingredient.carbohydrates}} g</td>+ </tr>+ <tr>+ <td>{% trans "Fat" %}</td>+ <td>{{ingredient.fat}} g</td>+ </tr>+ <tr>+ <td>{% trans "Fibres" %}</td>+ <td>{{ingredient.fibres}} g</td>+ </tr>+ <tr>+ <td>{% trans "Natrium" %}</td>+ <td>{{ingredient.natrium}} g</td>+ </tr>+ </tbody>+</table>++{% endblock %}++++{% block sidebar %}+<h3>{% trans "Options" %}</h3>++<p>+<a href="{% url nutrition.views.delete_ingredient ingredient.id %}"+ title="{% trans 'Delete ingredient' %}">+ <img src="{% static 'images/icons/trash.svg' %}"+ width="32"+ height="32"+ alt="{% trans 'Delete ingredient' %}"+ title="{% trans 'Delete ingredient' %}">+ {% trans 'Delete ingredient' %}+</a>+</p>++<p>+<a href="{% url nutrition.views.ingredient_edit ingredient.id %}"+ title="{% trans 'Edit ingredient' %}"+ class="modal-dialog">+ <img src="{% static 'images/icons/edit.svg' %}"+ width="32"+ height="32"+ alt="{% trans 'Edit ingredient' %}"+ title="{% trans 'Edit ingredient' %}">+{% trans 'Edit ingredient' %}+</a>+</p>++{% endblock %}\ No newline at end of filediff --git a/nutrition/urls.py b/nutrition/urls.pyindex 746799114..17531c2f9 100644--- a/nutrition/urls.py+++ b/nutrition/urls.py@@ -2,14 +2,26 @@ from django.conf.urls import patterns, include, urlurlpatterns = patterns('nutrition.views',url(r'^nutrition/overview/$', 'overview'),++ # Plansurl(r'^nutrition/add/$', 'add'),url(r'^nutrition/(?P<id>\d+)/view/$', 'view'),url(r'^nutrition/(?P<id>\d+)/edit/plan/$', 'edit_plan'),++ # Mealsurl(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/delete/meal/(?P<id>\d+)$', 'delete_meal'),++ # Meal itemsurl(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'),++ # Ingredients+ url(r'^nutrition/ingredient/overview/$', 'ingredient_overview'),+ url(r'^nutrition/ingredient/view/(?P<id>\d+)$', 'ingredient_view'),+ url(r'^nutrition/ingredient/edit/(?P<id>\w*)$', 'ingredient_edit'),+ url(r'^nutrition/ingredient/delete/(?P<id>\w*)$', 'delete_ingredient'),#url(r'^nutrition/export_pdf/$', 'export_pdf'),diff --git a/nutrition/views.py b/nutrition/views.pyindex 923aff939..2101adc35 100644--- a/nutrition/views.py+++ b/nutrition/views.py@@ -26,6 +26,8 @@ from django.forms import ModelFormfrom nutrition.models import NutritionPlanfrom nutrition.models import Mealfrom nutrition.models import MealItem+from nutrition.models import Ingredient+from exercises.views import load_language@@ -288,5 +290,75 @@ def edit_meal_item(request, id, meal_id, item_id=None):template_data['form'] = meal_formreturn render_to_response('edit_meal_item.html',+ template_data,+ context_instance=RequestContext(request))++# ************************+# Ingredient functions+# ************************+def ingredient_overview(request):+ """Show an overview of all ingredients+ """+ template_data = {}+ template_data['active_tab'] = 'nutrition'++ ingredients = Ingredient.objects.all()+ template_data['ingredients'] = ingredients++ return render_to_response('ingredient_overview.html',+ template_data,+ context_instance=RequestContext(request))++def ingredient_view(request, id):+ template_data = {}+ template_data['active_tab'] = 'nutrition'++ ingredient = get_object_or_404(Ingredient, pk=id)+ template_data['ingredient'] = ingredient++ return render_to_response('view_ingredient.html',+ template_data,+ context_instance=RequestContext(request))++def delete_ingredient(request, id):+ pass++class IngredientForm(ModelForm):+ class Meta:+ model = Ingredient+ exclude=('language',)++def ingredient_edit(request, id=None):+ """Edit view for an ingredient+ """+ template_data = {}+ template_data['active_tab'] = 'nutrition'++ # Load the ingredient+ if id:+ ingredient = get_object_or_404(Ingredient, pk=id)+ template_data['ingredient'] = plan+ else:+ ingredient = Ingredient()++++ # Process request+ if request.method == 'POST':+ form = IngredientForm(request.POST, instance=ingredient)++ # If the data is valid, save and redirect+ if form.is_valid():+ ingredient = form.save(commit=False)+ #ingrent.order = 1+ ingredient.save()++ return HttpResponseRedirect('/nutrition/ingredient/view/%s' % ingredient.id)+ else:+ form = IngredientForm(instance=ingredient)+ template_data['form'] = form+++ return render_to_response('edit_ingredient.html',template_data,context_instance=RequestContext(request))\ No newline at end of file