Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add more views and tests for the weight unit model

Roland Geider307ee407bd49
diff --git a/wger/manager/tests/testcase.py b/wger/manager/tests/testcase.py
index fb26dfa25..7334f7957 100644
--- a/wger/manager/tests/testcase.py
+++ b/wger/manager/tests/testcase.py
@@ -27,6 +27,7 @@ class WorkoutManagerTestCase(TestCase):
'test-user-data',
'test-weight-data',
'test-exercises',
+ 'test-weight-units',
'test-ingredients',
'test-nutrition-data',
'test-workout-data')
diff --git a/wger/nutrition/fixtures/test-weight-units.json b/wger/nutrition/fixtures/test-weight-units.json
new file mode 100644
index 000000000..676f30708
--- /dev/null
+++ b/wger/nutrition/fixtures/test-weight-units.json
@@ -0,0 +1,50 @@
+[
+ {
+ "pk": 1,
+ "model": "nutrition.weightunit",
+ "fields": {
+ "name": "Scheibe",
+ "language": 1
+ }
+ },
+ {
+ "pk": 2,
+ "model": "nutrition.weightunit",
+ "fields": {
+ "name": "Tasse",
+ "language": 1
+ }
+ },
+ {
+ "pk": 3,
+ "model": "nutrition.weightunit",
+ "fields": {
+ "name": "Handvoll",
+ "language": 1
+ }
+ },
+ {
+ "pk": 4,
+ "model": "nutrition.weightunit",
+ "fields": {
+ "name": "Spoon",
+ "language": 2
+ }
+ },
+ {
+ "pk": 5,
+ "model": "nutrition.weightunit",
+ "fields": {
+ "name": "Cup",
+ "language": 2
+ }
+ },
+ {
+ "pk": 6,
+ "model": "nutrition.weightunit",
+ "fields": {
+ "name": "A bit",
+ "language": 2
+ }
+ }
+]
\ No newline at end of file
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.py
index 07bcd8d37..6f8d766f2 100644
--- 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.py
@@ -21,6 +21,7 @@ class Migration(SchemaMigration):
# Adding model 'WeightUnit'
db.create_table('nutrition_weightunit', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+ ('language', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['exercises.Language'])),
('name', self.gf('django.db.models.fields.CharField')(max_length=200)),
))
db.send_create_signal('nutrition', ['WeightUnit'])
@@ -143,6 +144,7 @@ class Migration(SchemaMigration):
'nutrition.weightunit': {
'Meta': {'object_name': 'WeightUnit'},
'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'})
}
}
diff --git a/wger/nutrition/models.py b/wger/nutrition/models.py
index ed5703878..199c4d8cd 100644
--- a/wger/nutrition/models.py
+++ b/wger/nutrition/models.py
@@ -177,15 +177,26 @@ class WeightUnit(models.Model):
A more human usable weight unit (spoon, table, slice...)
'''
+ language = models.ForeignKey(Language, verbose_name=_('Language'))
name = models.CharField(max_length=200,
verbose_name=_('Name'),)
+ # Metaclass to set some other properties
+ class Meta:
+ ordering = ["name", ]
+
def __unicode__(self):
'''
Return a more human-readable representation
'''
return "{0}".format(self.name)
+ def get_owner_object(self):
+ '''
+ Weight unit has no owner information
+ '''
+ return None
+
class IngredientWeightUnit(models.Model):
'''
@@ -194,7 +205,7 @@ class IngredientWeightUnit(models.Model):
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,
@@ -205,10 +216,12 @@ class IngredientWeightUnit(models.Model):
'''
Return a more human-readable representation
'''
+
return u"{0}: {1}{2} á {3}g".format(self.ingredient.name,
- self.amount,
- self.unit.name,
- self.gramm)
+ self.amount,
+ self.unit.name,
+ self.gramm)
+
class Meal(models.Model):
'''
@@ -244,6 +257,7 @@ MEALITEM_WEIGHT_TYPES = (
(MEALITEM_WEIGHT_UNIT, _('Weight in units')),
)
+
class MealItem(models.Model):
'''
An item (component) of a meal
diff --git a/wger/nutrition/templates/units/list.html b/wger/nutrition/templates/units/list.html
index f515117d6..dbcb26c97 100644
--- a/wger/nutrition/templates/units/list.html
+++ b/wger/nutrition/templates/units/list.html
@@ -12,7 +12,6 @@
<table>
<thead>
<tr>
- <th>{% trans "ID" %}</th>
<th>{% trans "Actions" %}</th>
<th>{% trans "Name" %}</th>
</tr>
@@ -20,16 +19,17 @@
<tbody>
{% for unit in unit_list %}
<tr>
- <td>{{unit.id}}</td>
<td>
- <a href="{% url 'weight-unit-add' %}">
+ <a href="{% url 'weight-unit-delete' unit.id %}"
+ class="modal-dialog">
<img src="{% static 'images/icons/trash.svg' %}"
width="22"
height="22"
alt="{% trans 'Delete' %}"
title="{% trans 'Delete' %}">
</a>
- <a href="{% url 'weight-unit-add' %}">
+ <a href="{% url 'weight-unit-edit' unit.id %}"
+ class="modal-dialog">
<img src="{% static 'images/icons/edit.svg' %}"
width="22"
height="22"
diff --git a/wger/nutrition/tests/__init__.py b/wger/nutrition/tests/__init__.py
index 39b35a6ec..794fa2180 100644
--- a/wger/nutrition/tests/__init__.py
+++ b/wger/nutrition/tests/__init__.py
@@ -5,3 +5,4 @@ from wger.nutrition.tests.pdf import *
from wger.nutrition.tests.ingredient import *
from wger.nutrition.tests.ingredient_overview import *
from wger.nutrition.tests.meal import *
+from wger.nutrition.tests.weight_unit import *
diff --git a/wger/nutrition/tests/weight_unit.py b/wger/nutrition/tests/weight_unit.py
new file mode 100644
index 000000000..29bea2698
--- /dev/null
+++ b/wger/nutrition/tests/weight_unit.py
@@ -0,0 +1,52 @@
+# 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 wger.nutrition.models import WeightUnit
+
+from wger.manager.tests.testcase import WorkoutManagerDeleteTestCase
+from wger.manager.tests.testcase import WorkoutManagerEditTestCase
+from wger.manager.tests.testcase import WorkoutManagerAddTestCase
+
+
+class AddWeightUnitTestCase(WorkoutManagerAddTestCase):
+ '''
+ Tests adding a new weight unit
+ '''
+
+ object_class = WeightUnit
+ url = 'weight-unit-add'
+ data = {'name': 'A new weight unit'}
+ pk = 7
+
+
+class DeleteWeightUnitTestCase(WorkoutManagerDeleteTestCase):
+ '''
+ Tests deleting a weight unit
+ '''
+
+ object_class = WeightUnit
+ url = 'weight-unit-delete'
+ pk = 1
+
+
+class EditWeightUnitTestCase(WorkoutManagerEditTestCase):
+ '''
+ Tests editing a weight unit
+ '''
+
+ object_class = WeightUnit
+ url = 'weight-unit-edit'
+ pk = 1
+ data = {'name': 'A new name'}
diff --git a/wger/nutrition/urls.py b/wger/nutrition/urls.py
index 1d2e30b57..a3561588a 100644
--- a/wger/nutrition/urls.py
+++ b/wger/nutrition/urls.py
@@ -9,8 +9,11 @@ from wger.nutrition.views import PlanDeleteView
from wger.nutrition.views import PlanEditView
from wger.nutrition.views import MealCreateView
from wger.nutrition.views import MealEditView
+
from wger.nutrition.views import WeightUnitListView
from wger.nutrition.views import WeightUnitCreateView
+from wger.nutrition.views import WeightUnitDeleteView
+from wger.nutrition.views import WeightUnitUpdateView
urlpatterns = patterns('wger.nutrition.views',
url(r'^overview/$', 'overview'),
@@ -70,5 +73,16 @@ urlpatterns = patterns('wger.nutrition.views',
url(r'^ingredient/unit/add/$',
permission_required('exercises.change_exercise')(WeightUnitCreateView.as_view()),
name='weight-unit-add'),
+ url(r'^ingredient/unit/(?P<pk>\d+)/delete/$',
+ permission_required('exercises.change_exercise')(WeightUnitDeleteView.as_view()),
+ name='weight-unit-delete'),
+ url(r'^ingredient/unit/(?P<pk>\d+)/edit/$',
+ permission_required('exercises.change_exercise')(WeightUnitUpdateView.as_view()),
+ name='weight-unit-edit'),
+
+ # Ingredient to weight units cross table
+ #url(r'^ingredient/unit/add-to-ingredient/$',
+ # permission_required('exercises.change_exercise')(WeightUnitCreateView.as_view()),
+ # name='weight-unit-add'),
)
diff --git a/wger/nutrition/views.py b/wger/nutrition/views.py
index 321168908..4055f7b8b 100644
--- a/wger/nutrition/views.py
+++ b/wger/nutrition/views.py
@@ -607,15 +607,50 @@ class WeightUnitListView(ListView):
context_object_name = 'unit_list'
+class WeightUnitForm(ModelForm):
+ class Meta:
+ model = WeightUnit
+ exclude = ('language',)
+
+
class WeightUnitCreateView(YamlFormMixin, CreateView):
'''
Generic view to add a new weight unit for ingredients
'''
model = WeightUnit
- #form_class = IngredientForm
+ form_class = WeightUnitForm
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
+ return reverse('weight-unit-list')
+
+ def form_valid(self, form):
+ form.instance.language = load_language()
+ return super(WeightUnitCreateView, self).form_valid(form)
+
+
+class WeightUnitDeleteView(YamlDeleteMixin, DeleteView):
+ '''
+ Generic view to delete a weight unit
+ '''
+
+ model = WeightUnit
+ success_url = reverse_lazy('weight-unit-list')
+ title = ugettext_lazy('Delete weight unit?')
+ form_action_urlname = 'weight-unit-delete'
+
+
+class WeightUnitUpdateView(YamlFormMixin, UpdateView):
+ '''
+ Generic view to update an weight unit
+ '''
+
+ model = WeightUnit
+ form_class = WeightUnitForm
+ title = ugettext_lazy('Edit a weight unit')
+ form_action_urlname = 'weight-unit-edit'
+
+ def get_success_url(self):
+ return reverse('weight-unit-list')