From 3d5483019bb261babf1b88815b8285b83f796c86 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: Tue, 26 Dec 2023 10:11:08 +0300 Subject: [PATCH 1/5] add remove_all method in ShoppingCartViewSet --- backend/api/orders_views.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/backend/api/orders_views.py b/backend/api/orders_views.py index 4d1f73a..9593182 100644 --- a/backend/api/orders_views.py +++ b/backend/api/orders_views.py @@ -92,6 +92,20 @@ class ShoppingCartViewSet( http_method_names = ("get", "post", "delete") serializer_class = ShoppingCartSerializer + @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, + ) + shopping_cart.clear() + logger.info(MESSAGE_ON_DELETE) + return Response(status=status.HTTP_200_OK) + def list(self, request, **kwargs): shopping_cart = ShopCart(request) logger.info("The user's shopping cart list was successfully received.") From dff0e036237121bf75176ff7ae6ab47c7477b4d3 Mon Sep 17 00:00:00 2001 From: earlinn Date: Tue, 26 Dec 2023 13:59:19 +0600 Subject: [PATCH 2/5] Edit /api/shopping_cart/remove_all/ endpoint responses --- backend/api/orders_views.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/backend/api/orders_views.py b/backend/api/orders_views.py index 9593182..fff2a48 100644 --- a/backend/api/orders_views.py +++ b/backend/api/orders_views.py @@ -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, @@ -47,6 +48,7 @@ STRIPE_SESSION_CREATE_ERROR_MESSAGE = ( "Что-то пошло не так при создании Stripe Checkout Session." ) +SHOP_CART_CLEAR_MESSAGE = "Ваша корзина очищена, все товары из нее удалены." @method_decorator( @@ -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) From 60eb35622396e129f90a23dbb074545ccaea7bb1 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: Tue, 26 Dec 2023 11:43:49 +0300 Subject: [PATCH 3/5] fix README.md --- README.md | 135 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 89 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 2e59674..21a55c1 100644 --- a/README.md +++ b/README.md @@ -533,26 +533,38 @@ id этой записи, а не id пользователя или проду ``` { "products": [ - {"id": 1, "quantity": 9}, - {"id": 3, "quantity": 10} + {"id": 12, "quantity": 2}, + {"id": 1, "quantity": 2} ] } ``` Пример ответа: ``` { - "products": [ - { - "id": 10, - "name": "Помидоры черри", - "quantity": 20, - "final_price": 150, - "created_at": 1701761819, - "total_price": 3000 - } - ], - "count_of_products": 20, - "total_price": 3000 + "products": [ + { + "id": 12, + "name": "Перец болгарский", + "photo": "images/products/12.jpg", + "category": "vegetables-and-herbs", + "quantity": 2, + "final_price": 120.0, + "created_at": 1703501460, + "total_price": 240.0 + }, + { + "id": 1, + "name": "Миндаль", + "photo": "images/products/1.jpg", + "category": "nuts-dried-fruits", + "quantity": 2, + "final_price": 160.0, + "created_at": 1703501460, + "total_price": 320.0 + } + ], + "count_of_products": 4, + "total_price": 560.0 } ``` @@ -562,6 +574,12 @@ id этой записи, а не id пользователя или проду Для удаления продукта из корзины покупок необходимо передать в URL id продукта во время DELETE-запроса на указанный эндпойнт. +### Удаление всех продуктов из корзины покупок + +Удаление корзины покупок доступно по эндпойнту /api/shopping_cart/remove_all/ +Для удаления корзины покупок происходит во время DELETE-запроса +на указанный эндпойнт. + ## Заказ ### Создание заказа @@ -603,36 +621,57 @@ payment_method, delivery_method(delivery_point или address_anonimous), packag Пример ответа: ``` { - "id": 32, - "order_number": "32", - "user_data": "{'first_name': 'Vasya', - 'last_name': 'Kovin', - 'phone_number': '89764563456', - 'email': 'user@example.com'}", + "id": 3, + "order_number": "3", + "user_data": { + "first_name": "Vasya", + "last_name": "Kovin", + "phone_number": "89764563456", + "email": "user@example.ruu" + }, "products": [ { - "product": { - "id": 12, - "name": "Перец болгарский", - "measure_unit": "grams", - "amount": 1000, - "final_price": 120, - "photo": "/media/images/products/12.jpg" + "product": { + "id": 1, + "name": "Миндаль", + "measure_unit": "г.", + "amount": 100, + "final_price": 160.0, + "photo": null, + "category": { + "category_name": "Орехи и сухофрукты", + "category_slug": "nuts-dried-fruits" + } + }, + "quantity": 2 }, - "quantity": 10 + { + "product": { + "id": 12, + "name": "Перец болгарский", + "measure_unit": "г.", + "amount": 1000, + "final_price": 120.0, + "photo": null, + "category": { + "category_name": "Овощи", + "category_slug": "vegetables" + } + }, + "quantity": 2 } ], - "payment_method": "Payment at the point of delivery", - "delivery_method": "Point of delivery", - "delivery_point": 2, + "payment_method": "In getting by cash", + "delivery_method": "By courier", + "add_address": "Санкт-Петербург, улица Горохова, д.5, кв. 11", + "delivery_point": null, "package": 0.0, "comment": "string", - "total_price": 300.0, + "total_price": 280.0, "is_paid": false, "status": "Ordered", - "ordering_date": "2023-12-01T12:32:18.152993+03:00" + "ordering_date": "2023-12-26T11:38:55.422583+03:00" } - ``` Пример POST запроса на создание заказа авторизированного пользователя: @@ -640,7 +679,7 @@ payment_method, delivery_method(delivery_point или address_anonimous), packag { "payment_method": "In getting by cash", "delivery_method": "By courier", - "package": 100б + "package": 100, "add_address": "Санкт-Петербург, Невский прспект д.18, оф. 3 } ``` @@ -657,16 +696,20 @@ payment_method, delivery_method(delivery_point или address_anonimous), packag }, "products": [ { - "id": 4, - "name": "Молоко коровье 1.5%", - "measure_unit": "мл.", - "amount": 1000, - "final_price": 75.0, - "photo": "/media/images/products/4.jpg" + "product": { + "id": 1, + "name": "Миндаль", + "measure_unit": "г.", + "amount": 100, + "final_price": 160.0, + "photo": null, + "category": { + "category_name": "Орехи и сухофрукты", + "category_slug": "nuts-dried-fruits" + } + }, + "quantity": 2 }, - "quantity": 10 - - } ], "payment_method": "In getting by cash", "delivery_method": "By courier", @@ -675,7 +718,7 @@ payment_method, delivery_method(delivery_point или address_anonimous), packag "delivery_point": null, "package": 100.0, "comment": null, - "total_price": 250.0, + "total_price": 160.0, "is_paid": false, "status": "Ordered", "ordering_date": "2023-12-04T10:52:20.324335+03:00" @@ -696,5 +739,5 @@ payment_method, delivery_method(delivery_point или address_anonimous), packag на указанный эндпойнт. Отмена заказа возможна только если статус заказа: - Оформлен, - В обработке, -- Комплектуется +- Комплектуется Доступно авторизированному пользователю \ No newline at end of file From f2682ef072cdd1f3dfda8db5730f0bf6bb929c5b 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: Tue, 26 Dec 2023 11:46:36 +0300 Subject: [PATCH 4/5] fix README.md --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 21a55c1..293667e 100644 --- a/README.md +++ b/README.md @@ -697,18 +697,18 @@ payment_method, delivery_method(delivery_point или address_anonimous), packag "products": [ { "product": { - "id": 1, - "name": "Миндаль", - "measure_unit": "г.", - "amount": 100, - "final_price": 160.0, - "photo": null, - "category": { + "id": 1, + "name": "Миндаль", + "measure_unit": "г.", + "amount": 100, + "final_price": 160.0, + "photo": null, + "category": { "category_name": "Орехи и сухофрукты", "category_slug": "nuts-dried-fruits" - } + } }, - "quantity": 2 + "quantity": 2 }, ], "payment_method": "In getting by cash", From 505eb4321a371597f5d00c83bd8b51b2f88c1434 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: Tue, 26 Dec 2023 11:48:48 +0300 Subject: [PATCH 5/5] fix README.md --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 293667e..cd9ba4d 100644 --- a/README.md +++ b/README.md @@ -734,10 +734,9 @@ payment_method, delivery_method(delivery_point или address_anonimous), packag ### Удаление заказа -Удаление заказа доступно по эндпойнту /api/order/{order_id}/ +Удаление заказа доступно авторизированному пользователю по эндпойнту /api/order/{order_id}/ Для удаления заказа необходимо передать в URL id заказа во время DELETE-запроса на указанный эндпойнт. Отмена заказа возможна только если статус заказа: - Оформлен, - В обработке, -- Комплектуется -Доступно авторизированному пользователю \ No newline at end of file +- Комплектуется \ No newline at end of file