Skip to content

Commit

Permalink
Fix Update
Browse files Browse the repository at this point in the history
  • Loading branch information
LinaArtmv committed Sep 21, 2023
1 parent 2e51adc commit 483f85e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
46 changes: 27 additions & 19 deletions backend/foodgram/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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):
"""Сериализатор для чтения подписок."""
Expand Down
11 changes: 7 additions & 4 deletions backend/foodgram/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion backend/foodgram/foodgram/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
4 changes: 2 additions & 2 deletions backend/foodgram/users/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down

0 comments on commit 483f85e

Please sign in to comment.