Skip to content

Commit

Permalink
Merge branch 'develop' into feature/orders_list_optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
earlinn committed Dec 26, 2023
2 parents 96c4492 + caf9595 commit b4b4797
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 48 deletions.
138 changes: 90 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

Expand All @@ -562,6 +574,12 @@ id этой записи, а не id пользователя или проду
Для удаления продукта из корзины покупок необходимо передать в URL id продукта во время DELETE-запроса
на указанный эндпойнт.

### Удаление всех продуктов из корзины покупок

Удаление корзины покупок доступно по эндпойнту /api/shopping_cart/remove_all/
Для удаления корзины покупок происходит во время DELETE-запроса
на указанный эндпойнт.

## Заказ

### Создание заказа
Expand Down Expand Up @@ -603,44 +621,65 @@ 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 запроса на создание заказа авторизированного пользователя:

```
{
"payment_method": "In getting by cash",
"delivery_method": "By courier",
"package": 100б
"package": 100,
"add_address": "Санкт-Петербург, Невский прспект д.18, оф. 3
}
```
Expand All @@ -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",
Expand All @@ -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"
Expand All @@ -691,10 +734,9 @@ payment_method, delivery_method(delivery_point или address_anonimous), packag

### Удаление заказа

Удаление заказа доступно по эндпойнту /api/order/{order_id}/
Удаление заказа доступно авторизированному пользователю по эндпойнту /api/order/{order_id}/
Для удаления заказа необходимо передать в URL id заказа во время DELETE-запроса
на указанный эндпойнт. Отмена заказа возможна только если статус заказа:
- Оформлен,
- В обработке,
- Комплектуется
Доступно авторизированному пользователю
- Комплектуется
27 changes: 27 additions & 0 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,6 +94,31 @@ class ShoppingCartViewSet(
http_method_names = ("get", "post", "delete")
serializer_class = ShoppingCartSerializer

@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_400_BAD_REQUEST,
)
shopping_cart.clear()
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)
logger.info("The user's shopping cart list was successfully received.")
Expand Down

0 comments on commit b4b4797

Please sign in to comment.