From d16cfa0ca29f50b6d85414a28f55a0464fff908d Mon Sep 17 00:00:00 2001 From: Ching Date: Sun, 27 Feb 2022 19:41:45 +0800 Subject: [PATCH] =?UTF-8?q?feat(recipe=20views):=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E8=8F=9C=E8=B0=B1=E5=8E=9F=E6=9D=90=E6=96=99=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加菜谱原材料统计接口 Signed-off-by: Ching --- recipe/models.py | 16 ++++++++++++++++ recipe/serializers.py | 4 ++++ recipe/urls.py | 1 + recipe/views.py | 21 +++++++++++++++++++++ 4 files changed, 42 insertions(+) diff --git a/recipe/models.py b/recipe/models.py index 0348c7e..3e2ebad 100644 --- a/recipe/models.py +++ b/recipe/models.py @@ -141,6 +141,22 @@ class Recipe(models.Model): } return data + @classmethod + def sum_recipe_ingredients(cls, recipe_ids): + data = {} + for recipe_id in recipe_ids: + recipe = cls.objects.get(id=recipe_id) + recipe_ingredients = RecipeIngredient.objects.filter(recipe=recipe) + for ingredient in recipe_ingredients: + if not ingredient.ingredient.id in data: + data[ingredient.ingredient.id] = { + 'name': ingredient.ingredient.name, + 'quantity': 0, + 'unit': ingredient.ingredient.unit, + } + data[ingredient.ingredient.id]['quantity'] += ingredient.quantity + return data + class DailyRecipe(models.Model): recipes = models.ManyToManyField(Recipe) diff --git a/recipe/serializers.py b/recipe/serializers.py index 784d918..155c353 100644 --- a/recipe/serializers.py +++ b/recipe/serializers.py @@ -58,6 +58,10 @@ class RecipeSerializer(serializers.ModelSerializer): fields = ('recipe_ingredients', 'id', 'name', 'recipe_type', 'status', 'note', 'rate', 'difficulty') +class RecipeIngredientSummarySerializer(serializers.Serializer): + pass + + class WeekRecipeSerializer(serializers.ModelSerializer): class Meta: model = recipe.models.DailyRecipe diff --git a/recipe/urls.py b/recipe/urls.py index 6501b7c..bf278d7 100644 --- a/recipe/urls.py +++ b/recipe/urls.py @@ -17,6 +17,7 @@ recipe_nested_router.register(r'recipe-ingredient', views.RecipeIngredientAPI) urlpatterns = [ url(r'^week-recipe/$', views.WeekRecipeListAPI.as_view()), url(r'^daily-recipe/(?P\d+)$', views.DailyRecipeAPI.as_view(), name='dailyrecipe-detail'), + url(r'^ingredient-summary/$', views.RecipeIngredientSummaryAPI.as_view()), path(r'', include(router.urls)), path(r'', include(recipe_nested_router.urls)), ] diff --git a/recipe/views.py b/recipe/views.py index 9f85401..4d933da 100644 --- a/recipe/views.py +++ b/recipe/views.py @@ -142,3 +142,24 @@ class RecipeIngredientAPI(viewsets.ModelViewSet): recipe_id=kwargs['recipe_pk'], ingredient_id=request.data['ingredient'], quantity=request.data['quantity'] ) return Response(recipe_ingredient.serialize(), status=status.HTTP_201_CREATED, headers={}) + + +# class RecipeIngredientSummaryAPI(viewsets.ViewSetMixin, rest_framework.generics.CreateAPIView): +class RecipeIngredientSummaryAPI(rest_framework.generics.CreateAPIView): + + # authentication_classes = (authentication.TokenAuthentication, + # authentication.SessionAuthentication, + # authentication.BasicAuthentication) + # permission_classes = (permissions.IsAuthenticated,) + queryset = recipe.models.RecipeIngredient.objects.all() + serializer_class = recipe.serializers.RecipeIngredientSummarySerializer + + def post(self, request, *args, **kwargs): + daily_recipe_ids = request.data.get('daily_recipes', []) + recipe_ids = request.data.get('recipes', []) + if daily_recipe_ids: + daily_recipes = recipe.models.DailyRecipe.objects.filter(id__in=daily_recipe_ids) + recipe_ids = [recipe.id for daily_recipe in daily_recipes for recipe in daily_recipe.recipes.all()] + + data = recipe.models.Recipe.sum_recipe_ingredients(recipe_ids) + return Response(data, status=status.HTTP_200_OK, headers={})