Skip to content

Commit

Permalink
Реализовал механизм снятия или вывод товара в продажу
Browse files Browse the repository at this point in the history
  • Loading branch information
Abramov0Alexandr committed Sep 27, 2023
1 parent 20798fc commit 6982c7a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
9 changes: 9 additions & 0 deletions products/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = '__all__'


class ProductSaleStatusSerializers(serializers.ModelSerializer):
"""
Сериализатор используется в контроллере ChangeProductSaleStatus.
"""
class Meta:
model = Product
fields = ('is_active_sale', )
3 changes: 2 additions & 1 deletion products/urls.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from django.urls import path
from .apps import ProductsConfig
from .views import ProductCreateView, ProductDetailView, ProductListView, ProductDeleteView
from .views import ProductCreateView, ProductDetailView, ProductListView, ProductDeleteView, ChangeProductSaleStatus

app_name = ProductsConfig.name


urlpatterns = [
path('create/', ProductCreateView.as_view(), name='create-product'),
path('list/', ProductListView.as_view(), name='products-list'),
path('change_status/<int:pk>/', ChangeProductSaleStatus.as_view(), name='change-status'),
path('detail/<int:pk>/', ProductDetailView.as_view(), name='product-detail'),
path('delete/<int:pk>/', ProductDeleteView.as_view(), name='product-delete'),
]
47 changes: 45 additions & 2 deletions products/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from rest_framework import generics
from rest_framework import generics, status
from rest_framework.response import Response
from products.models import Product
from products.permissions import IsSeller, IsSuperUser, IsShopOwner
from products.serializers import ProductSerializer
from products.serializers import ProductSerializer, ProductSaleStatusSerializers


class ProductCreateView(generics.CreateAPIView):
Expand Down Expand Up @@ -37,6 +38,48 @@ def get_queryset(self):
return Product.objects.filter(seller=self.request.user)


class ChangeProductSaleStatus(generics.UpdateAPIView):
"""
Контроллер, отвечающий за снятие товара с продажи.
"""

queryset = Product.objects.all()
serializer_class = ProductSaleStatusSerializers
permission_classes = [IsShopOwner | IsSuperUser]

def get_object(self):
product = super().get_object()

if product.is_active_sale:
product.is_active_sale = False

else:
product.is_active_sale = True

product.save()
return product

def update(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=True)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)

if instance.is_active_sale:
sale_status_message = 'выведен в продажу'
else:
sale_status_message = 'снят с продажи'

response_message = {
"Product info": {'sale status': f'{instance.is_active_sale}',
'message': f'{instance.product_title} {sale_status_message}',
'status': status.HTTP_200_OK
}
}

return Response(response_message)


class ProductDetailView(generics.RetrieveAPIView):
"""
Контроллер для просмотра детальной информации о товаре.
Expand Down

0 comments on commit 6982c7a

Please sign in to comment.