118 lines
4.3 KiB
Python
118 lines
4.3 KiB
Python
"""Product app back-office views."""
|
|
from django.conf import settings
|
|
from django.db.transaction import on_commit
|
|
from django.shortcuts import get_object_or_404
|
|
from rest_framework import generics, status, permissions, views
|
|
from rest_framework.response import Response
|
|
|
|
from gallery.tasks import delete_image
|
|
from product import serializers, models
|
|
from utils.views import CreateDestroyGalleryViewMixin
|
|
|
|
|
|
class ProductBackOfficeMixinView:
|
|
"""Product back-office mixin view."""
|
|
|
|
permission_classes = (permissions.IsAuthenticated,)
|
|
queryset = models.Product.objects.with_base_related() \
|
|
.order_by('-created', )
|
|
|
|
|
|
class ProductTypeBackOfficeMixinView:
|
|
"""Product type back-office mixin view."""
|
|
|
|
permission_classes = (permissions.IsAuthenticated,)
|
|
queryset = models.ProductType.objects.all()
|
|
|
|
|
|
class ProductSubTypeBackOfficeMixinView:
|
|
"""Product sub type back-office mixin view."""
|
|
|
|
permission_classes = (permissions.IsAuthenticated,)
|
|
queryset = models.ProductSubType.objects.all()
|
|
|
|
|
|
class BackOfficeListCreateMixin(views.APIView):
|
|
"""Back-office list-create mixin view."""
|
|
|
|
def check_permissions(self, request):
|
|
"""
|
|
Check if the request should be permitted.
|
|
Raises an appropriate exception if the request is not permitted.
|
|
"""
|
|
if self.request.method != 'GET':
|
|
super().check_permissions(request)
|
|
|
|
|
|
class ProductBackOfficeGalleryCreateDestroyView(ProductBackOfficeMixinView,
|
|
CreateDestroyGalleryViewMixin):
|
|
"""Resource for a create gallery for product for back-office users."""
|
|
serializer_class = serializers.ProductBackOfficeGallerySerializer
|
|
|
|
def get_object(self):
|
|
"""
|
|
Returns the object the view is displaying.
|
|
"""
|
|
product_qs = self.filter_queryset(self.get_queryset())
|
|
|
|
product = get_object_or_404(product_qs, pk=self.kwargs['pk'])
|
|
gallery = get_object_or_404(product.product_gallery, image_id=self.kwargs['image_id'])
|
|
|
|
# May raise a permission denied
|
|
self.check_object_permissions(self.request, gallery)
|
|
|
|
return gallery
|
|
|
|
|
|
class ProductBackOfficeGalleryListView(ProductBackOfficeMixinView, generics.ListAPIView):
|
|
"""Resource for returning gallery for product for back-office users."""
|
|
serializer_class = serializers.ProductImageSerializer
|
|
|
|
def get_object(self):
|
|
"""Override get_object method."""
|
|
qs = super(ProductBackOfficeGalleryListView, self).get_queryset()
|
|
product = get_object_or_404(qs, pk=self.kwargs['pk'])
|
|
|
|
# May raise a permission denied
|
|
self.check_object_permissions(self.request, product)
|
|
|
|
return product
|
|
|
|
def get_queryset(self):
|
|
"""Override get_queryset method."""
|
|
return self.get_object().gallery.all()
|
|
|
|
|
|
class ProductDetailBackOfficeView(ProductBackOfficeMixinView, generics.RetrieveUpdateDestroyAPIView):
|
|
"""Product back-office R/U/D view."""
|
|
serializer_class = serializers.ProductBackOfficeDetailSerializer
|
|
|
|
|
|
class ProductListCreateBackOfficeView(BackOfficeListCreateMixin, ProductBackOfficeMixinView,
|
|
generics.ListCreateAPIView):
|
|
"""Product back-office list-create view."""
|
|
serializer_class = serializers.ProductBackOfficeDetailSerializer
|
|
|
|
|
|
class ProductTypeListCreateBackOfficeView(BackOfficeListCreateMixin, ProductTypeBackOfficeMixinView,
|
|
generics.ListCreateAPIView):
|
|
"""Product type back-office list-create view."""
|
|
serializer_class = serializers.ProductTypeBackOfficeDetailSerializer
|
|
|
|
|
|
class ProductTypeRUDBackOfficeView(BackOfficeListCreateMixin, ProductTypeBackOfficeMixinView,
|
|
generics.RetrieveUpdateDestroyAPIView):
|
|
"""Product type back-office retrieve-update-destroy view."""
|
|
serializer_class = serializers.ProductTypeBackOfficeDetailSerializer
|
|
|
|
|
|
class ProductTypeTagCategoryCreateBackOfficeView(ProductTypeBackOfficeMixinView,
|
|
generics.CreateAPIView):
|
|
"""View for attaching tag category to product type."""
|
|
serializer_class = serializers.ProductTypeTagCategorySerializer
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
super().create(request, *args, **kwargs)
|
|
return Response(status=status.HTTP_201_CREATED)
|
|
|