Self hosted FLOSS fitness/workout, nutrition and weight tracker
First commit to add support for 'human friendly' weight units
Roland Geiderddc0ccc20f65
diff --git a/wger/manager/templates/template.html b/wger/manager/templates/template.htmlindex d976c58b0..a06022ec6 100644--- a/wger/manager/templates/template.html+++ b/wger/manager/templates/template.html@@ -116,7 +116,7 @@class="dropdown-toggle"data-toggle="dropdown">{% trans "Weight" %} <b class="caret"></b></a><ul class="dropdown-menu">- <li><a href="{% url 'wger.weight.views.overview' %}">{% trans "Weight overview" %} </a></li>+ <li><a href="{% url 'wger.weight.views.overview' %}">{% trans "Weight overview" %}</a></li><li><a href="{% url 'weight-add' %}">{% trans "Add weight entry" %}</a></li></ul></li>@@ -128,9 +128,12 @@data-toggle="dropdown">{% trans "Nutrition" %} <b class="caret"></b></a><ul class="dropdown-menu">{% if user.is_authenticated %}- <li><a href="{% url 'wger.nutrition.views.overview' %}">{% trans "Nutrition plans" %} </a></li>+ <li><a href="{% url 'wger.nutrition.views.overview' %}">{% trans "Nutrition plans" %}</a></li>{% endif %}<li><a href="{% url 'wger.nutrition.views.ingredient_overview' %}">{% trans "Ingredient overview" %}</a></li>+ {% if perms.exercises %}+ <li><a href="{% url 'weight-unit-list' %}">{% trans "Ingredient weight units" %}</a></li>+ {% endif %}</ul></li>diff --git a/wger/nutrition/migrations/0002_auto__add_ingredientweightunit__add_weightunit__add_field_mealitem_wei.py b/wger/nutrition/migrations/0002_auto__add_ingredientweightunit__add_weightunit__add_field_mealitem_wei.pynew file mode 100644index 000000000..07bcd8d37--- /dev/null+++ b/wger/nutrition/migrations/0002_auto__add_ingredientweightunit__add_weightunit__add_field_mealitem_wei.py@@ -0,0 +1,150 @@+# -*- 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 model 'IngredientWeightUnit'+ db.create_table('nutrition_ingredientweightunit', (+ ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),+ ('ingredient', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['nutrition.Ingredient'])),+ ('unit', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['nutrition.WeightUnit'])),+ ('gramm', self.gf('django.db.models.fields.IntegerField')()),+ ('amount', self.gf('django.db.models.fields.DecimalField')(max_digits=5, decimal_places=2)),+ ))+ db.send_create_signal('nutrition', ['IngredientWeightUnit'])++ # Adding model 'WeightUnit'+ db.create_table('nutrition_weightunit', (+ ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),+ ('name', self.gf('django.db.models.fields.CharField')(max_length=200)),+ ))+ db.send_create_signal('nutrition', ['WeightUnit'])++ # Adding field 'MealItem.weight_unit'+ db.add_column('nutrition_mealitem', 'weight_unit',+ self.gf('django.db.models.fields.related.ForeignKey')(to=orm['nutrition.IngredientWeightUnit'], null=True),+ keep_default=False)++ # Adding field 'MealItem.weight_type'+ db.add_column('nutrition_mealitem', 'weight_type',+ self.gf('django.db.models.fields.CharField')(default='1', max_length=2),+ keep_default=False)+++ def backwards(self, orm):+ # Deleting model 'IngredientWeightUnit'+ db.delete_table('nutrition_ingredientweightunit')++ # Deleting model 'WeightUnit'+ db.delete_table('nutrition_weightunit')++ # Deleting field 'MealItem.weight_unit'+ db.delete_column('nutrition_mealitem', 'weight_unit_id')++ # Deleting field 'MealItem.weight_type'+ db.delete_column('nutrition_mealitem', 'weight_type')+++ models = {+ 'auth.group': {+ 'Meta': {'object_name': 'Group'},+ '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': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})+ },+ 'auth.permission': {+ 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},+ 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),+ 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})+ },+ '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': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),+ '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': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),+ 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})+ },+ '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'}),+ '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'})+ },+ 'exercises.language': {+ 'Meta': {'object_name': 'Language'},+ 'full_name': ('django.db.models.fields.CharField', [], {'max_length': '30'}),+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),+ 'short_name': ('django.db.models.fields.CharField', [], {'max_length': '2'})+ },+ 'nutrition.ingredient': {+ 'Meta': {'ordering': "['name']", 'object_name': 'Ingredient'},+ 'carbohydrates': ('django.db.models.fields.FloatField', [], {}),+ 'carbohydrates_sugar': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}),+ 'energy': ('django.db.models.fields.IntegerField', [], {}),+ 'fat': ('django.db.models.fields.FloatField', [], {'blank': 'True'}),+ 'fat_saturated': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}),+ 'fibres': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}),+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),+ 'language': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['exercises.Language']"}),+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '200'}),+ 'protein': ('django.db.models.fields.FloatField', [], {}),+ 'sodium': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'})+ },+ 'nutrition.ingredientweightunit': {+ 'Meta': {'object_name': 'IngredientWeightUnit'},+ 'amount': ('django.db.models.fields.DecimalField', [], {'max_digits': '5', 'decimal_places': '2'}),+ 'gramm': ('django.db.models.fields.IntegerField', [], {}),+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),+ 'ingredient': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['nutrition.Ingredient']"}),+ 'unit': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['nutrition.WeightUnit']"})+ },+ 'nutrition.meal': {+ 'Meta': {'ordering': "['time']", 'object_name': 'Meal'},+ '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': "orm['nutrition.NutritionPlan']"}),+ 'time': ('django.db.models.fields.TimeField', [], {'null': 'True', 'blank': 'True'})+ },+ 'nutrition.mealitem': {+ 'Meta': {'object_name': 'MealItem'},+ 'amount_gramm': ('django.db.models.fields.IntegerField', [], {'max_length': '4', 'blank': 'True'}),+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),+ 'ingredient': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['nutrition.Ingredient']"}),+ 'meal': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['nutrition.Meal']"}),+ 'order': ('django.db.models.fields.IntegerField', [], {'max_length': '1', 'blank': 'True'}),+ 'weight_type': ('django.db.models.fields.CharField', [], {'default': "'1'", 'max_length': '2'}),+ 'weight_unit': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['nutrition.IngredientWeightUnit']", 'null': 'True'})+ },+ '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'}),+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),+ 'language': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['exercises.Language']"}),+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"})+ },+ 'nutrition.weightunit': {+ 'Meta': {'object_name': 'WeightUnit'},+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '200'})+ }+ }++ complete_apps = ['nutrition']\ No newline at end of filediff --git a/wger/nutrition/models.py b/wger/nutrition/models.pyindex 870b98990..ed5703878 100644--- a/wger/nutrition/models.py+++ b/wger/nutrition/models.py@@ -172,6 +172,44 @@ class Ingredient(models.Model):return False+class WeightUnit(models.Model):+ '''+ A more human usable weight unit (spoon, table, slice...)+ '''++ name = models.CharField(max_length=200,+ verbose_name=_('Name'),)++ def __unicode__(self):+ '''+ Return a more human-readable representation+ '''+ return "{0}".format(self.name)+++class IngredientWeightUnit(models.Model):+ '''+ A specific human usable weight unit for an ingredient+ '''++ ingredient = models.ForeignKey(Ingredient, verbose_name=_('Ingredient'))+ unit = models.ForeignKey(WeightUnit, verbose_name=_('Weight unit'))++ gramm = models.IntegerField(verbose_name=_('Amount in gramms'))+ amount = models.DecimalField(decimal_places=2,+ max_digits=5,+ verbose_name=_('Amount'),+ help_text=_('Unit amount, e.g. "1 Cup" or "1/2 spoon"'))++ def __unicode__(self):+ '''+ Return a more human-readable representation+ '''+ return u"{0}: {1}{2} á {3}g".format(self.ingredient.name,+ self.amount,+ self.unit.name,+ self.gramm)+class Meal(models.Model):'''A meal@@ -198,18 +236,33 @@ class Meal(models.Model):return self.plan+MEALITEM_WEIGHT_GRAM = '1'+MEALITEM_WEIGHT_UNIT = '2'++MEALITEM_WEIGHT_TYPES = (+ (MEALITEM_WEIGHT_GRAM, _('Weight in grams')),+ (MEALITEM_WEIGHT_UNIT, _('Weight in units')),+)+class MealItem(models.Model):'''An item (component) of a meal'''meal = models.ForeignKey(Meal, verbose_name=_('Nutrition plan'))+ ingredient = models.ForeignKey(Ingredient, verbose_name=_('Ingredient'))+ weight_unit = models.ForeignKey(IngredientWeightUnit,+ verbose_name=_('Weight unit'),+ null=True)+order = models.IntegerField(max_length=1, blank=True, verbose_name=_('Order'))amount_gramm = models.IntegerField(max_length=4,blank=True,validators=[MaxValueValidator(1000)],verbose_name=_('Amount in gramms'))- ingredient = models.ForeignKey(Ingredient, verbose_name=_('Ingredient'))+ weight_type = models.CharField(max_length=2,+ choices=MEALITEM_WEIGHT_TYPES,+ default=MEALITEM_WEIGHT_GRAM)def __unicode__(self):'''diff --git a/wger/nutrition/templates/units/list.html b/wger/nutrition/templates/units/list.htmlnew file mode 100644index 000000000..f515117d6--- /dev/null+++ b/wger/nutrition/templates/units/list.html@@ -0,0 +1,66 @@+{% extends "base.html" %}+{% load url from future %}+{% load i18n %}+{% load staticfiles %}+{% load manager_extras %}++{% block title %}{% trans "Ingredient weight units" %}{% endblock %}+++{% block content %}++<table>+<thead>+<tr>+ <th>{% trans "ID" %}</th>+ <th>{% trans "Actions" %}</th>+ <th>{% trans "Name" %}</th>+</tr>+</thead>+<tbody>+{% for unit in unit_list %}+<tr>+ <td>{{unit.id}}</td>+ <td>+ <a href="{% url 'weight-unit-add' %}">+ <img src="{% static 'images/icons/trash.svg' %}"+ width="22"+ height="22"+ alt="{% trans 'Delete' %}"+ title="{% trans 'Delete' %}">+ </a>+ <a href="{% url 'weight-unit-add' %}">+ <img src="{% static 'images/icons/edit.svg' %}"+ width="22"+ height="22"+ alt="{% trans 'Edit' %}"+ title="{% trans 'Edit' %}">+ </a>+ </td>+ <td>{{unit.name}}</td>+</tr>+{% endfor %}+</tbody>+</table>++{% endblock %}+++++{% block sidebar %}+{% if perms.exercises %}+<h3>{% trans "Options" %}</h3>+<p>+ <a href="{% url 'weight-unit-add' %}"+ class="modal-dialog">+ <img src="{% static 'images/icons/new.svg' %}"+ width="32"+ height="32"+ alt="{% trans 'Add new weight unit' %}"+ title="{% trans 'Add new weight unit' %}">+ {% trans "Add new weight unit" %}+ </a>+</p>+{% endif %}+{% endblock %}diff --git a/wger/nutrition/urls.py b/wger/nutrition/urls.pyindex 02fc60f6d..1d2e30b57 100644--- a/wger/nutrition/urls.py+++ b/wger/nutrition/urls.py@@ -9,7 +9,8 @@ from wger.nutrition.views import PlanDeleteViewfrom wger.nutrition.views import PlanEditViewfrom wger.nutrition.views import MealCreateViewfrom wger.nutrition.views import MealEditView-+from wger.nutrition.views import WeightUnitListView+from wger.nutrition.views import WeightUnitCreateViewurlpatterns = patterns('wger.nutrition.views',url(r'^overview/$', 'overview'),@@ -61,4 +62,13 @@ urlpatterns = patterns('wger.nutrition.views',url(r'^ingredient/(?P<id>\d+)/view/$', 'ingredient_view'),url(r'^ingredient/(?P<id>\d+)/view/(?P<slug>[-\w]+)/$', 'ingredient_view'),url(r'^ingredient/search/$', 'ingredient_search'),++ # Ingredient units+ url(r'^ingredient/unit/list/$',+ permission_required('exercises.change_exercise')(WeightUnitListView.as_view()),+ name='weight-unit-list'),+ url(r'^ingredient/unit/add/$',+ permission_required('exercises.change_exercise')(WeightUnitCreateView.as_view()),+ name='weight-unit-add'),+)diff --git a/wger/nutrition/views.py b/wger/nutrition/views.pyindex cccb1e885..321168908 100644--- a/wger/nutrition/views.py+++ b/wger/nutrition/views.py@@ -34,12 +34,14 @@ from django.utils.translation import ugettext_lazyfrom django.views.generic import DeleteViewfrom django.views.generic import CreateViewfrom django.views.generic import UpdateView+from django.views.generic import ListViewfrom wger.nutrition.models import NutritionPlanfrom wger.nutrition.models import Mealfrom wger.nutrition.models import MealItemfrom wger.nutrition.models import Ingredient+from wger.nutrition.models import WeightUnitfrom reportlab.lib.styles import getSampleStyleSheetfrom reportlab.lib.pagesizes import A4, cm@@ -589,3 +591,31 @@ def ingredient_search(request):return render_to_response('ingredient_search.html',template_data,context_instance=RequestContext(request))+++# ************************+# Ingredient units functions+# ************************++class WeightUnitListView(ListView):+ '''+ Generic view to list all weight units+ '''++ model = WeightUnit+ template_name = 'units/list.html'+ context_object_name = 'unit_list'+++class WeightUnitCreateView(YamlFormMixin, CreateView):+ '''+ Generic view to add a new weight unit for ingredients+ '''++ model = WeightUnit+ #form_class = IngredientForm+ title = ugettext_lazy('Add a new weight unit')+ form_action = reverse_lazy('weight-unit-add')++ def get_success_url(self):+ return reverse('weight-unit-list')\ No newline at end of file