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 f51ed3f commit 9a2380a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
14 changes: 7 additions & 7 deletions backend/api/permissions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from rest_framework import permissions
from rest_framework.permissions import SAFE_METHODS
from rest_framework.permissions import SAFE_METHODS, BasePermission


class OwnerUserOrReadOnly(permissions.BasePermission):
class IsAuthorOrReadOnly(BasePermission):

def has_object_permission(self, request, view, obj):
if request.method in SAFE_METHODS:
return True
def has_permission(self, request, view):
return (request.method in SAFE_METHODS
or request.user.is_authenticated)

return obj.user == request.user
def has_object_permission(self, request, view, obj):
return obj.author == request.user
8 changes: 4 additions & 4 deletions backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .filters import IngredientFilter, RecipeFilter
from recipes.models import (Favorite, Ingredient, IngredientAmount,
Recipe, ShoppingCart, Tag)
from .permissions import OwnerUserOrReadOnly
from .permissions import IsAuthorOrReadOnly

from .serializers import (
FavoriteSerializer,
Expand Down Expand Up @@ -56,7 +56,7 @@ def get_permissions(self):

permissions_dict = {
'create': [permissions.IsAuthenticated()],
'partial_update': [OwnerUserOrReadOnly],
'partial_update': [IsAuthorOrReadOnly()],
'favorite': [permissions.IsAuthenticated()],
'shopping_cart': [permissions.IsAuthenticated()],
'download_shopping_cart': [permissions.IsAuthenticated()],
Expand Down Expand Up @@ -134,7 +134,7 @@ def download_shopping_cart(self, request):
).values(
'ingredient__name',
'ingredient__measurement_unit'
).annotate(cart_amount=Sum('amount'))
).annotate(amount=Sum('amount'))

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

0 comments on commit 9a2380a

Please sign in to comment.