This commit is contained in:
Mamalizz
2025-05-24 20:58:04 +03:30
2 changed files with 9 additions and 18 deletions
+1 -1
View File
@@ -23,7 +23,7 @@ class MainCategoryModel(models.Model):
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.name, allow_unicode=True)
self.slug = 'category-' + slugify(self.name, allow_unicode=True)
super().save(*args, **kwargs)
+8 -17
View File
@@ -95,12 +95,12 @@ class AllProductsView(APIView):
description="slug category (send it with category type)",
required=False,
),
OpenApiParameter(
name="category_type",
type=OpenApiTypes.STR,
required=False,
enum=['sub', 'main'],
),
# 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.",
@@ -161,22 +161,13 @@ class AllProductsView(APIView):
def get(self, request):
try:
category_slug = request.query_params.get('category')
category_type = request.query_params.get('category_type')
VALID_CATEGORY_TYPES = {'sub', 'main'}
products = ProductModel.objects.all()
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':
if 'category' not in category_slug:
sub_category = get_object_or_404(SubCategoryModel, slug=category_slug)
products = ProductModel.objects.filter(category=sub_category)
else: # category_type == 'main'
else:
main_category = get_object_or_404(MainCategoryModel, slug=category_slug)
sub_categories = main_category.subcategorys.all()
products = ProductModel.objects.filter(category__in=sub_categories)