From 699b5ab6fd173557a003cb6df3383fecebf1e5a0 Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Sat, 2 Aug 2025 11:04:46 +0330 Subject: [PATCH] first move for product statistics --- .idea/Rasaddam_Backend.iml | 2 +- .idea/misc.xml | 2 +- apps/product/models.py | 3 +- apps/product/web/api/v1/urls.py | 1 + .../web/api/v1/viewsets/product_api.py | 32 +++++++++++++++++++ apps/product/web/api/v1/viewsets/quota_api.py | 6 ++-- 6 files changed, 40 insertions(+), 6 deletions(-) diff --git a/.idea/Rasaddam_Backend.iml b/.idea/Rasaddam_Backend.iml index c5d6090..168bde0 100644 --- a/.idea/Rasaddam_Backend.iml +++ b/.idea/Rasaddam_Backend.iml @@ -14,7 +14,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 29f5506..296aa57 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/apps/product/models.py b/apps/product/models.py index ee6c3b4..e6aa7f0 100644 --- a/apps/product/models.py +++ b/apps/product/models.py @@ -114,7 +114,8 @@ class Product(BaseModel): ).aggregate(total_entry=models.Sum('warehouse_entry'))['total_entry'] or 0 data = { - 'product': self.id, + 'product_id': self.id, + 'product_name': self.name, 'quotas_count': quotas_count, 'total_quotas_weight': total_quotas_weight, 'total_remaining_quotas_weight': total_remaining_quotas_weight, diff --git a/apps/product/web/api/v1/urls.py b/apps/product/web/api/v1/urls.py index d9f2dcc..8d800f8 100644 --- a/apps/product/web/api/v1/urls.py +++ b/apps/product/web/api/v1/urls.py @@ -14,6 +14,7 @@ router.register(r'attribute_value', product_api.AttributeValueViewSet, basename= router.register(r'broker', product_api.BrokerViewSet, basename='broker') router.register(r'sale_unit', product_api.SaleUnitViewSet, basename='sale_unit') router.register(r'incentive_plan', product_api.IncentivePlanViewSet, basename='incentive_plan') +router.register(r'stats', product_api.ProductStatsViewSet, basename='stats') router.register(r'quota', quota_api.QuotaViewSet, basename='quota') router.register(r'quota_distribution', distribution_apis.QuotaDistributionViewSet, basename='quota_distribution') diff --git a/apps/product/web/api/v1/viewsets/product_api.py b/apps/product/web/api/v1/viewsets/product_api.py index b1db6f6..dc0eaf0 100644 --- a/apps/product/web/api/v1/viewsets/product_api.py +++ b/apps/product/web/api/v1/viewsets/product_api.py @@ -1,5 +1,6 @@ import datetime from apps.product.web.api.v1.serializers import product_serializers as product_serializers +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 @@ -115,6 +116,37 @@ class ProductViewSet(viewsets.ModelViewSet): return Response(e, status=status.HTTP_204_NO_CONTENT) +class ProductStatsViewSet(viewsets.ModelViewSet): + """ product statistics by its quotas """ + + queryset = product_models.ProductStats.objects.all() + serializer_class = product_serializers.ProductStatsSerializer + + @action( + methods=['get'], + detail=False, + url_path='my_products_stat', + url_name='my_products_stat', + name='my_products_stat' + ) + def my_products_stat(self, request): + """ my organization products with statistics """ + + try: + organization = get_organization_by_user(request.user) + product_stats = self.queryset.filter(organization=organization) + + page = self.paginate_queryset(product_stats) # noqa + if page is not None: + serializer = self.get_serializer(page, many=True) + return self.get_paginated_response(serializer.data) + + serializer = self.serializer_class(product_stats, many=True) + return Response(serializer.data, status=status.HTTP_200_OK) + except APIException as e: + raise e + + class AttributeViewSet(viewsets.ModelViewSet): """ attributes of reference product """ # diff --git a/apps/product/web/api/v1/viewsets/quota_api.py b/apps/product/web/api/v1/viewsets/quota_api.py index 07518a5..83061d9 100644 --- a/apps/product/web/api/v1/viewsets/quota_api.py +++ b/apps/product/web/api/v1/viewsets/quota_api.py @@ -382,9 +382,9 @@ class QuotaViewSet(viewsets.ModelViewSet, DynamicSearchMixin): # noqa @action( methods=['get'], detail=False, - url_path='quotas_statistics', - url_name='quotas_statistics', - name='quotas_statistics' + url_path='statistics_by_product', + url_name='statistics_by_product', + name='statistics_by_product' ) @transaction.atomic def quotas_statistics_by_product(self, request):