update search for excel
This commit is contained in:
@@ -28,3 +28,34 @@ class DynamicSearchMixin:
|
||||
end=end,
|
||||
date_field=date_field
|
||||
).apply()
|
||||
|
||||
|
||||
class ExcelDynamicSearchMixin:
|
||||
""" 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_query(self, queryset, search_list=None):
|
||||
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
|
||||
if search_list:
|
||||
search_fields = search_list
|
||||
else:
|
||||
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()
|
||||
@@ -7,7 +7,7 @@ from openpyxl import Workbook
|
||||
from rest_framework import viewsets, filters
|
||||
from rest_framework.decorators import action
|
||||
|
||||
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||
from apps.core.mixins.search_mixin import ExcelDynamicSearchMixin
|
||||
from apps.product import models as product_models
|
||||
from apps.product.web.api.v1.serializers import quota_distribution_serializers as distribution_serializers
|
||||
from apps.product.web.api.v1.serializers.product_serializers import IncentivePlanSerializer
|
||||
@@ -17,7 +17,7 @@ from common.helper_excel import create_header, excel_description, create_header_
|
||||
from common.helpers import get_organization_by_user
|
||||
|
||||
|
||||
class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
class ProductExcelViewSet(viewsets.ModelViewSet, ExcelDynamicSearchMixin):
|
||||
queryset = product_models.QuotaDistribution.objects.all()
|
||||
serializer_class = distribution_serializers.QuotaDistributionSerializer
|
||||
filter_backends = [filters.SearchFilter]
|
||||
@@ -191,6 +191,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
worksheet = workbook.active
|
||||
worksheet.sheet_view.rightToLeft = True
|
||||
worksheet.insert_rows(1)
|
||||
search_fields_incentive_plans = ['plan_type', 'name']
|
||||
|
||||
today = datetime.now().date()
|
||||
user_relations = product_models.UserRelations.objects.filter(user=request.user).first()
|
||||
@@ -199,7 +200,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
Q(is_time_unlimited=False) |
|
||||
Q(start_date_limit__lte=today, end_date_limit__gte=today)
|
||||
)
|
||||
incentive_plans = self.filter_query(incentive_plans)
|
||||
incentive_plans = self.filter_query(incentive_plans, search_list=search_fields_incentive_plans)
|
||||
ser_data = IncentivePlanSerializer(incentive_plans, many=True).data
|
||||
|
||||
excel_options = [
|
||||
@@ -279,8 +280,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
queryset = product_models.Quota.objects.filter(id=request.GET['id'], trash=False)
|
||||
queryset = self.filter_query(queryset)
|
||||
serializer_class = QuotaSerializer
|
||||
filter_backends = [filters.SearchFilter]
|
||||
search_fields = [
|
||||
search_fields_detail_quota = [
|
||||
"registerer_organization__name",
|
||||
"quota_id",
|
||||
"product__name",
|
||||
@@ -296,7 +296,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
worksheet.insert_rows(1)
|
||||
|
||||
active = request.GET.get('active')
|
||||
queryset = self.filter_query(queryset) # return by search param or all objects
|
||||
queryset = self.filter_query(queryset, search_list=search_fields_detail_quota) # return by search param or all objects
|
||||
|
||||
organization = get_organization_by_user(request.user)
|
||||
queryset = queryset.filter(
|
||||
@@ -535,7 +535,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
name='quota_excel'
|
||||
)
|
||||
def quota_excel(self, request):
|
||||
search_fields = [
|
||||
search_fields_quota = [
|
||||
"registerer_organization__name",
|
||||
"quota_id",
|
||||
"product__name",
|
||||
@@ -543,13 +543,9 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
"sale_unit__unit",
|
||||
"group",
|
||||
]
|
||||
|
||||
queryset = product_models.Quota.objects.all()
|
||||
queryset = self.filter_query(queryset)
|
||||
|
||||
serializer_class = QuotaSerializer
|
||||
filter_backends = [filters.SearchFilter]
|
||||
|
||||
|
||||
output = BytesIO()
|
||||
workbook = Workbook()
|
||||
worksheet = workbook.active
|
||||
@@ -557,7 +553,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
worksheet.insert_rows(1)
|
||||
|
||||
active = request.GET.get('active')
|
||||
queryset = self.filter_query(queryset) # return by search param or all objects
|
||||
queryset = self.filter_query(queryset, search_list=search_fields_quota) # return by search param or all objects
|
||||
|
||||
organization = get_organization_by_user(request.user)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user