dynamic mixin search for all apis
This commit is contained in:
2
.idea/Rasaddam_Backend.iml
generated
2
.idea/Rasaddam_Backend.iml
generated
@@ -14,7 +14,7 @@
|
|||||||
</component>
|
</component>
|
||||||
<component name="NewModuleRootManager">
|
<component name="NewModuleRootManager">
|
||||||
<content url="file://$MODULE_DIR$" />
|
<content url="file://$MODULE_DIR$" />
|
||||||
<orderEntry type="jdk" jdkName="Python 3.10 (bazrasienv)" jdkType="Python SDK" />
|
<orderEntry type="jdk" jdkName="Python 3.10 (env)" jdkType="Python SDK" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
</component>
|
</component>
|
||||||
<component name="PyDocumentationSettings">
|
<component name="PyDocumentationSettings">
|
||||||
|
|||||||
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (bazrasienv)" project-jdk-type="Python SDK" />
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (env)" project-jdk-type="Python SDK" />
|
||||||
</project>
|
</project>
|
||||||
@@ -383,8 +383,8 @@ JAZZMIN_SETTINGS = {
|
|||||||
# Copyright on the footer
|
# Copyright on the footer
|
||||||
"copyright": "Acme Library Ltd",
|
"copyright": "Acme Library Ltd",
|
||||||
|
|
||||||
# List of model admins to search from the search bar, search bar omitted if excluded
|
# List of model admins to filter from the filter bar, filter bar omitted if excluded
|
||||||
# If you want to use a single search field you dont need to use a list, you can use a simple string
|
# If you want to use a single filter field you dont need to use a list, you can use a simple string
|
||||||
"search_model": ["auth.User", "auth.Group"],
|
"search_model": ["auth.User", "auth.Group"],
|
||||||
|
|
||||||
# Field name on user model that contains avatar ImageField/URLField/Charfield or a callable that receives the user
|
# Field name on user model that contains avatar ImageField/URLField/Charfield or a callable that receives the user
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ urlpatterns = [
|
|||||||
path('herd/', include('apps.herd.urls')),
|
path('herd/', include('apps.herd.urls')),
|
||||||
path('livestock/', include('apps.livestock.urls')),
|
path('livestock/', include('apps.livestock.urls')),
|
||||||
path('tag/', include('apps.tag.urls')),
|
path('tag/', include('apps.tag.urls')),
|
||||||
path('search/', include('apps.search.urls')),
|
path('filter/', include('apps.search.urls')),
|
||||||
path('product/', include('apps.product.urls')),
|
path('product/', include('apps.product.urls')),
|
||||||
path('warehouse/', include('apps.warehouse.urls')),
|
path('warehouse/', include('apps.warehouse.urls')),
|
||||||
path('pos_device/', include('apps.pos_device.urls')),
|
path('pos_device/', include('apps.pos_device.urls')),
|
||||||
|
|||||||
@@ -24,8 +24,8 @@
|
|||||||
# def get(self, request, query):
|
# def get(self, request, query):
|
||||||
# try:
|
# try:
|
||||||
# q = self.generate_q_expression(query)
|
# q = self.generate_q_expression(query)
|
||||||
# search = self.document_class.search().query(q)
|
# filter = self.document_class.filter().query(q)
|
||||||
# response = search.execute()
|
# response = filter.execute()
|
||||||
#
|
#
|
||||||
# print(f"Found {response.hits.total.value} hit(s) for query: '{query}'")
|
# print(f"Found {response.hits.total.value} hit(s) for query: '{query}'")
|
||||||
#
|
#
|
||||||
|
|||||||
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()
|
||||||
0
apps/core/services/filter/__init__.py
Normal file
0
apps/core/services/filter/__init__.py
Normal file
40
apps/core/services/filter/search.py
Normal file
40
apps/core/services/filter/search.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
from django.db.models import Q
|
||||||
|
from django.db.models.functions import TruncDate
|
||||||
|
|
||||||
|
|
||||||
|
class DynamicSearchService:
|
||||||
|
def __init__(self, queryset, query_string: str = "", search_fields: list[str] = None,
|
||||||
|
start: str = None, end: str = None, date_field: str = "create_date"):
|
||||||
|
self.queryset = queryset
|
||||||
|
self.query = (query_string or "").strip()
|
||||||
|
self.fields = search_fields or []
|
||||||
|
self.start = start
|
||||||
|
self.end = end
|
||||||
|
self.date_field = date_field
|
||||||
|
|
||||||
|
def apply(self):
|
||||||
|
qs = self.queryset
|
||||||
|
|
||||||
|
if self.query and self.fields:
|
||||||
|
keywords = [k.strip() for k in self.query.split(",") if k.strip()]
|
||||||
|
q_obj = Q()
|
||||||
|
for kw in keywords:
|
||||||
|
for field in self.fields:
|
||||||
|
q_obj |= Q(**{f"{field}__icontains": kw}) # noqa
|
||||||
|
qs = qs.filter(q_obj).distinct()
|
||||||
|
|
||||||
|
if self.start or self.end:
|
||||||
|
qs = qs.annotate(date_only=TruncDate(self.date_field))
|
||||||
|
if self.start:
|
||||||
|
try:
|
||||||
|
qs = qs.filter(date_only__gte=self.start)
|
||||||
|
except ValueError:
|
||||||
|
pass # or log
|
||||||
|
|
||||||
|
if self.end:
|
||||||
|
try:
|
||||||
|
qs = qs.filter(date_only__lte=self.end)
|
||||||
|
except ValueError:
|
||||||
|
pass # or log
|
||||||
|
|
||||||
|
return qs
|
||||||
@@ -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.web.api.v1.serializers import quota_distribution_serializers
|
||||||
from apps.product.services.services import get_products_in_warehouse
|
from apps.product.services.services import get_products_in_warehouse
|
||||||
from apps.product.web.api.v1.serializers import quota_serializers
|
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.product.exceptions import QuotaExpiredTimeException
|
||||||
|
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||||
from apps.core.pagination import CustomPageNumberPagination
|
from apps.core.pagination import CustomPageNumberPagination
|
||||||
from apps.product.web.api.v1.viewsets import product_api
|
from apps.product.web.api.v1.viewsets import product_api
|
||||||
from common.helpers import get_organization_by_user
|
from common.helpers import get_organization_by_user
|
||||||
@@ -31,31 +31,21 @@ def delete(queryset, pk):
|
|||||||
obj.delete()
|
obj.delete()
|
||||||
|
|
||||||
|
|
||||||
class QuotaViewSet(viewsets.ModelViewSet): # noqa
|
class QuotaViewSet(viewsets.ModelViewSet, DynamicSearchMixin): # noqa
|
||||||
""" apis for product quota """
|
""" apis for product quota """
|
||||||
|
|
||||||
queryset = product_models.Quota.objects.all()
|
queryset = product_models.Quota.objects.all()
|
||||||
serializer_class = quota_serializers.QuotaSerializer
|
serializer_class = quota_serializers.QuotaSerializer
|
||||||
filter_backends = [filters.SearchFilter]
|
filter_backends = [filters.SearchFilter]
|
||||||
CustomPageNumberPagination.page_size = 5
|
CustomPageNumberPagination.page_size = 5
|
||||||
search_fields = ['']
|
search_fields = [
|
||||||
|
"registerer_organization__name",
|
||||||
def list(self, request, *args, **kwargs):
|
"quota_id",
|
||||||
""" search & filter quotas or return all """
|
"product__name",
|
||||||
|
"sale_type",
|
||||||
params = self.request.query_params
|
"sale_unit__unit",
|
||||||
query = params.get('search')
|
"group",
|
||||||
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)
|
|
||||||
|
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
def create(self, request, *args, **kwargs):
|
def create(self, request, *args, **kwargs):
|
||||||
@@ -320,11 +310,13 @@ class QuotaViewSet(viewsets.ModelViewSet): # 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
|
||||||
|
|
||||||
organization = get_organization_by_user(request.user)
|
organization = get_organization_by_user(request.user)
|
||||||
|
|
||||||
# paginate queryset
|
# paginate queryset
|
||||||
page = self.paginate_queryset(
|
page = self.paginate_queryset(
|
||||||
self.queryset.filter(
|
queryset.filter(
|
||||||
Q(registerer_organization=organization),
|
Q(registerer_organization=organization),
|
||||||
Q(is_closed=False)
|
Q(is_closed=False)
|
||||||
).order_by('-modify_date')
|
).order_by('-modify_date')
|
||||||
@@ -344,11 +336,13 @@ class QuotaViewSet(viewsets.ModelViewSet): # 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
|
||||||
|
|
||||||
organization = get_organization_by_user(request.user)
|
organization = get_organization_by_user(request.user)
|
||||||
|
|
||||||
# paginate queryset
|
# paginate queryset
|
||||||
page = self.paginate_queryset(
|
page = self.paginate_queryset(
|
||||||
self.queryset.filter(
|
queryset.filter(
|
||||||
Q(registerer_organization=organization),
|
Q(registerer_organization=organization),
|
||||||
Q(is_closed=True)
|
Q(is_closed=True)
|
||||||
).order_by('-modify_date')
|
).order_by('-modify_date')
|
||||||
@@ -369,10 +363,13 @@ class QuotaViewSet(viewsets.ModelViewSet): # noqa
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
quota = self.get_object()
|
quota = self.get_object()
|
||||||
|
queryset = self.filter_queryset(
|
||||||
|
quota.distributions_assigned.all().order_by('-modify_date')
|
||||||
|
) # return by search param or all objects
|
||||||
|
|
||||||
# paginate queryset
|
# paginate queryset
|
||||||
page = self.paginate_queryset(
|
page = self.paginate_queryset(
|
||||||
quota.distributions_assigned.all().order_by('-modify_date')
|
queryset
|
||||||
)
|
)
|
||||||
if page is not None:
|
if page is not None:
|
||||||
serializer = quota_distribution_serializers.QuotaDistributionSerializer(
|
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.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 apps.core.pagination import CustomPageNumberPagination
|
||||||
from rest_framework.exceptions import APIException
|
from rest_framework.exceptions import APIException
|
||||||
from apps.product import models as product_models
|
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 viewsets, filters
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
from django.db.models import Q
|
||||||
|
|
||||||
from common.helpers import get_organization_by_user
|
from common.helpers import get_organization_by_user
|
||||||
|
|
||||||
@@ -27,31 +26,23 @@ def delete(queryset, pk):
|
|||||||
obj.delete()
|
obj.delete()
|
||||||
|
|
||||||
|
|
||||||
class QuotaDistributionViewSet(viewsets.ModelViewSet):
|
class QuotaDistributionViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||||
""" quota distribution apis """
|
""" quota distribution apis """
|
||||||
|
|
||||||
queryset = product_models.QuotaDistribution.objects.all()
|
queryset = product_models.QuotaDistribution.objects.all()
|
||||||
serializer_class = distribution_serializers.QuotaDistributionSerializer
|
serializer_class = distribution_serializers.QuotaDistributionSerializer
|
||||||
filter_backends = [filters.SearchFilter]
|
filter_backends = [filters.SearchFilter]
|
||||||
search_fields = ['']
|
|
||||||
CustomPageNumberPagination.page_size = 5
|
CustomPageNumberPagination.page_size = 5
|
||||||
|
search_fields = [
|
||||||
def list(self, request, *args, **kwargs):
|
"assigner_organization__name",
|
||||||
""" search & filter in distributions or return all objects """
|
"assigned_organization__name",
|
||||||
|
"distribution_id",
|
||||||
params = self.request.query_params
|
"quota__quota_id",
|
||||||
query = params.get('search')
|
"quota__product__name",
|
||||||
start_date = params.get('start')
|
"quota__sale_type",
|
||||||
end_date = params.get('end')
|
"quota__group",
|
||||||
|
]
|
||||||
search = QuotaDistributionSearch(
|
date_field = "create_date"
|
||||||
query,
|
|
||||||
start_date,
|
|
||||||
end_date
|
|
||||||
)
|
|
||||||
|
|
||||||
serializer = self.serializer_class(search.search(), many=True)
|
|
||||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
|
||||||
|
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
def create(self, request, *args, **kwargs):
|
def create(self, request, *args, **kwargs):
|
||||||
@@ -99,13 +90,15 @@ class QuotaDistributionViewSet(viewsets.ModelViewSet):
|
|||||||
)
|
)
|
||||||
def my_distributions(self, request):
|
def my_distributions(self, request):
|
||||||
""" list of my distributions """
|
""" list of my distributions """
|
||||||
query = self.request.query_params
|
|
||||||
|
queryset = self.filter_queryset(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
|
||||||
if query.get('param') == 'assigned':
|
if query.get('param') == 'assigned':
|
||||||
# paginate queryset
|
# paginate queryset
|
||||||
page = self.paginate_queryset(
|
page = self.paginate_queryset(
|
||||||
self.queryset.filter(
|
queryset.filter(
|
||||||
Q(assigned_organization=organization)
|
Q(assigned_organization=organization)
|
||||||
).order_by('-modify_date')
|
).order_by('-modify_date')
|
||||||
)
|
)
|
||||||
@@ -113,7 +106,7 @@ class QuotaDistributionViewSet(viewsets.ModelViewSet):
|
|||||||
elif query.get('param') == 'assigner':
|
elif query.get('param') == 'assigner':
|
||||||
# paginate queryset
|
# paginate queryset
|
||||||
page = self.paginate_queryset(
|
page = self.paginate_queryset(
|
||||||
self.queryset.filter(
|
queryset.filter(
|
||||||
Q(assigner_organization=organization)
|
Q(assigner_organization=organization)
|
||||||
).order_by('-modify_date')
|
).order_by('-modify_date')
|
||||||
)
|
)
|
||||||
@@ -121,7 +114,7 @@ class QuotaDistributionViewSet(viewsets.ModelViewSet):
|
|||||||
elif query.get('param') == 'all':
|
elif query.get('param') == 'all':
|
||||||
# paginate queryset
|
# paginate queryset
|
||||||
page = self.paginate_queryset(
|
page = self.paginate_queryset(
|
||||||
self.queryset.filter(
|
queryset.filter(
|
||||||
Q(assigner_organization=organization) |
|
Q(assigner_organization=organization) |
|
||||||
Q(assigned_organization=organization)
|
Q(assigned_organization=organization)
|
||||||
).order_by('-modify_date')
|
).order_by('-modify_date')
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ class UserRelationDocument(Document):
|
|||||||
|
|
||||||
# In different parts of the code different fields are used. There are
|
# In different parts of the code different fields are used. There are
|
||||||
# a couple of use cases: (1) more-like-this functionality, where `title`,
|
# a couple of use cases: (1) more-like-this functionality, where `title`,
|
||||||
# `description` and `summary` fields are used, (2) search and filtering
|
# `description` and `summary` fields are used, (2) filter and filtering
|
||||||
# functionality where all the fields are used.
|
# functionality where all the fields are used.
|
||||||
|
|
||||||
user = fields.ObjectField(properties={
|
user = fields.ObjectField(properties={
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
from django.db.models import Q
|
|
||||||
from apps.warehouse.models import InventoryEntry
|
|
||||||
from django.db.models.functions import TruncDate
|
|
||||||
|
|
||||||
|
|
||||||
class InventoryEntrySearch:
|
|
||||||
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 = InventoryEntry.objects.all()
|
|
||||||
|
|
||||||
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(distribution__distribution_id__icontains=keyword)
|
|
||||||
combined_q |= Q(organization__name__icontains=keyword)
|
|
||||||
combined_q |= Q(weight__icontains=keyword)
|
|
||||||
combined_q |= Q(balance__icontains=keyword)
|
|
||||||
combined_q |= Q(lading_number__icontains=keyword)
|
|
||||||
combined_q |= Q(is_confirmed__icontains=keyword)
|
|
||||||
|
|
||||||
queryset = queryset.filter(combined_q)
|
|
||||||
|
|
||||||
return queryset.distinct()
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
from apps.warehouse.web.api.v1 import serializers as warehouse_serializers
|
from apps.warehouse.web.api.v1 import serializers as warehouse_serializers
|
||||||
from apps.warehouse.services.search.inventory_search import InventoryEntrySearch
|
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||||
from apps.warehouse import models as warehouse_models
|
from apps.warehouse import models as warehouse_models
|
||||||
from common.helpers import get_organization_by_user
|
from common.helpers import get_organization_by_user
|
||||||
from common.generics import base64_to_image_file
|
from common.generics import base64_to_image_file
|
||||||
@@ -12,11 +12,19 @@ from rest_framework import status
|
|||||||
import typing
|
import typing
|
||||||
|
|
||||||
|
|
||||||
class InventoryEntryViewSet(viewsets.ModelViewSet):
|
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",
|
||||||
|
"organization__name",
|
||||||
|
"weight",
|
||||||
|
"balance",
|
||||||
|
"lading_number",
|
||||||
|
"is_confirmed",
|
||||||
|
]
|
||||||
|
date_field = "create_date"
|
||||||
|
|
||||||
def upload_confirmation_document(self, request, inventory: int) -> typing.Any:
|
def upload_confirmation_document(self, request, inventory: int) -> typing.Any:
|
||||||
""" upload document for inventory entry confirmation """
|
""" upload document for inventory entry confirmation """
|
||||||
@@ -40,24 +48,6 @@ class InventoryEntryViewSet(viewsets.ModelViewSet):
|
|||||||
inventory.save()
|
inventory.save()
|
||||||
return Response(status=status.HTTP_200_OK)
|
return Response(status=status.HTTP_200_OK)
|
||||||
|
|
||||||
@transaction.atomic
|
|
||||||
def list(self, request, *args, **kwargs):
|
|
||||||
""" search & filter by date range or return all objects """
|
|
||||||
|
|
||||||
query_params = self.request.query_params
|
|
||||||
query = query_params.get('search')
|
|
||||||
start_date = query_params.get('start')
|
|
||||||
end_date = query_params.get('end')
|
|
||||||
|
|
||||||
search = InventoryEntrySearch(
|
|
||||||
query,
|
|
||||||
start_date,
|
|
||||||
end_date
|
|
||||||
)
|
|
||||||
|
|
||||||
serializer = self.serializer_class(search.search(), many=True)
|
|
||||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
|
||||||
|
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
def create(self, request, *args, **kwargs):
|
def create(self, request, *args, **kwargs):
|
||||||
""" custom create of inventory entry """
|
""" custom create of inventory entry """
|
||||||
@@ -101,7 +91,8 @@ class InventoryEntryViewSet(viewsets.ModelViewSet):
|
|||||||
def my_inventory_entries(self, request):
|
def my_inventory_entries(self, request):
|
||||||
""" list of my inventory entries """
|
""" list of my inventory entries """
|
||||||
|
|
||||||
entries = self.queryset.filter(organization=get_organization_by_user(request.user))
|
queryset = self.filter_queryset(self.queryset) # return by search param or all objects
|
||||||
|
entries = queryset.filter(organization=get_organization_by_user(request.user))
|
||||||
|
|
||||||
# paginate & response
|
# paginate & response
|
||||||
page = self.paginate_queryset(entries)
|
page = self.paginate_queryset(entries)
|
||||||
|
|||||||
Reference in New Issue
Block a user