remove categort type
This commit is contained in:
@@ -95,12 +95,12 @@ class AllProductsView(APIView):
|
|||||||
description="slug category (send it with category type)",
|
description="slug category (send it with category type)",
|
||||||
required=False,
|
required=False,
|
||||||
),
|
),
|
||||||
OpenApiParameter(
|
# OpenApiParameter(
|
||||||
name="category_type",
|
# name="category_type",
|
||||||
type=OpenApiTypes.STR,
|
# type=OpenApiTypes.STR,
|
||||||
required=False,
|
# required=False,
|
||||||
enum=['sub', 'main'],
|
# enum=['sub', 'main'],
|
||||||
),
|
# ),
|
||||||
OpenApiParameter(
|
OpenApiParameter(
|
||||||
name="price_gte",
|
name="price_gte",
|
||||||
description="Filter products with price greater than or equal to this value.",
|
description="Filter products with price greater than or equal to this value.",
|
||||||
@@ -161,22 +161,13 @@ class AllProductsView(APIView):
|
|||||||
def get(self, request):
|
def get(self, request):
|
||||||
try:
|
try:
|
||||||
category_slug = request.query_params.get('category')
|
category_slug = request.query_params.get('category')
|
||||||
category_type = request.query_params.get('category_type')
|
|
||||||
|
|
||||||
VALID_CATEGORY_TYPES = {'sub', 'main'}
|
|
||||||
products = ProductModel.objects.all()
|
products = ProductModel.objects.all()
|
||||||
|
|
||||||
if category_slug:
|
if category_slug:
|
||||||
if category_type not in VALID_CATEGORY_TYPES:
|
if 'category' in category_slug:
|
||||||
return Response(
|
|
||||||
{"detail": "category_type must be one of ['sub', 'main']"},
|
|
||||||
status=status.HTTP_400_BAD_REQUEST
|
|
||||||
)
|
|
||||||
|
|
||||||
if category_type == 'sub':
|
|
||||||
sub_category = get_object_or_404(SubCategoryModel, slug=category_slug)
|
sub_category = get_object_or_404(SubCategoryModel, slug=category_slug)
|
||||||
products = ProductModel.objects.filter(category=sub_category)
|
products = ProductModel.objects.filter(category=sub_category)
|
||||||
else: # category_type == 'main'
|
else:
|
||||||
main_category = get_object_or_404(MainCategoryModel, slug=category_slug)
|
main_category = get_object_or_404(MainCategoryModel, slug=category_slug)
|
||||||
sub_categories = main_category.subcategorys.all()
|
sub_categories = main_category.subcategorys.all()
|
||||||
products = ProductModel.objects.filter(category__in=sub_categories)
|
products = ProductModel.objects.filter(category__in=sub_categories)
|
||||||
|
|||||||
Reference in New Issue
Block a user