Skip to content

Commit

Permalink
Edit /api/shopping_cart/remove_all/ endpoint responses
Browse files Browse the repository at this point in the history
  • Loading branch information
earlinn committed Dec 26, 2023
1 parent 3d54830 commit dff0e03
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions backend/api/orders_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.conf import settings
from django.contrib.sites.shortcuts import get_current_site
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.utils.decorators import method_decorator
from drf_standardized_errors.openapi_serializers import (
ErrorResponse401Serializer,
Expand Down Expand Up @@ -47,6 +48,7 @@
STRIPE_SESSION_CREATE_ERROR_MESSAGE = (
"Что-то пошло не так при создании Stripe Checkout Session."
)
SHOP_CART_CLEAR_MESSAGE = "Ваша корзина очищена, все товары из нее удалены."


@method_decorator(
Expand Down Expand Up @@ -92,19 +94,30 @@ class ShoppingCartViewSet(
http_method_names = ("get", "post", "delete")
serializer_class = ShoppingCartSerializer

@action(detail=False, methods=["delete"],
permission_classes=[permissions.AllowAny])
@swagger_auto_schema(
method="delete",
operation_summary="Clear shopping cart",
operation_description=(
"Deletes a product from a user's favorites (authorized user only)"
),
responses={
200: '{"message": ' + SHOP_CART_CLEAR_MESSAGE + "}",
400: '{"errors": ' + SHOP_CART_ERROR + "}",
},
)
@transaction.atomic
@action(detail=False, methods=["delete"], permission_classes=[permissions.AllowAny])
def remove_all(self, request):
shopping_cart = ShopCart(request)
if not shopping_cart:
logger.error(SHOP_CART_ERROR)
return Response(
{"errors": SHOP_CART_ERROR},
status=status.HTTP_404_NOT_FOUND,
status=status.HTTP_400_BAD_REQUEST,
)
shopping_cart.clear()
logger.info(MESSAGE_ON_DELETE)
return Response(status=status.HTTP_200_OK)
logger.info(SHOP_CART_CLEAR_MESSAGE)
return Response({"message": SHOP_CART_CLEAR_MESSAGE}, status=status.HTTP_200_OK)

def list(self, request, **kwargs):
shopping_cart = ShopCart(request)
Expand Down

0 comments on commit dff0e03

Please sign in to comment.