category update
This commit is contained in:
@@ -94,6 +94,12 @@ class AllProductsView(APIView):
|
||||
type=OpenApiTypes.INT,
|
||||
required=False,
|
||||
),
|
||||
OpenApiParameter(
|
||||
name="category_type",
|
||||
type=OpenApiTypes.STR,
|
||||
required=False,
|
||||
enum=['sub', 'main'],
|
||||
),
|
||||
OpenApiParameter(
|
||||
name="price_gte",
|
||||
description="Filter products with price greater than or equal to this value.",
|
||||
@@ -153,15 +159,25 @@ class AllProductsView(APIView):
|
||||
)
|
||||
def get(self, request):
|
||||
try:
|
||||
category_id = request.query_params.get('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)
|
||||
category_slug = request.query_params.get('category')
|
||||
category_type = request.query_params.get('category_type')
|
||||
|
||||
sub_category = get_object_or_404(SubCategoryModel, pk=category_id)
|
||||
products = ProductModel.objects.filter(category=sub_category)
|
||||
VALID_CATEGORY_TYPES = {'sub', 'main'}
|
||||
|
||||
if category_slug:
|
||||
if category_type not in VALID_CATEGORY_TYPES:
|
||||
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)
|
||||
products = ProductModel.objects.filter(category=sub_category)
|
||||
else: # category_type == 'main'
|
||||
main_category = get_object_or_404(MainCategoryModel, slug=category_slug)
|
||||
sub_categories = main_category.subcategorys.all()
|
||||
products = ProductModel.objects.filter(category__in=sub_categories)
|
||||
else:
|
||||
products = ProductModel.objects.all()
|
||||
|
||||
@@ -204,8 +220,10 @@ class AllProductsView(APIView):
|
||||
return paginator.get_paginated_response(serializer.data)
|
||||
|
||||
except MainCategoryModel.DoesNotExist:
|
||||
return Response({"detail": "Category not found."}, status=status.HTTP_404_NOT_FOUND)
|
||||
return Response({"detail": "Main Category not found."}, status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
except SubCategoryModel.DoesNotExist:
|
||||
return Response({"detail": "Sub Category not found."}, status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
class ShowCaseCategoryListView(APIView):
|
||||
serializer_class = ShowCaseSliderSerialzier
|
||||
|
||||
Reference in New Issue
Block a user