mirror of
				https://gitlab.crans.org/bde/nk20
				synced 2025-11-04 09:12:11 +01:00 
			
		
		
		
	Alpha version (without tests)
This commit is contained in:
		@@ -3,7 +3,7 @@
 | 
			
		||||
 | 
			
		||||
from rest_framework import serializers
 | 
			
		||||
 | 
			
		||||
from ..models import Allergen, Food, BasicFood, TransformedFood, QRCode
 | 
			
		||||
from ..models import Allergen, Food, BasicFood, TransformedFood, QRCode, Dish, Supplement, Order, FoodTransaction
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class AllergenSerializer(serializers.ModelSerializer):
 | 
			
		||||
@@ -54,3 +54,43 @@ class QRCodeSerializer(serializers.ModelSerializer):
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = QRCode
 | 
			
		||||
        fields = '__all__'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DishSerializer(serializers.ModelSerializer):
 | 
			
		||||
    """
 | 
			
		||||
    REST API Serializer for Dish.
 | 
			
		||||
    The djangorestframework plugin will analyse the model `Dish` and parse all fields in the API.
 | 
			
		||||
    """
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = Dish
 | 
			
		||||
        fields = '__all__'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SupplementSerializer(serializers.ModelSerializer):
 | 
			
		||||
    """
 | 
			
		||||
    REST API Serializer for Supplement.
 | 
			
		||||
    The djangorestframework plugin will analyse the model `Supplement` and parse all fields in the API.
 | 
			
		||||
    """
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = Supplement
 | 
			
		||||
        fields = '__all__'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class OrderSerializer(serializers.ModelSerializer):
 | 
			
		||||
    """
 | 
			
		||||
    REST API Serializer for Order.
 | 
			
		||||
    The djangorestframework plugin will analyse the model `Order` and parse all fields in the API.
 | 
			
		||||
    """
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = Order
 | 
			
		||||
        fields = '__all__'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class FoodTransactionSerializer(serializers.ModelSerializer):
 | 
			
		||||
    """
 | 
			
		||||
    REST API Serializer for FoodTransaction.
 | 
			
		||||
    The djangorestframework plugin will analyse the model `FoodTransaction` and parse all fields in the API.
 | 
			
		||||
    """
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = FoodTransaction
 | 
			
		||||
        fields = '__all__'
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,8 @@
 | 
			
		||||
# Copyright (C) 2018-2025 by BDE ENS Paris-Saclay
 | 
			
		||||
# SPDX-License-Identifier: GPL-3.0-or-later
 | 
			
		||||
 | 
			
		||||
from .views import AllergenViewSet, FoodViewSet, BasicFoodViewSet, TransformedFoodViewSet, QRCodeViewSet
 | 
			
		||||
from .views import AllergenViewSet, FoodViewSet, BasicFoodViewSet, TransformedFoodViewSet, QRCodeViewSet, \
 | 
			
		||||
    DishViewSet, SupplementViewSet, OrderViewSet, FoodTransactionViewSet
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def register_food_urls(router, path):
 | 
			
		||||
@@ -13,3 +14,7 @@ def register_food_urls(router, path):
 | 
			
		||||
    router.register(path + '/basicfood', BasicFoodViewSet)
 | 
			
		||||
    router.register(path + '/transformedfood', TransformedFoodViewSet)
 | 
			
		||||
    router.register(path + '/qrcode', QRCodeViewSet)
 | 
			
		||||
    router.register(path + '/dish', DishViewSet)
 | 
			
		||||
    router.register(path + '/supplement', SupplementViewSet)
 | 
			
		||||
    router.register(path + '/order', OrderViewSet)
 | 
			
		||||
    router.register(path + '/foodtransaction', FoodTransactionViewSet)
 | 
			
		||||
 
 | 
			
		||||
@@ -3,10 +3,12 @@
 | 
			
		||||
 | 
			
		||||
from api.viewsets import ReadProtectedModelViewSet
 | 
			
		||||
from django_filters.rest_framework import DjangoFilterBackend
 | 
			
		||||
from django.utils import timezone
 | 
			
		||||
from rest_framework.filters import SearchFilter
 | 
			
		||||
 | 
			
		||||
from .serializers import AllergenSerializer, FoodSerializer, BasicFoodSerializer, TransformedFoodSerializer, QRCodeSerializer
 | 
			
		||||
