diff --git a/backend/foodgram/api/serializers.py b/backend/foodgram/api/serializers.py index 99d0143..48b90ae 100644 --- a/backend/foodgram/api/serializers.py +++ b/backend/foodgram/api/serializers.py @@ -159,6 +159,29 @@ def validate_ingredients(self, value): ingredients_list.append(ingredient) return value + def _create_ingredient(self, ingredients, recipe): + IngredientRecipe.objects.bulk_create([ + IngredientRecipe( + recipe=recipe, + ingredient=ingredient['id'], + amount=ingredient['amount'] + ) for ingredient in ingredients]) + return + + def to_representation(self, instance): + request = self.context.get('request') + return RecipeReadSerializer(instance, + context={'request': request}).data + + class Meta: + model = Recipe + fields = ('tags', 'ingredients', 'image', + 'name', 'text', 'cooking_time') + + +class RecipeCreateSerializer(RecipeWriteSerializer): + """Сериализатор для создания рецепта.""" + def validate(self, data): tags = data['tags'] ingredients = data['ingredients'] @@ -173,15 +196,6 @@ def validate(self, data): 'Такой рецепт уже есть, измените название или описание!') return data - def _create_ingredient(self, ingredients, recipe): - IngredientRecipe.objects.bulk_create([ - IngredientRecipe( - recipe=recipe, - ingredient=ingredient['id'], - amount=ingredient['amount'] - ) for ingredient in ingredients]) - return - @transaction.atomic def create(self, validated_data): tags = validated_data.pop('tags') @@ -191,6 +205,10 @@ def create(self, validated_data): self._create_ingredient(ingredients, recipe) return recipe + +class RecipeUpdateSerializer(RecipeWriteSerializer): + """Сериализатор для обновления рецепта.""" + @transaction.atomic def update(self, instance, validated_data): tags = validated_data.pop('tags') @@ -202,16 +220,6 @@ def update(self, instance, validated_data): instance.save() return instance - def to_representation(self, instance): - request = self.context.get('request') - return RecipeReadSerializer(instance, - context={'request': request}).data - - class Meta: - model = Recipe - fields = ('tags', 'ingredients', 'image', - 'name', 'text', 'cooking_time') - class SubscritionRecipeSerializer(serializers.ModelSerializer): """Сериализатор для чтения подписок.""" diff --git a/backend/foodgram/api/views.py b/backend/foodgram/api/views.py index 624af61..5a01032 100644 --- a/backend/foodgram/api/views.py +++ b/backend/foodgram/api/views.py @@ -12,9 +12,10 @@ from .filters import IngredientFilter, RecipeFilter from .permissions import IsOwnerOrAdminOrReadOnly from .serializers import (FavoriteSerializer, IngredientSerializer, - RecipeReadSerializer, RecipeWriteSerializer, - ShoppingCartSerializer, SubscriptionSerializer, - SubscriptionsSerializer, TagSerialiser) + RecipeCreateSerializer, RecipeReadSerializer, + RecipeUpdateSerializer, ShoppingCartSerializer, + SubscriptionSerializer, SubscriptionsSerializer, + TagSerialiser) class TagViewSet(viewsets.ReadOnlyModelViewSet): @@ -50,7 +51,9 @@ class RecipeViewSet(viewsets.ModelViewSet): def get_serializer_class(self): if self.action in ('list', 'retrieve'): return RecipeReadSerializer - return RecipeWriteSerializer + if self.action in ('update', 'partial_update'): + return RecipeUpdateSerializer + return RecipeCreateSerializer def perform_create(self, serializer): return serializer.save(author=self.request.user) diff --git a/backend/foodgram/foodgram/settings.py b/backend/foodgram/foodgram/settings.py index 0a87067..c7b6432 100644 --- a/backend/foodgram/foodgram/settings.py +++ b/backend/foodgram/foodgram/settings.py @@ -10,7 +10,7 @@ SECRET_KEY = os.getenv('SECRET_KEY', 'default') -DEBUG = True +DEBUG = False ALLOWED_HOSTS = ['linaartfoodgram.sytes.net', '158.160.30.28', 'localhost', '127.0.0.1'] diff --git a/backend/foodgram/users/migrations/0001_initial.py b/backend/foodgram/users/migrations/0001_initial.py index 38beaa9..e7ebb6c 100644 --- a/backend/foodgram/users/migrations/0001_initial.py +++ b/backend/foodgram/users/migrations/0001_initial.py @@ -1,10 +1,10 @@ # Generated by Django 3.2.20 on 2023-09-18 12:34 -from django.conf import settings import django.contrib.auth.models -from django.db import migrations, models import django.db.models.deletion import django.utils.timezone +from django.conf import settings +from django.db import migrations, models class Migration(migrations.Migration):