Skip to content

Commit

Permalink
убрал ненежные поля из Recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-xx committed Nov 11, 2023
1 parent b6d48e7 commit cedc543
Show file tree
Hide file tree
Showing 19 changed files with 77 additions and 66 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,7 @@ static/
frontend/

# vscode
.vscode/
.vscode/
/.vscode
/.vscode/
.vscode
2 changes: 1 addition & 1 deletion backend/api/filters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import django_filters
from django_filters.widgets import BooleanWidget

from recipes.models import Recipe, Ingredient
from recipes.models import Ingredient, Recipe


class RecipeFilter(django_filters.FilterSet):
Expand Down
2 changes: 1 addition & 1 deletion backend/api/permissions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from rest_framework.permissions import BasePermission, SAFE_METHODS
from rest_framework.permissions import SAFE_METHODS, BasePermission


class IsOwnerOrReadOnly(BasePermission):
Expand Down
20 changes: 8 additions & 12 deletions backend/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import re
import base64
import re

from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.hashers import make_password
from django.core.files.base import ContentFile
from rest_framework import serializers
from rest_framework.validators import UniqueValidator
from django.conf import settings


from recipes.models import (Tag, Recipe, RecipeIngredient, Ingredient,
Favorite, ShoppingCart, Follow,)
from recipes.models import (Favorite, Follow, Ingredient, Recipe,
RecipeIngredient, ShoppingCart, Tag)

User = get_user_model()

Expand Down Expand Up @@ -318,9 +318,6 @@ def create(self, validated_data):

def update(self, instance, validated_data):
items = validated_data.pop('recipeingredient')
# if not items:
# raise serializers.ValidationError(
# 'Ингредиенты не могут быть пустыми')
RecipeIngredient.objects.filter(recipe=instance).delete()
instance = super().update(instance, validated_data)
self.create_ingredient(items, instance)
Expand Down Expand Up @@ -416,14 +413,13 @@ class Meta:
model = Follow

def get_is_subscribed(self, obj):
if Follow.objects.filter(follower=self.context.get('request').user,
author=obj.author).exists():
return True
return False
return Follow.objects.filter(follower=self.context.get('request').user,
author=obj.author).exists()

def to_representation(self, instance):
representation = super().to_representation(instance)
recipes = representation.get('recipes')
print(recipes)
length = len(recipes)
representation['recipes_count'] = length
limit = self.context.get('request').query_params.get('recipes_limit')
Expand Down
11 changes: 4 additions & 7 deletions backend/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from django.urls import path, include
from django.urls import include, path
from rest_framework.routers import DefaultRouter

from .views import (UserMe, TagsViewSet, RecipeViewSet,
FavoriteViewSet,
FollowViewSet,
FollowListViewSet,
IngredientViewSet,
ShoppingCartViewSet)
from .utils import DownloadViewSet
from .views import (FavoriteViewSet, FollowListViewSet, FollowViewSet,
IngredientViewSet, RecipeViewSet, ShoppingCartViewSet,
TagsViewSet, UserMe)

