add product serialize to broker - quota statistic by product

This commit is contained in:
2025-07-08 11:59:58 +03:30
parent bafd1fbbcb
commit 90b090e848
5 changed files with 41 additions and 5 deletions

View File

@@ -113,6 +113,7 @@ class Product(BaseModel):
).aggregate(total_entry=models.Sum('warehouse_entry'))['total_entry'] or 0
data = {
'product': self.id,
'quotas_count': quotas_count,
'total_quotas_weight': total_quotas_weight,
'total_remaining_quotas_weight': total_remaining_quotas_weight,

View File

@@ -0,0 +1,16 @@
from apps.warehouse.models import InventoryEntry
def get_products_in_warehouse(organization_id):
""" get lis of products from organization warehouse """
entries = InventoryEntry.objects.select_related(
'distribution__quota__product'
)
product_objects = [
entry.distribution.quota.product for entry in entries
]
return list(set(product_objects))

View File

@@ -2,6 +2,8 @@ import datetime
from apps.product.web.api.v1 import product_serializers as product_serializers
from apps.product.exceptions import QuotaExpiredTimeException
from apps.product.services import get_products_in_warehouse
from common.helpers import get_organization_by_user
from rest_framework.exceptions import APIException
from apps.product import models as product_models
from rest_framework.response import Response
@@ -68,6 +70,8 @@ class ProductViewSet(viewsets.ModelViewSet):
serializer_class = product_serializers.ProductSerializer
def list(self, request, *args, **kwargs):
""" custom list view """ #
queryset = self.filter_queryset(self.get_queryset().order_by('-create_date')) # noqa
page = self.paginate_queryset(queryset)
@@ -87,13 +91,17 @@ class ProductViewSet(viewsets.ModelViewSet):
)
@transaction.atomic
def quotas_statistics_by_product(self, request):
""" quota statistics of each product """
""" quota statistics of each product in organization warehouse """
quotas_statistics = []
for product in self.queryset:
quotas_statistics.append(product.quota_information())
product_data = []
return Response(quotas_statistics, status=status.HTTP_200_OK)
organization = get_organization_by_user(request.user)
products = get_products_in_warehouse(organization.id)
for product in products:
product_data.append(product.quota_information())
return Response(product_data, status=status.HTTP_200_OK)
@action(
methods=['put'],

View File

@@ -78,6 +78,11 @@ class BrokerSerializer(serializers.ModelSerializer):
instance.organization
).data
if instance.product:
representation['product'] = ProductSerializer(
instance.product
).data
return representation

View File

@@ -1,7 +1,13 @@
import typing
from apps.authorization.models import UserRelations
def detect_file_extension(file_name: str) -> typing.AnyStr:
""" detect extension of a file like: jpg, png, pdf """
extended = file_name.split('.')
return extended[1]
def get_organization_by_user(user: object = None) -> typing.Any:
organization = UserRelations.objects.select_related('organization').get(user=user).organization
return organization