Skip to content

Commit

Permalink
Merge pull request #215 from healthy-food-and-dietary-products/featur…
Browse files Browse the repository at this point in the history
…e/add_logging

add logging
  • Loading branch information
earlinn authored Dec 22, 2023
2 parents 98297d1 + a4c33e0 commit 65b7397
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions backend/api/orders_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class ShoppingCartViewSet(

def list(self, request, **kwargs):
shopping_cart = ShopCart(request)
logger.info("Success.")
return Response(
{
"products": shopping_cart.__iter__(),
Expand All @@ -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__(),
Expand All @@ -123,18 +125,21 @@ 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,
)
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__(),
Expand Down Expand Up @@ -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):
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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])
Expand Down

0 comments on commit 65b7397

Please sign in to comment.