dynamic mixin search for all apis
This commit is contained in:
29
apps/core/mixins/search_mixin.py
Normal file
29
apps/core/mixins/search_mixin.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from apps.core.services.filter.search import DynamicSearchService
|
||||
|
||||
|
||||
class DynamicSearchMixin:
|
||||
""" search query sets with introduced fields in view set """
|
||||
|
||||
def get_search_fields(self):
|
||||
return getattr(self, "search_fields", [])
|
||||
|
||||
def get_date_field(self):
|
||||
return getattr(self, "date_field", "create_date")
|
||||
|
||||
def filter_queryset(self, queryset):
|
||||
queryset = super().filter_queryset(queryset) # noqa
|
||||
|
||||
q = self.request.query_params.get("search") # noqa
|
||||
start = self.request.query_params.get("start") # noqa
|
||||
end = self.request.query_params.get("end") # noqa
|
||||
search_fields = self.get_search_fields()
|
||||
date_field = self.get_date_field()
|
||||
|
||||
return DynamicSearchService(
|
||||
queryset=queryset,
|
||||
query_string=q,
|
||||
search_fields=search_fields,
|
||||
start=start,
|
||||
end=end,
|
||||
date_field=date_field
|
||||
).apply()
|
||||
Reference in New Issue
Block a user