fix search mixin bug
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
from apps.core.services.filter.search import DynamicSearchService
|
from apps.core.services.filter.search import DynamicSearchService
|
||||||
|
from rest_framework.exceptions import APIException
|
||||||
|
|
||||||
|
|
||||||
class DynamicSearchMixin:
|
class DynamicSearchMixin:
|
||||||
@@ -10,8 +11,8 @@ class DynamicSearchMixin:
|
|||||||
def get_date_field(self):
|
def get_date_field(self):
|
||||||
return getattr(self, "date_field", "create_date")
|
return getattr(self, "date_field", "create_date")
|
||||||
|
|
||||||
def filter_queryset(self, queryset):
|
def filter_query(self, queryset):
|
||||||
queryset = super().filter_queryset(queryset) # noqa
|
queryset = queryset # noqa
|
||||||
|
|
||||||
q = self.request.query_params.get("search") # noqa
|
q = self.request.query_params.get("search") # noqa
|
||||||
start = self.request.query_params.get("start") # noqa
|
start = self.request.query_params.get("start") # noqa
|
||||||
|
|||||||
@@ -310,7 +310,7 @@ class QuotaViewSet(viewsets.ModelViewSet, DynamicSearchMixin): # noqa
|
|||||||
def active_quotas(self, request):
|
def active_quotas(self, request):
|
||||||
""" list of organization active quotas """
|
""" 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)
|
organization = get_organization_by_user(request.user)
|
||||||
|
|
||||||
@@ -336,7 +336,7 @@ class QuotaViewSet(viewsets.ModelViewSet, DynamicSearchMixin): # noqa
|
|||||||
def closed_quotas(self, request):
|
def closed_quotas(self, request):
|
||||||
""" list of organization closed quotas """
|
""" 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)
|
organization = get_organization_by_user(request.user)
|
||||||
|
|
||||||
@@ -363,7 +363,7 @@ class QuotaViewSet(viewsets.ModelViewSet, DynamicSearchMixin): # noqa
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
quota = self.get_object()
|
quota = self.get_object()
|
||||||
queryset = self.filter_queryset(
|
queryset = self.filter_query(
|
||||||
quota.distributions_assigned.all().order_by('-modify_date')
|
quota.distributions_assigned.all().order_by('-modify_date')
|
||||||
) # return by search param or all objects
|
) # return by search param or all objects
|
||||||
|
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ class QuotaDistributionViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
|||||||
def my_distributions(self, request):
|
def my_distributions(self, request):
|
||||||
""" list of my distributions """
|
""" 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)
|
organization = get_organization_by_user(request.user)
|
||||||
|
|
||||||
query = self.request.query_params
|
query = self.request.query_params
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import typing
|
|||||||
class InventoryEntryViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
class InventoryEntryViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||||
queryset = warehouse_models.InventoryEntry.objects.all()
|
queryset = warehouse_models.InventoryEntry.objects.all()
|
||||||
serializer_class = warehouse_serializers.InventoryEntrySerializer
|
serializer_class = warehouse_serializers.InventoryEntrySerializer
|
||||||
filter_backends = [filters.SearchFilter]
|
# filter_backends = [filters.SearchFilter]
|
||||||
search_fields = [
|
search_fields = [
|
||||||
"distribution__distribution_id",
|
"distribution__distribution_id",
|
||||||
"organization__name",
|
"organization__name",
|
||||||
@@ -91,11 +91,11 @@ class InventoryEntryViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
|||||||
def my_inventory_entries(self, request):
|
def my_inventory_entries(self, request):
|
||||||
""" list of my inventory entries """
|
""" list of my inventory entries """
|
||||||
|
|
||||||
queryset = self.filter_queryset(self.queryset) # return by search param or all objects
|
entries = self.queryset.filter(organization=get_organization_by_user(request.user))
|
||||||
entries = queryset.filter(organization=get_organization_by_user(request.user))
|
queryset = self.filter_query(entries) # return by search param or all objects
|
||||||
|
|
||||||
# paginate & response
|
# paginate & response
|
||||||
page = self.paginate_queryset(entries)
|
page = self.paginate_queryset(queryset)
|
||||||
if page is not None:
|
if page is not None:
|
||||||
serializer = self.get_serializer(page, many=True)
|
serializer = self.get_serializer(page, many=True)
|
||||||
return self.get_paginated_response(serializer.data)
|
return self.get_paginated_response(serializer.data)
|
||||||
|
|||||||
Reference in New Issue
Block a user