new route
This commit is contained in:
@@ -13,6 +13,7 @@ from rest_framework.permissions import AllowAny
|
||||
from order.serializers import OrderItemSerailzier
|
||||
from order.models import OrderModel
|
||||
from django.db.models import Min, Max
|
||||
from home.models import ShowCaseSlider
|
||||
# class APIView(APIView):
|
||||
# def __init__(self, *args, **kwargs):
|
||||
# super().__init__(*args, **kwargs)
|
||||
@@ -205,6 +206,143 @@ class AllProductsView(APIView):
|
||||
return Response({"detail": "Category not found."}, status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
|
||||
|
||||
class ShowCaseProductsView(APIView):
|
||||
serializer_class = DynamicProductSerializer
|
||||
pagination_class = StructurePagination
|
||||
authentication_classes = []
|
||||
@extend_schema(
|
||||
parameters=[
|
||||
OpenApiParameter(
|
||||
name="search",
|
||||
description="Search by product name or description.",
|
||||
required=False,
|
||||
type=OpenApiTypes.STR,
|
||||
),
|
||||
OpenApiParameter(
|
||||
name="slider_category",
|
||||
type=OpenApiTypes.INT,
|
||||
required=False,
|
||||
),
|
||||
OpenApiParameter(
|
||||
name="price_gte",
|
||||
description="Filter products with price greater than or equal to this value.",
|
||||
required=False,
|
||||
type=OpenApiTypes.FLOAT,
|
||||
),
|
||||
OpenApiParameter(
|
||||
name="price_lte",
|
||||
description="Filter products with price less than or equal to this value.",
|
||||
required=False,
|
||||
type=OpenApiTypes.FLOAT,
|
||||
),
|
||||
OpenApiParameter(
|
||||
name="sort",
|
||||
description=(
|
||||
"Sort results by one of the following fields:\n"
|
||||
"`name`, `-name`, `price`, `-price`, `created_at`, `-created_at`."
|
||||
"\nPrefix with `-` for descending order."
|
||||
"remove the price form sorting templory "
|
||||
),
|
||||
required=False,
|
||||
type=OpenApiTypes.STR,
|
||||
),
|
||||
OpenApiParameter(
|
||||
name="limit",
|
||||
description="Number of results to return per page (pagination).",
|
||||
required=False,
|
||||
type=OpenApiTypes.INT,
|
||||
),
|
||||
OpenApiParameter(
|
||||
name="offset",
|
||||
description="The starting position of the results (pagination).",
|
||||
required=False,
|
||||
type=OpenApiTypes.INT,
|
||||
),
|
||||
OpenApiParameter(
|
||||
name="in_stock",
|
||||
description="Filter products that are in stock (positive stock).",
|
||||
required=False,
|
||||
type=OpenApiTypes.BOOL,
|
||||
),
|
||||
OpenApiParameter(
|
||||
name="has_discount",
|
||||
description="Filter products that have a discount.",
|
||||
required=False,
|
||||
type=OpenApiTypes.BOOL,
|
||||
)
|
||||
],
|
||||
description=(
|
||||
"Retrieve products with optional filters and sorting. "
|
||||
"Provide a list of category IDs to filter products by those categories and their subcategories."
|
||||
),
|
||||
responses={
|
||||
200: DynamicProductSerializer(many=True, context={'view_type': 'list'}),
|
||||
404: OpenApiTypes.OBJECT,
|
||||
},
|
||||
)
|
||||
def get(self, request):
|
||||
try:
|
||||
category_id = request.query_params.get('slider_category', None)
|
||||
if category_id:
|
||||
try:
|
||||
category_id = int(category_id)
|
||||
except ValueError:
|
||||
return Response({'detail': 'value error category id should be a number'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
slider_category = get_object_or_404(ShowCaseSlider, pk=category_id)
|
||||
|
||||
products = ProductModel.objects.filter(variants__slider_category=slider_category).distinct()
|
||||
else:
|
||||
products = ProductModel.objects.filter(variants__slider_category__isnull=False).distinct()
|
||||
|
||||
# Filter by stock status if `in_stock` is specified
|
||||
in_stock = request.query_params.get('in_stock', "false") == 'true'
|
||||
if in_stock:
|
||||
products = products.filter(variants__in_stock__gt=0)
|
||||
|
||||
# Filter by discount if `has_discount` is specified
|
||||
has_discount = request.query_params.get('has_discount', "false") == 'true'
|
||||
if has_discount:
|
||||
products = products.filter(variants__discount__gt=0)
|
||||
|
||||
# Search filter
|
||||
search_query = request.query_params.get('search', None)
|
||||
if search_query:
|
||||
products = products.filter(Q(name__icontains=search_query) | Q(description__icontains=search_query))
|
||||
|
||||
# Price filters
|
||||
price_gte = request.query_params.get('price_gte', None)
|
||||
price_lte = request.query_params.get('price_lte', None)
|
||||
|
||||
products = products.annotate(min_price=Min('variants__price'), max_price=Max('variants__price'))
|
||||
|
||||
if price_gte:
|
||||
products = products.filter(max_price__gte=price_gte)
|
||||
if price_lte:
|
||||
products = products.filter(min_price__lte=price_lte)
|
||||
# Sorting
|
||||
sort_by = request.query_params.get('sort', None)
|
||||
if sort_by in ['name', '-name', 'created_at', '-created_at']:
|
||||
products = products.order_by(sort_by)
|
||||
else:
|
||||
products = products.order_by('name')
|
||||
|
||||
# Pagination
|
||||
paginator = self.pagination_class()
|
||||
paginated_products = paginator.paginate_queryset(products, request)
|
||||
serializer = self.serializer_class(paginated_products, many=True, context={'request': request, 'view_type': 'slider'})
|
||||
return paginator.get_paginated_response(serializer.data)
|
||||
|
||||
except MainCategoryModel.DoesNotExist:
|
||||
return Response({"detail": "Category not found."}, status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class CommentView(APIView):
|
||||
serializer_class = CommentSerializer
|
||||
permission_classes = [IsAuthenticatedOrReadOnly]
|
||||
|
||||
Reference in New Issue
Block a user