feat(recipe api): 增加删除菜谱接口
增加删除菜谱接口 Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
parent
1277accf5c
commit
abce5b18fd
18
recipe/migrations/0003_recipe_status.py
Normal file
18
recipe/migrations/0003_recipe_status.py
Normal file
@ -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),
|
||||
),
|
||||
]
|
||||
@ -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)
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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']
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user