add company code to organization serialzier & handle quota weight exception
This commit is contained in:
10
apps/product/exceptions.py
Normal file
10
apps/product/exceptions.py
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
from rest_framework.exceptions import APIException
|
||||||
|
from rest_framework import status
|
||||||
|
|
||||||
|
|
||||||
|
class QuotaWeightException(APIException):
|
||||||
|
""" if quota distributions weight is more """
|
||||||
|
|
||||||
|
status_code = status.HTTP_401_UNAUTHORIZED
|
||||||
|
default_detail = ''
|
||||||
|
default_code = 'unauthorized'
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
from rest_framework import serializers
|
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 rest_framework.exceptions import APIException
|
||||||
|
from rest_framework import status
|
||||||
|
|
||||||
|
|
||||||
class QuotaDistributionSerializer(serializers.ModelSerializer):
|
class QuotaDistributionSerializer(serializers.ModelSerializer):
|
||||||
@@ -13,6 +16,29 @@ class QuotaDistributionSerializer(serializers.ModelSerializer):
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def validate(self, data):
|
||||||
|
""" to validate if distribution weight
|
||||||
|
more than quota weight raise exception """
|
||||||
|
|
||||||
|
quota = data['quota']
|
||||||
|
amount = data['weight']
|
||||||
|
instance_id = self.instance.id if self.instance else None
|
||||||
|
|
||||||
|
total = product_models.QuotaDistribution.objects.filter(
|
||||||
|
quota_id=quota
|
||||||
|
).exclude(id=instance_id).aggregate(
|
||||||
|
total=models.Sum('weight')
|
||||||
|
)['total'] or 0
|
||||||
|
print(total)
|
||||||
|
if total + amount > self.instance.weight:
|
||||||
|
raise APIException(
|
||||||
|
"مقدار وارد شده باعث میشود مجموع سهمیهها از مقدار کل سهمیه بیشتر شود.", # noqa
|
||||||
|
status.HTTP_400_BAD_REQUEST,
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
def to_representation(self, instance):
|
def to_representation(self, instance):
|
||||||
""" Custom output of serializer """
|
""" Custom output of serializer """
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user