Skip to content

Commit

Permalink
11
Browse files Browse the repository at this point in the history
  • Loading branch information
fabilya committed Oct 1, 2023
1 parent 6dd7e9c commit 31c6e4f
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from rest_framework.response import Response

from .filters import IngredientFilter, RecipeFilter
from recipes.models import Favorite, Ingredient, Recipe, ShoppingCart, Tag
from recipes.models import (Favorite, Ingredient, IngredientAmount,
Recipe, ShoppingCart, Tag)
from .permissions import OwnerUserOrReadOnly

from .serializers import (
Expand Down Expand Up @@ -61,7 +62,7 @@ def get_permissions(self):
'shopping_cart': [permissions.IsAuthenticated()],
'download_shopping_cart': [permissions.IsAuthenticated()],
'list': [permissions.AllowAny()],
'retrieve': [OwnerUserOrReadOnly],
'retrieve': [permissions.AllowAny()],
}
return permissions_dict.get(
self.action, [permissions.IsAuthenticated()]
Expand Down Expand Up @@ -129,12 +130,12 @@ def download_shopping_cart(self, request):
if not user.shoppingcart.exists():
return Response(status=status.HTTP_400_BAD_REQUEST)

shopping_cart = (
request.user.shoppingcart.recipe.
values(
'ingredients__name',
'ingredients__measurement_unit'
).annotate(amount=Sum('recipe__amount')).order_by())
ingredients = IngredientAmount.objects.filter(
recipe__shoppingcart__user=request.user
).values(
'ingredient__name',
'ingredient__measurement_unit'
).annotate(cart_amount=Sum('ingredient__amount')).order_by()

today = datetime.today()
shopping_list = (
Expand All @@ -144,7 +145,7 @@ def download_shopping_cart(self, request):
f'- {ingredient["ingredient__name"]} '
f'({ingredient["ingredient__measurement_unit"]})'
f' - {ingredient["cart_amount"]}'
for ingredient in shopping_cart
for ingredient in ingredients
])
shopping_list += f'\n\nFoodgram ({today:%Y})'

Expand Down

0 comments on commit 31c6e4f

Please sign in to comment.