diff --git a/core/apps/api/urls.py b/core/apps/api/urls.py index 6b04a93..4c0a9fa 100644 --- a/core/apps/api/urls.py +++ b/core/apps/api/urls.py @@ -1,10 +1,11 @@ from django.urls import include, path from rest_framework.routers import DefaultRouter -from core.apps.api.views import CategoryViewSet, SearchHistoryViewSet, HomeAdApiView +from core.apps.api.views import CategoryViewSet, SearchHistoryViewSet, HomeAdApiView, CategoryHomeApiViewSet router = DefaultRouter() router.register("category", CategoryViewSet, basename="category") +router.register("category-home-api", CategoryHomeApiViewSet, basename="category-home-api") router.register("search-history", SearchHistoryViewSet, basename="search-history") router.register("home-ad", HomeAdApiView, basename="home-ad") urlpatterns = [path("", include(router.urls))] diff --git a/core/apps/api/views/category/category.py b/core/apps/api/views/category/category.py index ea08a04..f3596cd 100644 --- a/core/apps/api/views/category/category.py +++ b/core/apps/api/views/category/category.py @@ -15,6 +15,7 @@ from core.apps.api.serializers.category import ( @extend_schema(tags=["Category"]) class CategoryViewSet(BaseViewSetMixin, ReadOnlyModelViewSet): + queryset = Category.objects.filter(level=0) permission_classes = [AllowAny] serializer_class = ListCategorySerializer pagination_class = None @@ -28,18 +29,19 @@ class CategoryViewSet(BaseViewSetMixin, ReadOnlyModelViewSet): "create": CreateCategorySerializer, } - def get_queryset(self): - qs = Category.objects.all() - if not self.request.query_params: - qs = qs.filter(level=0) - return qs +@extend_schema(tags=["Category"]) +class CategoryHomeApiViewSet(BaseViewSetMixin, ReadOnlyModelViewSet): + queryset = Category.objects.all() + permission_classes = [AllowAny] + serializer_class = ListCategoryNoChildSerializer + pagination_class = None + filter_backends = [DjangoFilterBackend] + filterset_class = CategoryFilter - def get_serializer_class(self): - if "show_home" in self.request.query_params: - return ListCategoryNoChildSerializer - - if hasattr(self, 'action_serializer_class'): - return self.action_serializer_class.get(self.action, self.serializer_class) - - return super().get_serializer_class() + action_permission_classes = {} + action_serializer_class = { + "list": ListCategoryNoChildSerializer, + "retrieve": RetrieveCategorySerializer, + "create": CreateCategorySerializer, + }