some apis of pos

This commit is contained in:
2025-08-20 14:44:43 +03:30
parent 14cd349a7d
commit 20e4bfad75
12 changed files with 316 additions and 29 deletions

View File

@@ -1,6 +1,11 @@
from apps.herd.web.api.v1.serializers import HerdSerializer, RancherSerializer
from apps.core.mixins.search_mixin import DynamicSearchMixin
from apps.livestock.web.api.v1.serializers import LiveStockSerializer
from apps.herd.services.services import (
get_rancher_statistics,
rancher_quota_weight
)
from apps.warehouse.models import InventoryEntry
from apps.core.mixins.search_mixin import DynamicSearchMixin
from rest_framework.exceptions import APIException
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
@@ -110,8 +115,9 @@ class HerdViewSet(viewsets.ModelViewSet):
class RancherViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
queryset = Rancher.objects.all()
queryset = Rancher.objects.all() # noqa
serializer_class = RancherSerializer
permission_classes = [AllowAny]
search_fields = [
"ranching_farm",
"first_name",
@@ -125,30 +131,28 @@ class RancherViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
"city__name",
]
def list(self, request, *args, **kwargs):
""" list of ranchers """
search = self.filter_query(self.queryset.order_by('-modify_date')) # search & filter
page = self.paginate_queryset(search)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
@action(
methods=['get'],
detail=True,
url_path='herds',
url_name='herds',
name='herds'
methods=['post'],
detail=False,
url_name='check_national_code',
url_path='check_national_code',
name='check_national_code'
)
def herds_by_rancher(self, request, pk=None):
""" list of rancher herds """
@transaction.atomic
def check_national_code(self, request):
""" check national code & existence of rancher """
rancher = self.get_object()
queryset = rancher.herd.all().order_by('-modify_date') # get rancher herds
rancher = self.queryset.filter(national_code=request.data['national_code'])
inventory = InventoryEntry.objects.get(id=43)
# paginate queryset
page = self.paginate_queryset(queryset)
if page is not None:
serializer = HerdSerializer(page, many=True)
return self.get_paginated_response(serializer.data)
# get rancher live stocks information ant total quota for rancher
rancher_quota_by_live_stock = rancher_quota_weight(rancher.first(), inventory)
if rancher.exists():
serializer = self.serializer_class(rancher.first())
return Response(serializer.data, status=status.HTTP_200_OK)
else:
return Response({
"message": "rancher has not existence"
}, status=status.HTTP_204_NO_CONTENT)