Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add ingredient info endpoint to API

Roland Geider24fa39d45594
diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.py
index 63f0080da..c9e74b6f8 100644
--- a/wger/exercises/api/views.py
+++ b/wger/exercises/api/views.py
@@ -134,7 +134,8 @@ def search(request):
class ExerciseInfoViewset(viewsets.ReadOnlyModelViewSet):
"""
- API endpoint for exercise objects
+ Read-only info API endpoint for exercise objects. Returns nested data
+ structures for more easy parsing.
"""
queryset = Exercise.objects.accepted()
diff --git a/wger/nutrition/api/serializers.py b/wger/nutrition/api/serializers.py
index 35b39aaab..75e91b320 100644
--- a/wger/nutrition/api/serializers.py
+++ b/wger/nutrition/api/serializers.py
@@ -90,4 +90,42 @@ class IngredientSerializer(serializers.ModelSerializer):
class Meta:
model = Ingredient
- fields = '__all__'
+ fields = ['id',
+ 'name',
+ 'creation_date',
+ 'update_date',
+ 'energy',
+ 'protein',
+ 'carbohydrates',
+ 'carbohydrates_sugar',
+ 'fat',
+ 'fat_saturated',
+ 'fibres',
+ 'sodium',
+ 'license',
+ 'license_author']
+
+
+class IngredientInfoSerializer(serializers.ModelSerializer):
+ """
+ Ingredient info serializer
+ """
+
+ class Meta:
+ model = Ingredient
+ depth = 1
+ fields = ['id',
+ 'name',
+ 'creation_date',
+ 'update_date',
+ 'energy',
+ 'protein',
+ 'carbohydrates',
+ 'carbohydrates_sugar',
+ 'fat',
+ 'fat_saturated',
+ 'fibres',
+ 'sodium',
+ 'license',
+ 'license_author',
+ 'ingredientweightunit_set']
diff --git a/wger/nutrition/api/views.py b/wger/nutrition/api/views.py
index 7a4a95d37..0a25d99bd 100644
--- a/wger/nutrition/api/views.py
+++ b/wger/nutrition/api/views.py
@@ -31,6 +31,7 @@ from rest_framework.response import Response
# wger
from wger.nutrition.api.serializers import (
+ IngredientInfoSerializer,
IngredientSerializer,
IngredientWeightUnitSerializer,
MealItemSerializer,
@@ -126,6 +127,14 @@ class IngredientViewSet(viewsets.ReadOnlyModelViewSet):
return Response(result)
+class IngredientInfoViewSet(IngredientViewSet):
+ """
+ Read-only info API endpoint for ingredient objects. Returns nested data
+ structures for more easy parsing.
+ """
+ serializer_class = IngredientInfoSerializer
+
+
@api_view(['GET'])
def search(request):
"""
diff --git a/wger/software/templates/api.html b/wger/software/templates/api.html
index 92f9277c4..760891b9a 100644
--- a/wger/software/templates/api.html
+++ b/wger/software/templates/api.html
@@ -36,6 +36,7 @@ generated. At the moment it is not possible to register via the API.</p>
<li>exercisecomment</li>
<li>exerciseimage</li>
<li>ingredient</li>
+ <li>ingredientinfo</li>
<li>ingredienttoweightunit</li>
<li>language</li>
<li>license</li>
diff --git a/wger/urls.py b/wger/urls.py
index 94b95dd13..a552ce4bc 100644
--- a/wger/urls.py
+++ b/wger/urls.py
@@ -80,6 +80,7 @@ router.register(r'muscle', exercises_api_views.MuscleViewSet, basename='muscle')
# Nutrition app
router.register(r'ingredient', nutrition_api_views.IngredientViewSet, basename='api-ingredient')
+router.register(r'ingredientinfo', nutrition_api_views.IngredientInfoViewSet, basename='api-ingredientinfo')
router.register(r'weightunit', nutrition_api_views.WeightUnitViewSet, basename='weightunit')
router.register(r'ingredientweightunit', nutrition_api_views.IngredientWeightUnitViewSet, basename='ingredientweightunit')
router.register(r'nutritionplan', nutrition_api_views.NutritionPlanViewSet, basename='nutritionplan')