add field of activity organization - handle quota weight

This commit is contained in:
2025-06-16 09:26:55 +03:30
parent 05b46a82b1
commit 2164c4415b
3 changed files with 9 additions and 12 deletions

View File

@@ -192,7 +192,8 @@ class OrganizationSerializer(serializers.ModelSerializer):
'city', 'city',
'parent_organization', 'parent_organization',
'national_unique_id', 'national_unique_id',
'company_code' 'company_code',
'field_of_activity'
] ]
extra_kwargs = {} extra_kwargs = {}

View File

@@ -5,6 +5,6 @@ from rest_framework import status
class QuotaWeightException(APIException): class QuotaWeightException(APIException):
""" if quota distributions weight is more """ """ if quota distributions weight is more """
status_code = status.HTTP_401_UNAUTHORIZED status_code = status.HTTP_400_BAD_REQUEST
default_detail = '' default_detail = "مقدار وارد شده باعث می‌شود مجموع سهمیه‌ها از مقدار کل سهمیه بیشتر شود." # noqa
default_code = 'unauthorized' default_code = 'error'

View File

@@ -2,7 +2,7 @@ from rest_framework import serializers
from apps.product import models as product_models from apps.product import models as product_models
from apps.product.web.api.v1.product_serializers import QuotaSerializer from apps.product.web.api.v1.product_serializers import QuotaSerializer
from django.db import models from django.db import models
from rest_framework.exceptions import APIException from apps.product.exceptions import QuotaWeightException
from rest_framework import status from rest_framework import status
@@ -20,22 +20,18 @@ class QuotaDistributionSerializer(serializers.ModelSerializer):
""" to validate if distribution weight """ to validate if distribution weight
more than quota weight raise exception """ more than quota weight raise exception """
quota = data['quota'] quota = product_models.Quota.objects.get(id=data['quota'])
amount = data['weight'] amount = data['weight']
instance_id = self.instance.id if self.instance else None instance_id = self.instance.id if self.instance else None
total = product_models.QuotaDistribution.objects.filter( total = product_models.QuotaDistribution.objects.filter(
quota_id=quota quota=quota
).exclude(id=instance_id).aggregate( ).exclude(id=instance_id).aggregate(
total=models.Sum('weight') total=models.Sum('weight')
)['total'] or 0 )['total'] or 0
print(total) print(total)
if total + amount > self.instance.weight: if total + amount > self.instance.weight:
raise APIException( raise QuotaWeightException()
"مقدار وارد شده باعث می‌شود مجموع سهمیه‌ها از مقدار کل سهمیه بیشتر شود.", # noqa
status.HTTP_400_BAD_REQUEST,
)
return data return data