Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add filter sets for finer control of results in the api
Roland Geider30accb2eda80
diff --git a/wger/nutrition/api/filtersets.py b/wger/nutrition/api/filtersets.pynew file mode 100644index 000000000..35f22fa45--- /dev/null+++ b/wger/nutrition/api/filtersets.py@@ -0,0 +1,47 @@+# Third Party+from django_filters import rest_framework as filters++# wger+from wger.nutrition.models import (+ Ingredient,+ LogItem,+)+++class LogItemFilterSet(filters.FilterSet):++ class Meta:+ model = LogItem+ fields = {+ 'datetime': ['exact', 'date'],+ 'amount': ['exact'],+ 'ingredient': ['exact'],+ 'plan': ['exact'],+ 'weight_unit': ['exact'],+ }+++class IngredientFilterSet(filters.FilterSet):++ class Meta:+ model = Ingredient+ fields = {+ 'id': ['exact', 'in'],+ 'uuid': ['exact'],+ 'code': ['exact'],+ 'carbohydrates': ['exact'],+ 'carbohydrates_sugar': ['exact'],+ 'created': ['exact', 'gt', 'lt'],+ 'last_update': ['exact', 'gt', 'lt'],+ 'energy': ['exact'],+ 'fat': ['exact'],+ 'fat_saturated': ['exact'],+ 'fibres': ['exact'],+ 'name': ['exact'],+ 'protein': ['exact'],+ 'sodium': ['exact'],+ 'status': ['exact'],+ 'language': ['exact'],+ 'license': ['exact'],+ 'license_author': ['exact'],+ }diff --git a/wger/nutrition/api/views.py b/wger/nutrition/api/views.pyindex 421bd04ad..df955ed7d 100644--- a/wger/nutrition/api/views.py+++ b/wger/nutrition/api/views.py@@ -46,6 +46,10 @@ from rest_framework.fields import (from rest_framework.response import Response# wger+from wger.nutrition.api.filtersets import (+ IngredientFilterSet,+ LogItemFilterSet,+)from wger.nutrition.api.serializers import (IngredientImageSerializer,IngredientInfoSerializer,@@ -84,25 +88,7 @@ class IngredientViewSet(viewsets.ReadOnlyModelViewSet):"""serializer_class = IngredientSerializerordering_fields = '__all__'- filterset_fields = (- 'uuid',- 'code',- 'carbohydrates',- 'carbohydrates_sugar',- 'created',- 'last_update',- 'energy',- 'fat',- 'fat_saturated',- 'fibres',- 'name',- 'protein',- 'sodium',- 'status',- 'language',- 'license',- 'license_author',- )+ filterset_class = IngredientFilterSet@method_decorator(cache_page(settings.WGER_SETTINGS['EXERCISE_CACHE_TTL']))def list(self, request, *args, **kwargs):@@ -474,12 +460,7 @@ class LogItemViewSet(WgerOwnerObjectModelViewSet):serializer_class = LogItemSerializeris_private = Trueordering_fields = '__all__'- filterset_fields = (- 'amount',- 'ingredient',- 'plan',- 'weight_unit',- )+ filterset_class = LogItemFilterSetdef get_queryset(self):"""