Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add missing endpoints, set some to read only, cleanup querysets
Roland Geiderd4aca2b3f7cb
diff --git a/wger/core/api/views.py b/wger/core/api/views.pyindex 93c410ea0..237a4bda9 100644--- a/wger/core/api/views.py+++ b/wger/core/api/views.py@@ -20,6 +20,8 @@ from rest_framework import viewsetsfrom wger.core.models import UserProfile, Language, DaysOfWeek, License+viewsets.ReadOnlyModelViewSet+class UserProfileViewSet(viewsets.ModelViewSet):'''API endpoint for workout objects@@ -32,42 +34,27 @@ class UserProfileViewSet(viewsets.ModelViewSet):'''return UserProfile.objects.filter(user=self.request.user)+ def create(self, request):+ pass+-class LanguageViewSet(viewsets.ModelViewSet):+class LanguageViewSet(viewsets.ReadOnlyModelViewSet):'''API endpoint for workout objects'''model = Language- def get_queryset(self):- '''- Only allow access to appropriate objects- '''- return Language.objects.all()--class DaysOfWeekViewSet(viewsets.ModelViewSet):+class DaysOfWeekViewSet(viewsets.ReadOnlyModelViewSet):'''API endpoint for workout objects'''model = DaysOfWeek- def get_queryset(self):- '''- Only allow access to appropriate objects- '''- return DaysOfWeek.objects.all()--class LicenseViewSet(viewsets.ModelViewSet):+class LicenseViewSet(viewsets.ReadOnlyModelViewSet):'''API endpoint for workout objects'''model = License- def get_queryset(self):- '''- Only allow access to appropriate objects- '''- return License.objects.all()-diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.pyindex cc2886dc3..f73dc1a4d 100644--- a/wger/exercises/api/views.py+++ b/wger/exercises/api/views.py@@ -21,7 +21,6 @@ from rest_framework.response import Responsefrom easy_thumbnails.alias import aliasesfrom easy_thumbnails.files import get_thumbnailer-from wger.exercises.api.serializers import ExerciseImageSerializerfrom wger.exercises.models import Exercisefrom wger.exercises.models import Equipmentfrom wger.exercises.models import ExerciseCategory@@ -36,38 +35,20 @@ class ExerciseViewSet(viewsets.ModelViewSet):'''model = Exercise- def get_queryset(self):- '''- Only allow access to appropriate objects- '''- return Exercise.objects.all()--class EquipmentViewSet(viewsets.ModelViewSet):+class EquipmentViewSet(viewsets.ReadOnlyModelViewSet):'''API endpoint for workout objects'''model = Equipment- def get_queryset(self):- '''- Only allow access to appropriate objects- '''- return Equipment.objects.all()--class ExerciseCategoryViewSet(viewsets.ModelViewSet):+class ExerciseCategoryViewSet(viewsets.ReadOnlyModelViewSet):'''API endpoint for workout objects'''model = ExerciseCategory- def get_queryset(self):- '''- Only allow access to appropriate objects- '''- return ExerciseCategory.objects.all()-class ExerciseImageViewSet(viewsets.ModelViewSet):'''@@ -75,12 +56,6 @@ class ExerciseImageViewSet(viewsets.ModelViewSet):'''model = ExerciseImage- def get_queryset(self):- '''- Only allow access to appropriate objects- '''- return ExerciseImage.objects.all()-@link()def thumbnails(self, request, pk):'''@@ -95,27 +70,15 @@ class ExerciseImageViewSet(viewsets.ModelViewSet):return Response(thumbnails)-class ExerciseCommentViewSet(viewsets.ModelViewSet):+class ExerciseCommentViewSet(viewsets.ReadOnlyModelViewSet):'''API endpoint for workout objects'''model = ExerciseComment- def get_queryset(self):- '''- Only allow access to appropriate objects- '''- return ExerciseComment.objects.all()--class MuscleViewSet(viewsets.ModelViewSet):+class MuscleViewSet(viewsets.ReadOnlyModelViewSet):'''API endpoint for workout objects'''- model = Muscle-- def get_queryset(self):- '''- Only allow access to appropriate objects- '''- return Muscle.objects.all()\ No newline at end of file+ model = Muscle\ No newline at end of filediff --git a/wger/nutrition/api/serializers.py b/wger/nutrition/api/serializers.pynew file mode 100644index 000000000..87807a627--- /dev/null+++ b/wger/nutrition/api/serializers.py@@ -0,0 +1,29 @@+# -*- coding: utf-8 -*-++# This file is part of wger Workout Manager.+#+# wger 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.+#+# wger 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 rest_framework import serializers+from wger.nutrition.models import NutritionPlan+++class NutritionPlanSerializer(serializers.ModelSerializer):+ '''+ Nutritional plan serializer+ '''++ class Meta:+ model = NutritionPlan+ exclude = ('user',)diff --git a/wger/nutrition/api/views.py b/wger/nutrition/api/views.pynew file mode 100644index 000000000..974a95fa2--- /dev/null+++ b/wger/nutrition/api/views.py@@ -0,0 +1,91 @@+# -*- coding: utf-8 -*-++# This file is part of wger Workout Manager.+#+# wger 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.+#+# wger 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 rest_framework import viewsets+from wger.nutrition.api.serializers import NutritionPlanSerializer++from wger.nutrition.models import Ingredient, Meal, MealItem+from wger.nutrition.models import WeightUnit+from wger.nutrition.models import IngredientWeightUnit+from wger.nutrition.models import NutritionPlan+++class IngredientViewSet(viewsets.ReadOnlyModelViewSet):+ '''+ API endpoint for ingredient objects+ '''+ model = Ingredient+++class WeightUnitViewSet(viewsets.ReadOnlyModelViewSet):+ '''+ API endpoint for weight unit objects+ '''+ model = WeightUnit+++class IngredientWeightUnitViewSet(viewsets.ReadOnlyModelViewSet):+ '''+ API endpoint for many-to-many table ingredient-weight unit objects+ '''+ model = IngredientWeightUnit+++class NutritionPlanViewSet(viewsets.ModelViewSet):+ '''+ API endpoint for nutrition plan objects+ '''+ model = NutritionPlan+ serializer_class = NutritionPlanSerializer++ def get_queryset(self):+ '''+ Only allow access to appropriate objects+ '''+ return NutritionPlan.objects.filter(user=self.request.user)++ def pre_save(self, obj):+ '''+ Set the owner+ '''+ obj.user = self.request.user+++class MealViewSet(viewsets.ModelViewSet):+ '''+ API endpoint for meal objects+ '''+ model = Meal++ def get_queryset(self):+ '''+ Only allow access to appropriate objects+ '''+ return NutritionPlan.objects.filter(plan__user=self.request.user)+++class MealItemViewSet(viewsets.ModelViewSet):+ '''+ API endpoint for meal item objects+ '''+ model = MealItem++ def get_queryset(self):+ '''+ Only allow access to appropriate objects+ '''+ return MealItem.objects.filter(meal__plan__user=self.request.user)\ No newline at end of filediff --git a/wger/urls.py b/wger/urls.pyindex 0f9a1d679..401aa7f43 100644--- a/wger/urls.py+++ b/wger/urls.py@@ -37,6 +37,8 @@ from wger.weight.api import resources as weight_apifrom wger.manager.api import views as manager_api_viewsfrom wger.core.api import views as core_api_viewsfrom wger.exercises.api import views as exercises_api_views+from wger.nutrition.api import views as nutrition_api_views+from wger.weight.api import views as weight_api_views## REST API@@ -103,8 +105,15 @@ router.register(r'exercisecomment', exercises_api_views.ExerciseCommentViewSet)router.register(r'muscle', exercises_api_views.MuscleViewSet)# Nutrition app+router.register(r'ingredient', nutrition_api_views.IngredientViewSet)+router.register(r'weightunit', nutrition_api_views.WeightUnitViewSet)+router.register(r'ingredienttoweightunit', nutrition_api_views.IngredientWeightUnitViewSet)+router.register(r'nutrition', nutrition_api_views.NutritionPlanViewSet)+router.register(r'meal', nutrition_api_views.MealViewSet)+router.register(r'mealitem', nutrition_api_views.MealItemViewSet)# Weight app+router.register(r'weight', weight_api_views.WeightEntryViewSet)from django.contrib import admindiff --git a/wger/weight/api/serializers.py b/wger/weight/api/serializers.pynew file mode 100644index 000000000..6f29e61f3--- /dev/null+++ b/wger/weight/api/serializers.py@@ -0,0 +1,34 @@+# -*- coding: utf-8 -*-++# This file is part of wger Workout Manager.+#+# wger 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.+#+# wger 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 rest_framework import serializers+from rest_framework.decorators import link+from rest_framework.response import Response++from wger.manager.models import Workout+from wger.manager.models import WorkoutSession+from wger.weight.models import WeightEntry+++class WeightEntrySerializer(serializers.ModelSerializer):+ '''+ Weight serializer+ '''++ class Meta:+ model = WeightEntry+ exclude = ('user',)diff --git a/wger/weight/api/views.py b/wger/weight/api/views.pynew file mode 100644index 000000000..6c1d9165f--- /dev/null+++ b/wger/weight/api/views.py@@ -0,0 +1,41 @@+# -*- coding: utf-8 -*-++# This file is part of wger Workout Manager.+#+# wger 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.+#+# wger 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 rest_framework import viewsets+from wger.weight.api.serializers import WeightEntrySerializer++from wger.weight.models import WeightEntry+++class WeightEntryViewSet(viewsets.ModelViewSet):+ '''+ API endpoint for nutrition plan objects+ '''+ model = WeightEntry+ serializer_class = WeightEntrySerializer++ def get_queryset(self):+ '''+ Only allow access to appropriate objects+ '''+ return WeightEntry.objects.filter(user=self.request.user)++ def pre_save(self, obj):+ '''+ Set the owner+ '''+ obj.user = self.request.user\ No newline at end of file