Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add resources for the nutritional app
Roland Geiderf0e7fcf8fd7a
diff --git a/wger/nutrition/api/resources.py b/wger/nutrition/api/resources.pyindex cddd7f75d..fd657fccc 100644--- a/wger/nutrition/api/resources.py+++ b/wger/nutrition/api/resources.py@@ -14,10 +14,16 @@## You should have received a copy of the GNU Affero General Public License-+from tastypie import fields+from tastypie.authentication import ApiKeyAuthenticationfrom tastypie.resources import ModelResource+from wger.nutrition.models import Ingredientfrom wger.nutrition.models import WeightUnit+from wger.nutrition.models import NutritionPlan+from wger.nutrition.models import Meal+from wger.nutrition.models import MealItem+from wger.utils.resources import UserObjectsOnlyAuthorizationclass IngredientResource(ModelResource):@@ -28,3 +34,84 @@ class IngredientResource(ModelResource):class WeightUnitResource(ModelResource):class Meta:queryset = WeightUnit.objects.all()+++class NutritionPlanResource(ModelResource):+ '''+ Resource for nutritional plans+ '''++ meals = fields.ToManyField('wger.nutrition.api.resources.MealResource', 'meal_set')++ def authorized_read_list(self, object_list, bundle):+ '''+ Filter to own objects+ '''+ return object_list.filter(user=bundle.request.user)++ def dehydrate(self, bundle):+ '''+ Also send the nutritional values+ '''+ bundle.data['nutritional_values'] = bundle.obj.get_nutritional_values()+ return bundle++ class Meta:+ queryset = NutritionPlan.objects.all()+ authentication = ApiKeyAuthentication()+ authorization = UserObjectsOnlyAuthorization()+++class MealResource(ModelResource):+ '''+ Resource for meals+ '''++ plan = fields.ToOneField(NutritionPlanResource, 'plan')+ meal_items = fields.ToManyField('wger.nutrition.api.resources.MealItemResource', 'mealitem_set')++ def authorized_read_list(self, object_list, bundle):+ '''+ Filter to own objects+ '''+ return object_list.filter(plan__user=bundle.request.user)++ def dehydrate(self, bundle):+ '''+ Also send the nutritional values+ '''+ bundle.data['nutritional_values'] = bundle.obj.get_nutritional_values()+ return bundle++ class Meta:+ queryset = Meal.objects.all()+ authentication = ApiKeyAuthentication()+ authorization = UserObjectsOnlyAuthorization()+++class MealItemResource(ModelResource):+ '''+ Resource for meal items+ '''++ meal = fields.ToOneField(MealResource, 'meal')+ ingredient = fields.ToOneField(IngredientResource, 'ingredient')+ weight_unit = fields.ToOneField(WeightUnitResource, 'weight_unit', null=True)++ def authorized_read_list(self, object_list, bundle):+ '''+ Filter to own objects+ '''+ return object_list.filter(meal__plan__user=bundle.request.user)++ def dehydrate(self, bundle):+ '''+ Also send the nutritional values+ '''+ bundle.data['nutritional_values'] = bundle.obj.get_nutritional_values()+ return bundle++ class Meta:+ queryset = MealItem.objects.all()+ authentication = ApiKeyAuthentication()+ authorization = UserObjectsOnlyAuthorization()\ No newline at end of filediff --git a/wger/urls.py b/wger/urls.pyindex d11a4d48e..b19f1273e 100644--- a/wger/urls.py+++ b/wger/urls.py@@ -27,6 +27,9 @@ v1_api.register(exercises_api.LanguageResource())# Nutrition appv1_api.register(nutrition_api.IngredientResource())v1_api.register(nutrition_api.WeightUnitResource())+v1_api.register(nutrition_api.NutritionPlanResource())+v1_api.register(nutrition_api.MealResource())+v1_api.register(nutrition_api.MealItemResource())# Manager appv1_api.register(manager_api.UserProfileResource())