nk20/apps/sheets/api/views.py

79 lines
3.5 KiB
Python

# Copyright (C) 2018-2021 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from api.viewsets import ReadProtectedModelViewSet
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import SearchFilter, OrderingFilter
from .serializers import SheetSerializer, FoodSerializer, FoodOptionSerializer, MealSerializer, OrderSerializer, \
OrderedMealSerializer, OrderedFoodSerializer, SheetOrderTransactionSerializer
from ..models import Sheet, Food, FoodOption, Meal, Order, OrderedMeal, OrderedFood, SheetOrderTransaction
class SheetViewSet(ReadProtectedModelViewSet):
queryset = Sheet.objects.order_by('id')
serializer_class = SheetSerializer
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ['name', 'date', ]
search_fields = ['$name', ]
class FoodViewSet(ReadProtectedModelViewSet):
queryset = Food.objects.order_by('id')
serializer_class = FoodSerializer
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ['name', 'sheet', 'price', 'club', 'available', ]
search_fields = ['$name', ]
class FoodOptionViewSet(ReadProtectedModelViewSet):
queryset = FoodOption.objects.order_by('id')
serializer_class = FoodOptionSerializer
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ['name', 'food', 'extra_cost', 'available', ]
search_fields = ['$name', '$food__name', ]
class MealViewSet(ReadProtectedModelViewSet):
queryset = Meal.objects.order_by('id')
serializer_class = MealSerializer
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ['name', 'content', 'price', 'available', ]
search_fields = ['$name', ]
class OrderViewSet(ReadProtectedModelViewSet):
queryset = Order.objects.order_by('id')
serializer_class = OrderSerializer
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ['sheet', 'note', 'date', 'gift', ]
search_fields = ['$sheet__name', '$note__alias__name', '$note__alias__normalized_name', ]
class OrderedMealViewSet(ReadProtectedModelViewSet):
queryset = OrderedMeal.objects.order_by('id')
serializer_class = OrderedMealSerializer
filter_backends = [DjangoFilterBackend]
filterset_fields = ['order', 'meal', ]
class OrderedFoodViewSet(ReadProtectedModelViewSet):
queryset = OrderedFood.objects.order_by('id')
serializer_class = OrderedFoodSerializer
filter_backends = [DjangoFilterBackend]
filterset_fields = ['order', 'meal', 'food', 'options', 'number', 'status', 'served_date', ]
class SheetOrderTransactionViewSet(ReadProtectedModelViewSet):
queryset = SheetOrderTransaction.objects.order_by('-created_at')
serializer_class = SheetOrderTransactionSerializer
filter_backends = [DjangoFilterBackend, SearchFilter, OrderingFilter]
filterset_fields = ['source', 'source_alias', 'source__alias__name', 'source__alias__normalized_name',
'destination', 'destination_alias', 'destination__alias__name',
'destination__alias__normalized_name', 'quantity', 'polymorphic_ctype', 'amount',
'created_at', 'valid', 'invalidity_reason', 'ordered_food', ]
search_fields = ['$reason', '$source_alias', '$source__alias__name', '$source__alias__normalized_name',
'$destination_alias', '$destination__alias__name', '$destination__alias__normalized_name',
'$invalidity_reason', ]
ordering_fields = ['created_at', 'amount', ]