from ..models import Allergen, Food, BasicFood, TransformedFood, QRCode
 | 
			
		||||
from .serializers import AllergenSerializer, FoodSerializer, BasicFoodSerializer, TransformedFoodSerializer, QRCodeSerializer, \
 | 
			
		||||
    DishSerializer, SupplementSerializer, OrderSerializer, FoodTransactionSerializer
 | 
			
		||||
from ..models import Allergen, Food, BasicFood, TransformedFood, QRCode, Dish, Supplement, Order, FoodTransaction
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class AllergenViewSet(ReadProtectedModelViewSet):
 | 
			
		||||
@@ -72,3 +74,61 @@ class QRCodeViewSet(ReadProtectedModelViewSet):
 | 
			
		||||
    filter_backends = [DjangoFilterBackend, SearchFilter]
 | 
			
		||||
    filterset_fields = ['qr_code_number', ]
 | 
			
		||||
    search_fields = ['$qr_code_number', ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DishViewSet(ReadProtectedModelViewSet):
 | 
			
		||||
    """
 | 
			
		||||
    REST API View set.
 | 
			
		||||
    The djangorestframework plugin will get all `Dish` objects, serialize it to JSON with the given serializer,
 | 
			
		||||
    then render it on /api/food/dish/
 | 
			
		||||
    """
 | 
			
		||||
    queryset = Dish.objects.order_by('id')
 | 
			
		||||
    serializer_class = DishSerializer
 | 
			
		||||
    filter_backends = [DjangoFilterBackend, SearchFilter]
 | 
			
		||||
    filterset_fields = ['main__name', 'activity', ]
 | 
			
		||||
    search_fields = ['$main__name', '$activity', ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SupplementViewSet(ReadProtectedModelViewSet):
 | 
			
		||||
    """
 | 
			
		||||
    REST API View set.
 | 
			
		||||
    The djangorestframework plugin will get all `Supplement` objects, serialize it to JSON with the given serializer,
 | 
			
		||||
    then render it on /api/food/supplement/
 | 
			
		||||
    """
 | 
			
		||||
    queryset = Supplement.objects.order_by('id')
 | 
			
		||||
    serializer_class = SupplementSerializer
 | 
			
		||||
    filter_backends = [DjangoFilterBackend, SearchFilter]
 | 
			
		||||
    filterset_fields = ['food__name', 'dish__activity', ]
 | 
			
		||||
    search_fields = ['$food__name', '$dish__activity', ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class OrderViewSet(ReadProtectedModelViewSet):
 | 
			
		||||
    """
 | 
			
		||||
    REST API View set.
 | 
			
		||||
    The djangorestframework plugin will get all `Order` objects, serialize it to JSON with the given serializer,
 | 
			
		||||
    then render it on /api/food/order/
 | 
			
		||||
    """
 | 
			
		||||
    queryset = Order.objects.order_by('id')
 | 
			
		||||
    serializer_class = OrderSerializer
 | 
			
		||||
    filter_backends = [DjangoFilterBackend, SearchFilter]
 | 
			
		||||
    filterset_fields = ['user', 'activity', 'dish', 'supplements', 'number', ]
 | 
			
		||||
    search_fields = ['$user', '$activity', '$dish', '$supplements', '$number', ]
 | 
			
		||||
 | 
			
		||||
    def perform_update(self, serializer):
 | 
			
		||||
        instance = serializer.save()
 | 
			
		||||
        if instance.served and not instance.served_at:
 | 
			
		||||
            instance.served_at = timezone.now()
 | 
			
		||||
            instance.save()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class FoodTransactionViewSet(ReadProtectedModelViewSet):
 | 
			
		||||
    """
 | 
			
		||||
    REST API View set.
 | 
			
		||||
    The djangorestframework plugin will get all `FoodTransaction` objects, serialize it to JSON with the given serializer,
 | 
			
		||||
    then render it on /api/food/foodtransaction/
 | 
			
		||||
    """
 | 
			
		||||
    queryset = FoodTransaction.objects.order_by('id')
 | 
			
		||||
    serializer_class = FoodTransactionSerializer
 | 
			
		||||
    filter_backends = [DjangoFilterBackend, SearchFilter]
 | 
			
		||||
    filterset_fields = ['order', ]
 | 
			
		||||
    search_fields = ['$order', ]
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user