From 7c613f27b99635a7a6d209741d2aebd90b2765de Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Thu, 31 Jul 2025 16:00:37 +0330 Subject: [PATCH] fix search mixin bug --- apps/core/mixins/search_mixin.py | 5 +++-- apps/product/web/api/v1/viewsets/quota_api.py | 6 +++--- .../product/web/api/v1/viewsets/quota_distribution_api.py | 2 +- apps/warehouse/web/api/v1/api.py | 8 ++++---- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/apps/core/mixins/search_mixin.py b/apps/core/mixins/search_mixin.py index 60ec2e7..57dcb12 100644 --- a/apps/core/mixins/search_mixin.py +++ b/apps/core/mixins/search_mixin.py @@ -1,4 +1,5 @@ from apps.core.services.filter.search import DynamicSearchService +from rest_framework.exceptions import APIException class DynamicSearchMixin: @@ -10,8 +11,8 @@ class DynamicSearchMixin: def get_date_field(self): return getattr(self, "date_field", "create_date") - def filter_queryset(self, queryset): - queryset = super().filter_queryset(queryset) # noqa + def filter_query(self, queryset): + queryset = queryset # noqa q = self.request.query_params.get("search") # noqa start = self.request.query_params.get("start") # noqa diff --git a/apps/product/web/api/v1/viewsets/quota_api.py b/apps/product/web/api/v1/viewsets/quota_api.py index 55990e4..07518a5 100644 --- a/apps/product/web/api/v1/viewsets/quota_api.py +++ b/apps/product/web/api/v1/viewsets/quota_api.py @@ -310,7 +310,7 @@ class QuotaViewSet(viewsets.ModelViewSet, DynamicSearchMixin): # noqa def active_quotas(self, request): """ list of organization active quotas """ - queryset = self.filter_queryset(self.queryset) # return by search param or all objects + queryset = self.filter_query(self.queryset) # return by search param or all objects organization = get_organization_by_user(request.user) @@ -336,7 +336,7 @@ class QuotaViewSet(viewsets.ModelViewSet, DynamicSearchMixin): # noqa def closed_quotas(self, request): """ list of organization closed quotas """ - queryset = self.filter_queryset(self.queryset) # return by search param or all objects + queryset = self.filter_query(self.queryset) # return by search param or all objects organization = get_organization_by_user(request.user) @@ -363,7 +363,7 @@ class QuotaViewSet(viewsets.ModelViewSet, DynamicSearchMixin): # noqa try: quota = self.get_object() - queryset = self.filter_queryset( + queryset = self.filter_query( quota.distributions_assigned.all().order_by('-modify_date') ) # return by search param or all objects diff --git a/apps/product/web/api/v1/viewsets/quota_distribution_api.py b/apps/product/web/api/v1/viewsets/quota_distribution_api.py index 379d07e..15a0197 100644 --- a/apps/product/web/api/v1/viewsets/quota_distribution_api.py +++ b/apps/product/web/api/v1/viewsets/quota_distribution_api.py @@ -91,7 +91,7 @@ class QuotaDistributionViewSet(viewsets.ModelViewSet, DynamicSearchMixin): def my_distributions(self, request): """ list of my distributions """ - queryset = self.filter_queryset(self.queryset) # return by search param or all objects + queryset = self.filter_query(self.queryset) # return by search param or all objects organization = get_organization_by_user(request.user) query = self.request.query_params diff --git a/apps/warehouse/web/api/v1/api.py b/apps/warehouse/web/api/v1/api.py index 79d253c..8b81221 100644 --- a/apps/warehouse/web/api/v1/api.py +++ b/apps/warehouse/web/api/v1/api.py @@ -15,7 +15,7 @@ import typing class InventoryEntryViewSet(viewsets.ModelViewSet, DynamicSearchMixin): queryset = warehouse_models.InventoryEntry.objects.all() serializer_class = warehouse_serializers.InventoryEntrySerializer - filter_backends = [filters.SearchFilter] + # filter_backends = [filters.SearchFilter] search_fields = [ "distribution__distribution_id", "organization__name", @@ -91,11 +91,11 @@ class InventoryEntryViewSet(viewsets.ModelViewSet, DynamicSearchMixin): def my_inventory_entries(self, request): """ list of my inventory entries """ - queryset = self.filter_queryset(self.queryset) # return by search param or all objects - entries = queryset.filter(organization=get_organization_by_user(request.user)) + entries = self.queryset.filter(organization=get_organization_by_user(request.user)) + queryset = self.filter_query(entries) # return by search param or all objects # paginate & response - page = self.paginate_queryset(entries) + page = self.paginate_queryset(queryset) if page is not None: serializer = self.get_serializer(page, many=True) return self.get_paginated_response(serializer.data)