Merge branch 'main' of https://github.com/MNPC-IR/Rasaddam_Backend
This commit is contained in:
@@ -7,10 +7,11 @@ from rest_framework.decorators import action
|
||||
|
||||
from apps.authorization.api.v1.serializers import UserRelationSerializer
|
||||
from apps.authorization.models import UserRelations
|
||||
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||
from common.helper_excel import excel_description, create_header_freez, create_value
|
||||
|
||||
|
||||
class AuthExcelViewSet(viewsets.ModelViewSet):
|
||||
class AuthExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
queryset = UserRelations.objects.all()
|
||||
serializer_class = UserRelationSerializer
|
||||
|
||||
|
||||
@@ -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,10 +17,18 @@ 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]
|
||||
search_fields = [
|
||||
"registerer_organization__name",
|
||||
"quota_id",
|
||||
"product__name",
|
||||
"sale_type",
|
||||
"sale_unit__unit",
|
||||
"group",
|
||||
]
|
||||
|
||||
# noqa # سهمیه و توزیع
|
||||
@action(
|
||||
@@ -183,14 +191,16 @@ 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()
|
||||
|
||||
incentive_plans = user_relations.incentive_plans.filter(
|
||||
Q(is_time_unlimited=False) |
|
||||
Q(start_date_limit__lte=today, end_date_limit__gte=today)
|
||||
)
|
||||
|
||||
incentive_plans = self.filter_query(incentive_plans, search_list=search_fields_incentive_plans)
|
||||
ser_data = IncentivePlanSerializer(incentive_plans, many=True).data
|
||||
|
||||
excel_options = [
|
||||
@@ -269,8 +279,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
def detail_quota_excel(self, request):
|
||||
queryset = product_models.Quota.objects.filter(id=request.GET['id'], trash=False)
|
||||
serializer_class = QuotaSerializer
|
||||
filter_backends = [filters.SearchFilter]
|
||||
search_fields = [
|
||||
search_fields_detail_quota = [
|
||||
"registerer_organization__name",
|
||||
"quota_id",
|
||||
"product__name",
|
||||
@@ -278,6 +287,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
"sale_unit__unit",
|
||||
"group",
|
||||
]
|
||||
queryset = self.filter_query(queryset, search_list=search_fields_detail_quota)
|
||||
|
||||
output = BytesIO()
|
||||
workbook = Workbook()
|
||||
@@ -286,7 +296,6 @@ 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
|
||||
|
||||
organization = get_organization_by_user(request.user)
|
||||
queryset = queryset.filter(
|
||||
@@ -525,10 +534,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
name='quota_excel'
|
||||
)
|
||||
def quota_excel(self, request):
|
||||
queryset = product_models.Quota.objects.all()
|
||||
serializer_class = QuotaSerializer
|
||||
filter_backends = [filters.SearchFilter]
|
||||
search_fields = [
|
||||
search_fields_quota = [
|
||||
"registerer_organization__name",
|
||||
"quota_id",
|
||||
"product__name",
|
||||
@@ -537,6 +543,8 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
"group",
|
||||
]
|
||||
|
||||
queryset = product_models.Quota.objects.all()
|
||||
serializer_class = QuotaSerializer
|
||||
output = BytesIO()
|
||||
workbook = Workbook()
|
||||
worksheet = workbook.active
|
||||
@@ -544,11 +552,10 @@ 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
|
||||
|
||||
organization = get_organization_by_user(request.user)
|
||||
|
||||
if active:
|
||||
if active == 'true':
|
||||
queryset = queryset.filter(
|
||||
Q(registerer_organization=organization),
|
||||
Q(is_closed=False)
|
||||
@@ -562,6 +569,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
).order_by('-modify_date')
|
||||
|
||||
quta_type = 'بایگانی'
|
||||
queryset = self.filter_query(queryset, search_list=search_fields_quota) # return by search param or all objects
|
||||
|
||||
ser_data = serializer_class(queryset, many=True).data
|
||||
excel_options = [
|
||||
|
||||
@@ -6,6 +6,7 @@ from openpyxl.styles import Font
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.decorators import action
|
||||
|
||||
from apps.core.mixins.search_mixin import ExcelDynamicSearchMixin
|
||||
from apps.warehouse import models as warehouse_models
|
||||
from apps.warehouse.web.api.v1 import serializers as warehouse_serializers
|
||||
from common.helper_excel import create_header, excel_description, create_header_freez, create_value, shamsi_date, \
|
||||
@@ -13,9 +14,18 @@ from common.helper_excel import create_header, excel_description, create_header_
|
||||
from common.helpers import get_organization_by_user
|
||||
|
||||
|
||||
class WareHouseExcelViewSet(viewsets.ModelViewSet):
|
||||
class WareHouseExcelViewSet(viewsets.ModelViewSet, ExcelDynamicSearchMixin):
|
||||
queryset = warehouse_models.InventoryEntry.objects.all()
|
||||
serializer_class = warehouse_serializers.InventoryEntrySerializer
|
||||
search_fields = [
|
||||
"distribution__distribution_id",
|
||||
"organization__name",
|
||||
"weight",
|
||||
"balance",
|
||||
"lading_number",
|
||||
"is_confirmed",
|
||||
]
|
||||
date_field = "create_date"
|
||||
|
||||
# noqa # ورودی به انبار
|
||||
@action(
|
||||
@@ -31,8 +41,9 @@ class WareHouseExcelViewSet(viewsets.ModelViewSet):
|
||||
worksheet = workbook.active
|
||||
worksheet.sheet_view.rightToLeft = True
|
||||
worksheet.insert_rows(1)
|
||||
queryset = self.filter_query(self.queryset)
|
||||
|
||||
entries = self.queryset.filter(organization=get_organization_by_user(request.user))
|
||||
entries = queryset.filter(organization=get_organization_by_user(request.user))
|
||||
ser_data = self.serializer_class(entries, many=True).data
|
||||
|
||||
excel_options = [
|
||||
|
||||
Reference in New Issue
Block a user