From a4c33e0a0fc1685cd7bfde27c8e41693f5049ba8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D0=B8=D0=B0=D0=BD=D0=B0=20=D0=A1=D1=82=D1=80?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2=D0=B0?= Date: Fri, 22 Dec 2023 09:58:06 +0300 Subject: [PATCH] add logging --- backend/api/orders_views.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/backend/api/orders_views.py b/backend/api/orders_views.py index cdaba4e..a033ca6 100644 --- a/backend/api/orders_views.py +++ b/backend/api/orders_views.py @@ -95,6 +95,7 @@ class ShoppingCartViewSet( def list(self, request, **kwargs): shopping_cart = ShopCart(request) + logger.info("Success.") return Response( { "products": shopping_cart.__iter__(), @@ -111,6 +112,7 @@ def create(self, request, **kwargs): serializer.is_valid(raise_exception=True) for product in products: shopping_cart.add(product=product, quantity=product["quantity"]) + logger.info("Success.") return Response( { "products": shopping_cart.__iter__(), @@ -123,6 +125,7 @@ def create(self, request, **kwargs): def destroy(self, *args, **kwargs): shopping_cart = ShopCart(self.request) if not shopping_cart: + logger.error(NO_SHOP_CART_ERROR_MESSAGE) return Response( {"errors": NO_SHOP_CART_ERROR_MESSAGE}, status=status.HTTP_404_NOT_FOUND, @@ -130,11 +133,13 @@ def destroy(self, *args, **kwargs): product_id = int(self.kwargs["pk"]) products = [product["id"] for product in shopping_cart.get_shop_products()] if product_id not in products: + logger.error(SHOP_CART_ERROR_MESSAGE) return Response( {"errors": SHOP_CART_ERROR_MESSAGE}, status=status.HTTP_404_NOT_FOUND, ) shopping_cart.remove(product_id) + logger.info(MESSAGE_ON_DELETE) return Response( { "products": shopping_cart.__iter__(), @@ -263,6 +268,7 @@ def retrieve(self, request, **kwargs): if user.is_authenticated and order.user != user: return Response({"errors": ORDER_USER_ERROR_MESSAGE}) serializer = self.get_serializer(order) + logger.info("Success.") return Response(serializer.data, status=status.HTTP_200_OK) def list(self, request, **kwargs): @@ -278,7 +284,9 @@ def list(self, request, **kwargs): .filter(user=self.request.user) ) serializer = self.get_serializer(queryset, many=True) + logger.info("Success.") return Response(serializer.data, status=status.HTTP_200_OK) + logger.error(METHOD_ERROR_MESSAGE) return Response( {"errors": METHOD_ERROR_MESSAGE}, status=status.HTTP_401_UNAUTHORIZED, @@ -287,6 +295,7 @@ def list(self, request, **kwargs): def create(self, request, *args, **kwargs): 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, @@ -331,6 +340,7 @@ def create(self, request, *args, **kwargs): else OrderGetAnonSerializer ) response_serializer = response_serializer(order) + logger.info("Order was successful.") return Response(response_serializer.data, status=status.HTTP_201_CREATED) def destroy(self, request, *args, **kwargs): @@ -346,9 +356,11 @@ def destroy(self, request, *args, **kwargs): else: order = get_object_or_404(Order, id=self.kwargs.get("pk")) if order.user != self.request.user: + logger.error("PermissionDenied.") raise PermissionDenied() if order.status in order_restricted_deletion_statuses: + logger.error(DELIVERY_ERROR_MESSAGE) return Response({"errors": DELIVERY_ERROR_MESSAGE}) response_serializer = ( OrderGetAuthSerializer @@ -358,6 +370,7 @@ def destroy(self, request, *args, **kwargs): serializer_data = response_serializer(order).data serializer_data["Success"] = MESSAGE_ON_DELETE order.delete() + logger.info(MESSAGE_ON_DELETE) return Response(serializer_data, status=status.HTTP_200_OK) @action(methods=["POST"], detail=True, permission_classes=[permissions.AllowAny])