From abce5b18fd66a5ce7bd52080920ddce2257aff4c Mon Sep 17 00:00:00 2001 From: Ching Date: Sat, 5 Feb 2022 00:38:30 +0800 Subject: [PATCH] =?UTF-8?q?feat(recipe=20api):=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=8F=9C=E8=B0=B1=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/migrations/0003_recipe_status.py | 18 ++++++++++++++++++ recipe/models.py | 22 +++++++++++++++++++--- recipe/views.py | 10 +++++++--- utils/const.py | 3 +++ 4 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 recipe/migrations/0003_recipe_status.py diff --git a/recipe/migrations/0003_recipe_status.py b/recipe/migrations/0003_recipe_status.py new file mode 100644 index 0000000..8ecba17 --- /dev/null +++ b/recipe/migrations/0003_recipe_status.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.6 on 2022-02-04 16:13 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('recipe', '0002_auto_20211002_1926'), + ] + + operations = [ + migrations.AddField( + model_name='recipe', + name='status', + field=models.CharField(default='active', max_length=32), + ), + ] diff --git a/recipe/models.py b/recipe/models.py index 2cc9982..94c2dd1 100644 --- a/recipe/models.py +++ b/recipe/models.py @@ -9,6 +9,7 @@ import utils class Recipe(models.Model): name = models.CharField(max_length=128) recipe_type = models.CharField(max_length=32, default=const.RECIPE_TYPE_MEAT) + status = models.CharField(max_length=32, default=const.RECIPE_STATUS_ACTIVE) note = models.TextField(null=True) rate = models.IntegerField(default=0) difficulty = models.IntegerField(default=0) @@ -125,7 +126,12 @@ class DailyRecipe(models.Model): # meat for x in range(0, 2): while True: - recipe = Recipe.objects.filter(recipe_type=const.RECIPE_TYPE_MEAT).order_by('?').first() + recipe = ( + Recipe.objects.filter(recipe_type=const.RECIPE_TYPE_MEAT) + .exclude(status=const.RECIPE_STATUS_DELETED) + .order_by('?') + .first() + ) if recipe and recipe.id not in recipes and recipe.id not in prev_recipes: recipes.append(recipe.id) break @@ -138,7 +144,12 @@ class DailyRecipe(models.Model): # vegetable for x in range(0, 1): while True: - recipe = Recipe.objects.filter(recipe_type=const.RECIPE_TYPE_VEGETABLE).order_by('?').first() + recipe = ( + Recipe.objects.filter(recipe_type=const.RECIPE_TYPE_VEGETABLE) + .exclude(status=const.RECIPE_STATUS_DELETED) + .order_by('?') + .first() + ) if recipe and recipe.id not in recipes and recipe.id not in prev_recipes: recipes.append(recipe.id) break @@ -150,7 +161,12 @@ class DailyRecipe(models.Model): # soup if random.randint(0, 2): - recipe = Recipe.objects.filter(recipe_type=const.RECIPE_TYPE_SOUP).order_by('?').first() + recipe = ( + Recipe.objects.filter(recipe_type=const.RECIPE_TYPE_SOUP) + .exclude(status=const.RECIPE_STATUS_DELETED) + .order_by('?') + .first() + ) # if recipe not in recipes and recipe not in prev_recipes: if recipe: recipes.append(recipe.id) diff --git a/recipe/views.py b/recipe/views.py index 175c057..e958f5f 100644 --- a/recipe/views.py +++ b/recipe/views.py @@ -12,15 +12,19 @@ import recipe.serializers from utils import const -class RecipeAPI(rest_framework.generics.RetrieveUpdateAPIView): +class RecipeAPI(rest_framework.generics.RetrieveUpdateDestroyAPIView): # authentication_classes = (authentication.TokenAuthentication, # authentication.SessionAuthentication, # authentication.BasicAuthentication) # permission_classes = (permissions.IsAuthenticated,) - queryset = recipe.models.Recipe.objects.all() + queryset = recipe.models.Recipe.objects.exclude(status=const.RECIPE_STATUS_DELETED) serializer_class = recipe.serializers.RecipeSerializer + def perform_destroy(self, instance): + instance.status = const.RECIPE_STATUS_DELETED + instance.save(update_fields=['status']) + class RecipeListAPI(rest_framework.generics.ListAPIView, rest_framework.generics.CreateAPIView): @@ -28,7 +32,7 @@ class RecipeListAPI(rest_framework.generics.ListAPIView, rest_framework.generics # authentication.SessionAuthentication, # authentication.BasicAuthentication) # permission_classes = (permissions.IsAuthenticated,) - queryset = recipe.models.Recipe.objects.all() + queryset = recipe.models.Recipe.objects.exclude(status=const.RECIPE_STATUS_DELETED) serializer_class = recipe.serializers.RecipeSerializer filterset_fields = { 'recipe_type': const.FILTER_EXACT, diff --git a/utils/const.py b/utils/const.py index bdfc767..ff00587 100644 --- a/utils/const.py +++ b/utils/const.py @@ -11,6 +11,9 @@ RECIPE_TYPE_MEAT = 'meat' RECIPE_TYPE_VEGETABLE = 'vegetable' RECIPE_TYPE_SOUP = 'soup' +RECIPE_STATUS_ACTIVE = 'active' +RECIPE_STATUS_DELETED = 'deleted' + RECIPE_TYPE_CHOICE = [RECIPE_TYPE_MEAT, RECIPE_TYPE_VEGETABLE, RECIPE_TYPE_SOUP] FILTER_EXACT = ['exact']