Self hosted FLOSS fitness/workout, nutrition and weight tracker
Use the API to retreive the units for the ingredients
Roland Geider8acb6f94f8ae
diff --git a/wger/nutrition/static/js/nutrition.js b/wger/nutrition/static/js/nutrition.jsindex 6fc085bc4..44f2d7cd4 100644--- a/wger/nutrition/static/js/nutrition.js+++ b/wger/nutrition/static/js/nutrition.js@@ -80,24 +80,26 @@ function init_ingredient_autocompleter()$('#exercise_name').html(ui.item.label);// See if the ingredient has any units and set the values for the forms- $.get('/' + get_current_language() + '/nutrition/ingredient/' + ui.item.id + '/get-units',- function(data){-- // Remove any old units, if any- var options = $('#id_weight_unit').find('option');- $.each(options, function(index, option_obj) {- if (option_obj.value != '')- {- $(option_obj).remove();- }- });-- // Add new units, if any- $.each(data, function(index, value) {- $('#id_unit').append(new Option(value.name, value.id));- $('#id_weight_unit').append(new Option(value.name_model, value.id));- });- });+ $.get('/api/v2/ingredientweightunit/?ingredient=' + ui.item.id, function(unit_data) {++ // Remove any old units, if any+ var options = $('#id_weight_unit').find('option');+ $.each(options, function(index, option_obj) {+ if (option_obj.value != '')+ {+ $(option_obj).remove();+ }+ });++ // Add new units, if any+ $.each(unit_data.results, function(index, value) {+ $.get('/api/v2/weightunit/' + value.unit + '/', function(unit) {+ var unit_name = unit.name + ' (' + value.gramm + 'g)';+ $('#id_unit').append(new Option(unit_name, value.id));+ $('#id_weight_unit').append(new Option(unit_name, value.id));+ });+ });+ });}});}diff --git a/wger/nutrition/tests/test_ingredient.py b/wger/nutrition/tests/test_ingredient.pyindex aac22af79..ce91b3ed1 100644--- a/wger/nutrition/tests/test_ingredient.py+++ b/wger/nutrition/tests/test_ingredient.py@@ -199,50 +199,6 @@ class IngredientSearchTestCase(WorkoutManagerTestCase):self.search_ingredient()-class IngredientUnitSearchTestCase(WorkoutManagerTestCase):- '''- Tests the ingredient unit search functions- '''-- def search_ingredient_unit(self, fail=True):- '''- Helper function- '''-- # Search for an ingredient that has units- response = self.client.get(reverse('ingredient-get-units', kwargs={'pk': 1}))- if fail:- self.assertEqual(response.status_code, 302)- else:- self.assertEqual(response.status_code, 200)- result = json.loads(response.content)- self.assertEqual(len(result), 5)-- # Search for an ingredient that has no units- response = self.client.get(reverse('ingredient-get-units', kwargs={'pk': 2}))- if fail:- self.assertEqual(response.status_code, 302)- else:- self.assertEqual(response.status_code, 200)- result = json.loads(response.content)- self.assertEqual(len(result), 0)-- def test_search_ingredient_unit_anonymous(self):- '''- Test searching for an ingredient by an anonymous user- '''-- self.search_ingredient_unit(fail=True)-- def test_search_ingredient_unit_logged_in(self):- '''- Test searching for an ingredient by a logged in user- '''-- self.user_login('test')- self.search_ingredient_unit(fail=False)--class IngredientValuesTestCase(WorkoutManagerTestCase):'''Tests the nutritional value calculator for an ingredientdiff --git a/wger/nutrition/urls.py b/wger/nutrition/urls.pyindex cef1e7a46..f87e67b27 100644--- a/wger/nutrition/urls.py+++ b/wger/nutrition/urls.py@@ -96,9 +96,6 @@ urlpatterns = patterns('wger.nutrition.views',url(r'^ingredient/(?P<id>\d+)/view/(?P<slug>[-\w]+)/$',ingredient.view,name='ingredient-view'),- url(r'^ingredient/(?P<pk>\d+)/get-units$',- ingredient.ajax_get_ingredient_units,- name='ingredient-get-units'),url(r'^ingredient/(?P<pk>\d+)/get-nutritional-values$',ingredient.ajax_get_ingredient_values,name='ingredient-get-values'),diff --git a/wger/nutrition/views/ingredient.py b/wger/nutrition/views/ingredient.pyindex be0a7d488..83b45d5ef 100644--- a/wger/nutrition/views/ingredient.py+++ b/wger/nutrition/views/ingredient.py@@ -223,24 +223,6 @@ def decline(request, pk):return HttpResponseRedirect(ingredient.get_absolute_url())-@login_required-def ajax_get_ingredient_units(request, pk):- '''- Fetches the available ingredient units- '''-- units = IngredientWeightUnit.objects.filter(ingredient_id=pk)- result = []- for unit in units:- result.append({'id': unit.id,- 'name': unit.unit.name,- 'amount': unit.amount,- 'name_model': unicode(unit)})-- return HttpResponse(json.dumps(result, cls=helpers.DecimalJsonEncoder),- 'application/json')--def ajax_get_ingredient_values(request, pk):'''Calculates the nutritional values for the given amount and exercise