router = DefaultRouter()
router.register('tags',
Expand Down
6 changes: 3 additions & 3 deletions backend/api/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django.http import HttpResponse
from django.db.models import Sum
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
from django.http import HttpResponse
from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView

from recipes.models import RecipeIngredient, ShoppingCart

Expand Down
21 changes: 11 additions & 10 deletions backend/api/views.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
from django.db.models import Count
from django.contrib.auth import get_user_model
from django.db.models import Count
from django.shortcuts import get_object_or_404
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import viewsets, mixins, status
from rest_framework import mixins, status, viewsets
from rest_framework.decorators import action
from rest_framework.permissions import (AllowAny, IsAuthenticated,
IsAuthenticatedOrReadOnly)
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework.views import APIView

from core.pagination import CustomPagination
from recipes.models import (Tag, Recipe, Favorite, Follow,
Ingredient, ShoppingCart)
from .serializers import (UserSerializer, TagSerializer, RecipeSerializer,
RecipeCreateSerializer, UserMeSerializer,
FavoriteSerializer, FollowSerializer,
IngredientSerializer, ShoppingCartSerializer)
from recipes.models import (Favorite, Follow, Ingredient, Recipe, ShoppingCart,
Tag)

from .filters import IngredientFilter, RecipeFilter
from .permissions import IsOwnerOrReadOnly
from .filters import RecipeFilter, IngredientFilter
from .serializers import (FavoriteSerializer, FollowSerializer,
IngredientSerializer, RecipeCreateSerializer,
RecipeSerializer, ShoppingCartSerializer,
TagSerializer, UserMeSerializer, UserSerializer)

User = get_user_model()

Expand Down
3 changes: 2 additions & 1 deletion backend/core/management/commands/import.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from io import open
import json
import os
from io import open

from django.core.management.base import BaseCommand

from recipes.models import Ingredient as Ingrt
Expand Down
1 change: 0 additions & 1 deletion backend/core/management/commands/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django.core.management.base import BaseCommand
from django.db.models import Count


from recipes.models import Follow

User = get_user_model()
Expand Down
2 changes: 1 addition & 1 deletion backend/core/pagination.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rest_framework.pagination import PageNumberPagination
from django.conf import settings
from rest_framework.pagination import PageNumberPagination


class CustomPagination(PageNumberPagination):
Expand Down
2 changes: 1 addition & 1 deletion backend/foodgram/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib import admin
from django.urls import path, include
from django.urls import include, path

urlpatterns = [
path('admin/', admin.site.urls),
Expand Down
22 changes: 6 additions & 16 deletions backend/recipes/admin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django.contrib.auth import get_user_model
from django.contrib import admin
from django.contrib.auth import get_user_model

from .models import (Tag, Ingredient, Recipe, RecipeIngredient, Favorite,
ShoppingCart, Follow, RecipeTag)
from .models import (Favorite, Follow, Ingredient, Recipe, RecipeIngredient,
RecipeTag, ShoppingCart, Tag)

User = get_user_model()

Expand Down Expand Up @@ -52,8 +52,8 @@ class RecipeAdmin(admin.ModelAdmin):
'text',
'cooking_time',
'get_tag',
'get_in_shopping_card',
'get_is_favorited',
# 'get_in_shopping_card',
# 'get_is_favorited',
'pub_date',
'get_favorite_counter',
)
Expand All @@ -67,19 +67,9 @@ def get_tag(self, obj):
return ", ".join([p.name for p in obj.tags.all()])
get_tag.short_description = 'Теги'

def get_in_shopping_card(self, obj):
"""Позволяет увидеть все добавленные Список покупок."""
return ", ".join([p.username for p in obj.is_in_shopping_cart.all()])
get_in_shopping_card.short_description = 'В Списке покупок'

def get_is_favorited(self, obj):
"""Позволяет увидеть все добавленные в Избранное."""
return ", ".join([p.username for p in obj.is_favorited.all()])
get_is_favorited.short_description = 'В избранном'

def get_favorite_counter(self, obj):
"""Позволяет увидеть кол-во добавлений в Избранное."""
return obj.is_favorited.all().count()
return Favorite.objects.filter(recipe=obj).count()
get_favorite_counter.short_description = 'Всего в Избранном'


Expand Down
3 changes: 2 additions & 1 deletion backend/recipes/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Generated by Django 3.2.16 on 2023-11-06 10:30

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion

import recipes.validators


Expand Down
2 changes: 1 addition & 1 deletion backend/recipes/migrations/0005_recipe_pub_date.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by Django 3.2.16 on 2023-11-08 12:02

from django.db import migrations, models
import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):
Expand Down
1 change: 1 addition & 0 deletions backend/recipes/migrations/0007_auto_20231111_0536.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by Django 3.2.16 on 2023-11-11 01:36

from django.db import migrations, models

import recipes.validators


Expand Down
28 changes: 28 additions & 0 deletions backend/recipes/migrations/0008_auto_20231111_1154.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 3.2.16 on 2023-11-11 07:54

from django.db import migrations, models

import recipes.validators


class Migration(migrations.Migration):

dependencies = [
('recipes', '0007_auto_20231111_0536'),
]

operations = [
migrations.RemoveField(
model_name='recipe',
name='is_favorited',
),
migrations.RemoveField(
model_name='recipe',
name='is_in_shopping_cart',
),
migrations.AlterField(
model_name='recipeingredient',
name='amount',
field=models.IntegerField(validators=[recipes.validators.amount_validator], verbose_name='Количество'),
),
]
8 changes: 1 addition & 7 deletions backend/recipes/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.contrib.auth import get_user_model
from django.db import models

from .validators import time_validator, amount_validator
from .validators import amount_validator, time_validator

User = get_user_model()

Expand Down Expand Up @@ -56,12 +56,6 @@ class Recipe(models.Model):
ingredients = models.ManyToManyField(Ingredient,
through='RecipeIngredient',
blank=False,)
is_favorited = models.ManyToManyField(User,
through='Favorite',
related_name='recipes')
is_in_shopping_cart = models.ManyToManyField(User,
through='ShoppingCart',
related_name='products')
pub_date = models.DateTimeField('Дата публикации',
auto_now_add=True)

Expand Down
2 changes: 1 addition & 1 deletion backend/users/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin

User = get_user_model()

Expand Down
2 changes: 1 addition & 1 deletion backend/users/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):
Expand Down

0 comments on commit cedc543

Please sign in to comment.