From a7e87ea639b7bb398409c984376b4b5d1cd7e00f Mon Sep 17 00:00:00 2001 From: korenstin Date: Sun, 4 Aug 2024 23:38:21 +0200 Subject: [PATCH] API Food --- apps/api/urls.py | 26 ++++++++------- apps/food/api/__init__.py | 0 apps/food/api/serializers.py | 50 +++++++++++++++++++++++++++++ apps/food/api/urls.py | 14 +++++++++ apps/food/api/views.py | 61 ++++++++++++++++++++++++++++++++++++ 5 files changed, 140 insertions(+), 11 deletions(-) create mode 100644 apps/food/api/__init__.py create mode 100644 apps/food/api/serializers.py create mode 100644 apps/food/api/urls.py create mode 100644 apps/food/api/views.py diff --git a/apps/api/urls.py b/apps/api/urls.py index 7c73093b..6a09411f 100644 --- a/apps/api/urls.py +++ b/apps/api/urls.py @@ -14,29 +14,33 @@ router = routers.DefaultRouter() router.register('models', ContentTypeViewSet) router.register('user', UserViewSet) +if "activity" in settings.INSTALLED_APPS: + from activity.api.urls import register_activity_urls + register_activity_urls(router, 'activity') + +if "food" in settings.INSTALLED_APPS: + from food.api.urls import register_food_urls + register_food_urls(router, 'food') + +if "logs" in settings.INSTALLED_APPS: + from logs.api.urls import register_logs_urls + register_logs_urls(router, 'logs') + if "member" in settings.INSTALLED_APPS: from member.api.urls import register_members_urls register_members_urls(router, 'members') -if "member" in settings.INSTALLED_APPS: - from activity.api.urls import register_activity_urls - register_activity_urls(router, 'activity') - if "note" in settings.INSTALLED_APPS: from note.api.urls import register_note_urls register_note_urls(router, 'note') -if "treasury" in settings.INSTALLED_APPS: - from treasury.api.urls import register_treasury_urls - register_treasury_urls(router, 'treasury') - if "permission" in settings.INSTALLED_APPS: from permission.api.urls import register_permission_urls register_permission_urls(router, 'permission') -if "logs" in settings.INSTALLED_APPS: - from logs.api.urls import register_logs_urls - register_logs_urls(router, 'logs') +if "treasury" in settings.INSTALLED_APPS: + from treasury.api.urls import register_treasury_urls + register_treasury_urls(router, 'treasury') if "wei" in settings.INSTALLED_APPS: from wei.api.urls import register_wei_urls diff --git a/apps/food/api/__init__.py b/apps/food/api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/food/api/serializers.py b/apps/food/api/serializers.py new file mode 100644 index 00000000..9c42013f --- /dev/null +++ b/apps/food/api/serializers.py @@ -0,0 +1,50 @@ +# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay +# SPDX-License-Identifier: GPL-3.0-or-later + +from rest_framework import serializers + +from ..models import Allergen, BasicFood, QRCode, TransformedFood + + +class AllergenSerializer(serializers.ModelSerializer): + """ + REST API Serializer for Allergen. + The djangorestframework plugin will analyse the model `Allergen` and parse all fields in the API. + """ + + class Meta: + model = Allergen + fields = '__all__' + + +class BasicFoodSerializer(serializers.ModelSerializer): + """ + REST API Serializer for BasicFood. + The djangorestframework plugin will analyse the model `BasicFood` and parse all fields in the API. + """ + + class Meta: + model = BasicFood + fields = '__all__' + + +class QRCodeSerializer(serializers.ModelSerializer): + """ + REST API Serializer for QRCode. + The djangorestframework plugin will analyse the model `QRCode` and parse all fields in the API. + """ + + class Meta: + model = QRCode + fields = '__all__' + + +class TransformedFoodSerializer(serializers.ModelSerializer): + """ + REST API Serializer for TransformedFood. + The djangorestframework plugin will analyse the model `TransformedFood` and parse all fields in the API. + """ + + class Meta: + model = TransformedFood + fields = '__all__' diff --git a/apps/food/api/urls.py b/apps/food/api/urls.py new file mode 100644 index 00000000..acfb635d --- /dev/null +++ b/apps/food/api/urls.py @@ -0,0 +1,14 @@ +# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay +# SPDX-License-Identifier: GPL-3.0-or-later + +from .views import AllergenViewSet, BasicFoodViewSet, QRCodeViewSet, TransformedFoodViewSet + + +def register_food_urls(router, path): + """ + Configure router for Food REST API. + """ + router.register(path + '/allergen', AllergenViewSet) + router.register(path + '/basic_food', BasicFoodViewSet) + router.register(path + '/qrcode', QRCodeViewSet) + router.register(path + '/transformed_food', TransformedFoodViewSet) diff --git a/apps/food/api/views.py b/apps/food/api/views.py new file mode 100644 index 00000000..824ff809 --- /dev/null +++ b/apps/food/api/views.py @@ -0,0 +1,61 @@ +# Copyright (C) 2018-2024 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 + +from .serializers import AllergenSerializer, BasicFoodSerializer, QRCodeSerializer, TransformedFoodSerializer +from ..models import Allergen, BasicFood, QRCode, TransformedFood + + +class AllergenViewSet(ReadProtectedModelViewSet): + """ + REST API View set. + The djangorestframework plugin will get all `Allergen` objects, serialize it to JSON with the given serializer, + then render it on /api/food/allergen/ + """ + queryset = Allergen.objects.order_by('id') + serializer_class = AllergenSerializer + filter_backends = [DjangoFilterBackend, SearchFilter] + filterset_fields = ['name', ] + search_fields = ['$name', ] + + +class BasicFoodViewSet(ReadProtectedModelViewSet): + """ + REST API View set. + The djangorestframework plugin will get all `BasicFood` objects, serialize it to JSON with the given serializer, + then render it on /api/food/basic_food/ + """ + queryset = BasicFood.objects.order_by('id') + serializer_class = BasicFoodSerializer + filter_backends = [DjangoFilterBackend, SearchFilter] + filterset_fields = ['name', ] + search_fields = ['$name', ] + + +class QRCodeViewSet(ReadProtectedModelViewSet): + """ + REST API View set. + The djangorestframework plugin will get all `QRCode` objects, serialize it to JSON with the given serializer, + then render it on /api/food/qrcode/ + """ + queryset = QRCode.objects.order_by('id') + serializer_class = QRCodeSerializer + filter_backends = [DjangoFilterBackend, SearchFilter] + filterset_fields = ['qr_code_number', ] + search_fields = ['$qr_code_number', ] + + +class TransformedFoodViewSet(ReadProtectedModelViewSet): + """ + REST API View set. + The djangorestframework plugin will get all `TransformedFood` objects, serialize it to JSON with the given serializer, + then render it on /api/food/transformed_food/ + """ + queryset = TransformedFood.objects.order_by('id') + serializer_class = TransformedFoodSerializer + filter_backends = [DjangoFilterBackend, SearchFilter] + filterset_fields = ['name', ] + search_fields = ['$name', ]