Self hosted FLOSS fitness/workout, nutrition and weight tracker
Only show weigt units in the user's language
Roland Geider60f3aa59faaf
diff --git a/wger/nutrition/tests/weight_unit_ingredient.py b/wger/nutrition/tests/weight_unit_ingredient.pyindex 3e07a2aea..55e47a8c4 100644--- a/wger/nutrition/tests/weight_unit_ingredient.py+++ b/wger/nutrition/tests/weight_unit_ingredient.py@@ -14,9 +14,12 @@# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.from django.core.urlresolvers import reverse_lazy+from django.core.urlresolvers import reverse+from wger.nutrition.models import WeightUnitfrom wger.nutrition.models import IngredientWeightUnit+from wger.manager.tests.testcase import WorkoutManagerTestCasefrom wger.manager.tests.testcase import WorkoutManagerDeleteTestCasefrom wger.manager.tests.testcase import WorkoutManagerEditTestCasefrom wger.manager.tests.testcase import WorkoutManagerAddTestCase@@ -30,7 +33,7 @@ class AddWeightUnitIngredientTestCase(WorkoutManagerAddTestCase):object_class = IngredientWeightUniturl = reverse_lazy('weight-unit-ingredient-add',kwargs={'ingredient_pk': 1})- data = {'unit': 1,+ data = {'unit': 5,'gramm': 123,'amount': 1}pk = 9875@@ -54,6 +57,42 @@ class EditWeightUnitTestCase(WorkoutManagerEditTestCase):object_class = IngredientWeightUniturl = 'weight-unit-ingredient-edit'pk = 1- data = {'unit': 1,+ data = {'unit': 5,'gramm': 10,'amount': 0.3}+++class WeightUnitFormTestCase(WorkoutManagerTestCase):+ '''+ Tests the form for the weight units+ '''++ def test_add_weight_unit(self):+ '''+ Tests the form in the add view+ '''+ self.user_login('admin')+ response = self.client.get(reverse('weight-unit-ingredient-add',+ kwargs={'ingredient_pk': 1}))++ choices = [text for value, text in response.context['form']['unit'].field.choices]+ for unit in WeightUnit.objects.all():+ if unit.language_id == 1:+ self.assertNotIn(unit.name, choices)+ else:+ self.assertIn(unit.name, choices)++ def test_edit_weight_unit(self):+ '''+ Tests that the form in the edit view only shows weigh units in the user's language+ '''+ self.user_login('admin')+ response = self.client.get(reverse('weight-unit-ingredient-edit',+ kwargs={'pk': 1}))++ choices = [text for value, text in response.context['form']['unit'].field.choices]+ for unit in WeightUnit.objects.all():+ if unit.language_id == 1:+ self.assertNotIn(unit.name, choices)+ else:+ self.assertIn(unit.name, choices)diff --git a/wger/nutrition/views/unit_ingredient.py b/wger/nutrition/views/unit_ingredient.pyindex b74877439..fa5529a01 100644--- a/wger/nutrition/views/unit_ingredient.py+++ b/wger/nutrition/views/unit_ingredient.py@@ -18,6 +18,7 @@ import loggingfrom django.shortcuts import get_object_or_404from django.core.urlresolvers import reversefrom django.forms import ModelForm+from django.forms import ModelChoiceFieldfrom django.utils.translation import ugettext_lazyfrom django.views.generic import DeleteView@@ -26,7 +27,9 @@ from django.views.generic import UpdateViewfrom wger.nutrition.models import Ingredientfrom wger.nutrition.models import IngredientWeightUnit+from wger.nutrition.models import WeightUnit+from wger.utils.language import load_languagefrom wger.utils.generic_views import YamlFormMixinfrom wger.utils.generic_views import YamlDeleteMixin@@ -63,6 +66,19 @@ class WeightUnitIngredientCreateView(YamlFormMixin, CreateView):form.instance.ingredient = ingredientreturn super(WeightUnitIngredientCreateView, self).form_valid(form)+ def get_form_class(self):+ '''+ The form can only show units in the user's language+ '''++ class IngredientWeightUnitForm(ModelForm):+ unit = ModelChoiceField(queryset=WeightUnit.objects.filter(language=load_language()))++ class Meta:+ model = IngredientWeightUnit++ return IngredientWeightUnitForm+class WeightUnitIngredientUpdateView(YamlFormMixin, UpdateView):'''@@ -77,6 +93,19 @@ class WeightUnitIngredientUpdateView(YamlFormMixin, UpdateView):return reverse('wger.nutrition.views.ingredient.view',kwargs={'id': self.object.ingredient.id})+ def get_form_class(self):+ '''+ The form can only show units in the user's language+ '''++ class IngredientWeightUnitForm(ModelForm):+ unit = ModelChoiceField(queryset=WeightUnit.objects.filter(language=load_language()))++ class Meta:+ model = IngredientWeightUnit++ return IngredientWeightUnitForm+class WeightUnitIngredientDeleteView(YamlDeleteMixin, DeleteView):'''