Skip to content

Commit

Permalink
review-fix. Stylefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Resistor-git committed Sep 25, 2023
1 parent c56f80f commit 45fc652
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
65 changes: 48 additions & 17 deletions backend/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
from django.contrib.auth import get_user_model
from django.core.files.base import ContentFile
from django.db import transaction
from django.shortcuts import get_object_or_404
from djoser.serializers import UserCreateSerializer, UserSerializer
from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator

from recipes.models import (
Recipe,
Expand Down Expand Up @@ -236,7 +234,8 @@ def get_is_favorited(self, obj):
"""
Returns True if the recipe is in favorites of the current user.
obj is the recipe (Recipe instance).
Anonymous user can not have favorites, so func returns False in this case.
Anonymous user can not have favorites,
so func returns False in this case.
"""
current_user = self.context.get('request').user
if current_user.is_authenticated:
Expand All @@ -247,7 +246,8 @@ def get_is_in_shopping_cart(self, obj):
"""
Returns True if the recipe is in shopping cart of the current user.
obj is the recipe (Recipe instance).
Anonymous user can not have anything in shopping cart, so func returns False in this case.
Anonymous user can not have anything in shopping cart,
so func returns False in this case.
"""
current_user = self.context.get('request').user
if current_user.is_authenticated:
Expand Down Expand Up @@ -289,14 +289,26 @@ class SubscriptionCreateDestroySerializer(serializers.ModelSerializer):
def validate(self, data):
if self.context['request'].method == 'POST':
if data['user'] == data['author']:
raise serializers.ValidationError('Can not subscribe to yourself')
if Subscription.objects.filter(user=data['user'], author=data['author']).exists():
raise serializers.ValidationError('You are already subscribed to that author')
raise serializers.ValidationError(
'Can not subscribe to yourself'
)
if Subscription.objects.filter(
user=data['user'],
author=data['author']
).exists():
raise serializers.ValidationError(
'You are already subscribed to that author'
)
return data
if self.context['request'].method == 'DELETE':
if Subscription.objects.filter(user=data['user'], author=data['author']).exists():
if Subscription.objects.filter(
user=data['user'],
author=data['author']
).exists():
return data
raise serializers.ValidationError('Not subscribed')
raise serializers.ValidationError(
'Not subscribed'
)

def to_representation(self, instance):
return SubscriptionRetrieveSerializer(instance['author'], context={
Expand Down Expand Up @@ -361,12 +373,21 @@ def to_representation(self, instance):

def validate(self, data):
if self.context['request'].method == 'POST':
if Favorite.objects.filter(user=data['user'], recipe=data['recipe']).exists():
raise serializers.ValidationError('Already in favorites')
if Favorite.objects.filter(
user=data['user'], recipe=data['recipe']
).exists():
raise serializers.ValidationError(
'Already in favorites'
)
if self.context['request'].method == 'DELETE':
if Favorite.objects.filter(user=data['user'], recipe=data['recipe']).exists():
if Favorite.objects.filter(
user=data['user'],
recipe=data['recipe']
).exists():
return data
raise serializers.ValidationError('The recipe is not in favorites')
raise serializers.ValidationError(
'The recipe is not in favorites'
)
return data

class Meta:
Expand Down Expand Up @@ -396,13 +417,23 @@ def validate_recipe(self, value):

def validate(self, data):
if self.context['request'].method == 'POST':
if ShoppingCart.objects.filter(user=data['user'], recipe=data['recipe']).exists():
raise serializers.ValidationError('The recipe is already in shopping cart')
if ShoppingCart.objects.filter(
user=data['user'],
recipe=data['recipe']
).exists():
raise serializers.ValidationError(
'The recipe is already in shopping cart'
)
return data
if self.context['request'].method == 'DELETE':
if ShoppingCart.objects.filter(user=data['user'], recipe=data['recipe']).exists():
if ShoppingCart.objects.filter(
user=data['user'],
recipe=data['recipe']
).exists():
return data
raise serializers.ValidationError('The recipe is not in shopping cart')
raise serializers.ValidationError(
'The recipe is not in shopping cart'
)

class Meta:
model = ShoppingCart
Expand Down
2 changes: 1 addition & 1 deletion backend/foodgram/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

SECRET_KEY = os.getenv('SECRET_KEY', 'default_stub_key')

DEBUG = True
DEBUG = False

ALLOWED_HOSTS = [
'127.0.0.1',
Expand Down

0 comments on commit 45fc652

Please sign in to comment.