diff --git a/products/serializers.py b/products/serializers.py index d4464c8..c91b97b 100644 --- a/products/serializers.py +++ b/products/serializers.py @@ -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', ) diff --git a/products/urls.py b/products/urls.py index 0a465f1..2e5dd99 100644 --- a/products/urls.py +++ b/products/urls.py @@ -1,6 +1,6 @@ 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 @@ -8,6 +8,7 @@ urlpatterns = [ path('create/', ProductCreateView.as_view(), name='create-product'), path('list/', ProductListView.as_view(), name='products-list'), + path('change_status//', ChangeProductSaleStatus.as_view(), name='change-status'), path('detail//', ProductDetailView.as_view(), name='product-detail'), path('delete//', ProductDeleteView.as_view(), name='product-delete'), ] diff --git a/products/views.py b/products/views.py index dcf63c2..de50c0e 100644 --- a/products/views.py +++ b/products/views.py @@ -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): @@ -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): """ Контроллер для просмотра детальной информации о товаре.