diff --git a/apps/product/models.py b/apps/product/models.py index 31de30a..630804c 100644 --- a/apps/product/models.py +++ b/apps/product/models.py @@ -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, diff --git a/apps/product/services.py b/apps/product/services.py index e69de29..0aabb24 100644 --- a/apps/product/services.py +++ b/apps/product/services.py @@ -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)) + diff --git a/apps/product/web/api/v1/product_api.py b/apps/product/web/api/v1/product_api.py index 9375363..0e8afc6 100644 --- a/apps/product/web/api/v1/product_api.py +++ b/apps/product/web/api/v1/product_api.py @@ -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'], diff --git a/apps/product/web/api/v1/product_serializers.py b/apps/product/web/api/v1/product_serializers.py index 5898bd1..2505e6f 100644 --- a/apps/product/web/api/v1/product_serializers.py +++ b/apps/product/web/api/v1/product_serializers.py @@ -78,6 +78,11 @@ class BrokerSerializer(serializers.ModelSerializer): instance.organization ).data + if instance.product: + representation['product'] = ProductSerializer( + instance.product + ).data + return representation diff --git a/common/helpers.py b/common/helpers.py index a3ddb58..4cf4468 100644 --- a/common/helpers.py +++ b/common/helpers.py @@ -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