update excel
This commit is contained in:
@@ -1,40 +0,0 @@
|
||||
from django.db.models import Q
|
||||
from apps.product.models import QuotaDistribution
|
||||
from django.db.models.functions import TruncDate
|
||||
|
||||
|
||||
class QuotaDistributionSearch:
|
||||
def __init__(self, query: str = None, start_date: str = None, end_date: str = None):
|
||||
self.query = (query or '').strip()
|
||||
self.start_date = start_date
|
||||
self.end_date = end_date
|
||||
|
||||
def search(self):
|
||||
""" multi term search & filter by date range """
|
||||
|
||||
queryset = QuotaDistribution.objects.all() # noqa
|
||||
|
||||
if self.start_date or self.end_date:
|
||||
queryset = queryset.annotate(date_only=TruncDate('create_date'))
|
||||
if self.start_date:
|
||||
queryset = queryset.filter(date_only__gte=self.start_date)
|
||||
if self.end_date:
|
||||
queryset = queryset.filter(date_only__lte=self.end_date)
|
||||
|
||||
# convert string to list of words
|
||||
if self.query:
|
||||
keywords = [word.strip() for word in self.query.split(',') if word.strip()]
|
||||
if keywords:
|
||||
combined_q = Q()
|
||||
for keyword in keywords:
|
||||
combined_q |= Q(assigner_organization__name__icontains=keyword)
|
||||
combined_q |= Q(assigned_organization__name__icontains=keyword)
|
||||
combined_q |= Q(distribution_id__icontains=keyword)
|
||||
combined_q |= Q(quota__quota_id__icontains=keyword)
|
||||
combined_q |= Q(quota__product__name__icontains=keyword)
|
||||
combined_q |= Q(quota__sale_type__icontains=keyword)
|
||||
combined_q |= Q(quota__group__icontains=keyword)
|
||||
|
||||
queryset = queryset.filter(combined_q)
|
||||
|
||||
return queryset.distinct()
|
||||
@@ -1,39 +0,0 @@
|
||||
from django.db.models import Q
|
||||
from django.db.models.functions import TruncDate
|
||||
from apps.product.models import Quota
|
||||
|
||||
|
||||
class QuotaSearch:
|
||||
def __init__(self, query: str = None, start_date: str = None, end_date: str = None):
|
||||
self.query = (query or '').strip()
|
||||
self.start_date = start_date
|
||||
self.end_date = end_date
|
||||
|
||||
def search(self):
|
||||
""" multi term search & filter by date range """
|
||||
|
||||
queryset = QuotaDistribution.objects.all() # noqa
|
||||
|
||||
if self.start_date or self.end_date:
|
||||
queryset = queryset.annotate(date_only=TruncDate('create_date'))
|
||||
if self.start_date:
|
||||
queryset = queryset.filter(date_only__gte=self.start_date)
|
||||
if self.end_date:
|
||||
queryset = queryset.filter(date_only__lte=self.end_date)
|
||||
|
||||
# convert string to list of words
|
||||
if self.query:
|
||||
keywords = [word.strip() for word in self.query.split(',') if word.strip()]
|
||||
if keywords:
|
||||
combined_q = Q()
|
||||
for keyword in keywords:
|
||||
combined_q |= Q(registerer_organization__name__icontains=keyword)
|
||||
combined_q |= Q(quota_id__icontains=keyword)
|
||||
combined_q |= Q(product__name__icontains=keyword)
|
||||
combined_q |= Q(sale_type__icontains=keyword)
|
||||
combined_q |= Q(sale_unit__unit__icontains=keyword)
|
||||
combined_q |= Q(group__icontains=keyword)
|
||||
|
||||
queryset = queryset.filter(combined_q)
|
||||
|
||||
return queryset.distinct()
|
||||
@@ -1,8 +1,8 @@
|
||||
from apps.product.web.api.v1.serializers import quota_distribution_serializers
|
||||
from apps.product.services.services import get_products_in_warehouse
|
||||
from apps.product.web.api.v1.serializers import quota_serializers
|
||||
from apps.product.services.search.quota_search import QuotaSearch
|
||||
from apps.product.exceptions import QuotaExpiredTimeException
|
||||
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||
from apps.core.pagination import CustomPageNumberPagination
|
||||
from apps.product.web.api.v1.viewsets import product_api
|
||||
from common.helpers import get_organization_by_user
|
||||
@@ -31,31 +31,21 @@ def delete(queryset, pk):
|
||||
obj.delete()
|
||||
|
||||
|
||||
class QuotaViewSet(viewsets.ModelViewSet): # noqa
|
||||
class QuotaViewSet(viewsets.ModelViewSet, DynamicSearchMixin): # noqa
|
||||
""" apis for product quota """
|
||||
|
||||
queryset = product_models.Quota.objects.all()
|
||||
serializer_class = quota_serializers.QuotaSerializer
|
||||
filter_backends = [filters.SearchFilter]
|
||||
CustomPageNumberPagination.page_size = 5
|
||||
search_fields = ['']
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
""" search & filter quotas or return all """
|
||||
|
||||
params = self.request.query_params
|
||||
query = params.get('search')
|
||||
start_date = params.get('start')
|
||||
end_date = params.get('end')
|
||||
|
||||
search = QuotaSearch(
|
||||
query=query,
|
||||
start_date=start_date,
|
||||
end_date=end_date
|
||||
)
|
||||
|
||||
serializer = self.serializer_class(search.search(), many=True)
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
search_fields = [
|
||||
"registerer_organization__name",
|
||||
"quota_id",
|
||||
"product__name",
|
||||
"sale_type",
|
||||
"sale_unit__unit",
|
||||
"group",
|
||||
]
|
||||
|
||||
@transaction.atomic
|
||||
def create(self, request, *args, **kwargs):
|
||||
@@ -320,11 +310,13 @@ class QuotaViewSet(viewsets.ModelViewSet): # noqa
|
||||
def active_quotas(self, request):
|
||||
""" list of organization active quotas """
|
||||
|
||||
queryset = self.filter_query(self.queryset) # return by search param or all objects
|
||||
|
||||
organization = get_organization_by_user(request.user)
|
||||
|
||||
# paginate queryset
|
||||
page = self.paginate_queryset(
|
||||
self.queryset.filter(
|
||||
queryset.filter(
|
||||
Q(registerer_organization=organization),
|
||||
Q(is_closed=False)
|
||||
).order_by('-modify_date')
|
||||
@@ -344,11 +336,13 @@ class QuotaViewSet(viewsets.ModelViewSet): # noqa
|
||||
def closed_quotas(self, request):
|
||||
""" list of organization closed quotas """
|
||||
|
||||
queryset = self.filter_query(self.queryset) # return by search param or all objects
|
||||
|
||||
organization = get_organization_by_user(request.user)
|
||||
|
||||
# paginate queryset
|
||||
page = self.paginate_queryset(
|
||||
self.queryset.filter(
|
||||
queryset.filter(
|
||||
Q(registerer_organization=organization),
|
||||
Q(is_closed=True)
|
||||
).order_by('-modify_date')
|
||||
@@ -369,10 +363,13 @@ class QuotaViewSet(viewsets.ModelViewSet): # noqa
|
||||
|
||||
try:
|
||||
quota = self.get_object()
|
||||
queryset = self.filter_query(
|
||||
quota.distributions_assigned.all().order_by('-modify_date')
|
||||
) # return by search param or all objects
|
||||
|
||||
# paginate queryset
|
||||
page = self.paginate_queryset(
|
||||
quota.distributions_assigned.all().order_by('-modify_date')
|
||||
queryset
|
||||
)
|
||||
if page is not None:
|
||||
serializer = quota_distribution_serializers.QuotaDistributionSerializer(
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
from django.db.models import Q
|
||||
|
||||
from apps.product.web.api.v1.serializers import quota_distribution_serializers as distribution_serializers
|
||||
from apps.product.services.search.distribution_search import QuotaDistributionSearch
|
||||
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||
from apps.core.pagination import CustomPageNumberPagination
|
||||
from rest_framework.exceptions import APIException
|
||||
from apps.product import models as product_models
|
||||
@@ -10,6 +8,7 @@ from rest_framework.decorators import action
|
||||
from rest_framework import viewsets, filters
|
||||
from rest_framework import status
|
||||
from django.db import transaction
|
||||
from django.db.models import Q
|
||||
|
||||
from common.helpers import get_organization_by_user
|
||||
|
||||
@@ -27,31 +26,23 @@ def delete(queryset, pk):
|
||||
obj.delete()
|
||||
|
||||
|
||||
class QuotaDistributionViewSet(viewsets.ModelViewSet):
|
||||
class QuotaDistributionViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
""" quota distribution apis """
|
||||
|
||||
queryset = product_models.QuotaDistribution.objects.all()
|
||||
serializer_class = distribution_serializers.QuotaDistributionSerializer
|
||||
filter_backends = [filters.SearchFilter]
|
||||
search_fields = ['']
|
||||
CustomPageNumberPagination.page_size = 5
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
""" search & filter in distributions or return all objects """
|
||||
|
||||
params = self.request.query_params
|
||||
query = params.get('search')
|
||||
start_date = params.get('start')
|
||||
end_date = params.get('end')
|
||||
|
||||
search = QuotaDistributionSearch(
|
||||
query,
|
||||
start_date,
|
||||
end_date
|
||||
)
|
||||
|
||||
serializer = self.serializer_class(search.search(), many=True)
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
search_fields = [
|
||||
"assigner_organization__name",
|
||||
"assigned_organization__name",
|
||||
"distribution_id",
|
||||
"quota__quota_id",
|
||||
"quota__product__name",
|
||||
"quota__sale_type",
|
||||
"quota__group",
|
||||
]
|
||||
date_field = "create_date"
|
||||
|
||||
@transaction.atomic
|
||||
def create(self, request, *args, **kwargs):
|
||||
@@ -99,13 +90,15 @@ class QuotaDistributionViewSet(viewsets.ModelViewSet):
|
||||
)
|
||||
def my_distributions(self, request):
|
||||
""" list of my distributions """
|
||||
query = self.request.query_params
|
||||
|
||||
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
|
||||
if query.get('param') == 'assigned':
|
||||
# paginate queryset
|
||||
page = self.paginate_queryset(
|
||||
self.queryset.filter(
|
||||
queryset.filter(
|
||||
Q(assigned_organization=organization)
|
||||
).order_by('-modify_date')
|
||||
)
|
||||
@@ -113,7 +106,7 @@ class QuotaDistributionViewSet(viewsets.ModelViewSet):
|
||||
elif query.get('param') == 'assigner':
|
||||
# paginate queryset
|
||||
page = self.paginate_queryset(
|
||||
self.queryset.filter(
|
||||
queryset.filter(
|
||||
Q(assigner_organization=organization)
|
||||
).order_by('-modify_date')
|
||||
)
|
||||
@@ -121,7 +114,7 @@ class QuotaDistributionViewSet(viewsets.ModelViewSet):
|
||||
elif query.get('param') == 'all':
|
||||
# paginate queryset
|
||||
page = self.paginate_queryset(
|
||||
self.queryset.filter(
|
||||
queryset.filter(
|
||||
Q(assigner_organization=organization) |
|
||||
Q(assigned_organization=organization)
|
||||
).order_by('-modify_date')
|
||||
|
||||
Reference in New Issue
Block a user