fix - custom APIExceptions for quota stats
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
from django.apps import apps
|
||||
from django.core.exceptions import ValidationError
|
||||
from rest_framework.exceptions import APIException
|
||||
from rest_framework import status
|
||||
|
||||
from apps.product.exceptions import DistributionException
|
||||
|
||||
|
||||
def get_model(app_label, model_name):
|
||||
@@ -15,28 +17,41 @@ class QuotaStatsValidator:
|
||||
@staticmethod
|
||||
def _get_stat(quota, organization):
|
||||
organization_quota_stats = get_model("product", "OrganizationQuotaStats")
|
||||
return organization_quota_stats.objects.filter(quota=quota, organization=organization).first()
|
||||
return organization_quota_stats.objects.filter(quota=quota, organization=organization).first() # noqa
|
||||
|
||||
@staticmethod
|
||||
def validate_assigner_has_enough(assigner_org, quota, amount, allow_zero=False):
|
||||
def validate_assigner_has_enough(assigner_org, quota, amount, allow_zero=False, update_operation=None):
|
||||
"""
|
||||
if organization has enough remaining weight
|
||||
"""
|
||||
stat = QuotaStatsValidator._get_stat(quota, assigner_org)
|
||||
if not stat:
|
||||
raise APIException(f"Organization {assigner_org} has no quota record for quota {quota}.")
|
||||
raise DistributionException(
|
||||
f"سازمان {assigner_org.name} هیچ مدل میانی برای این سهمیه {quota} ندارد .", # noqa
|
||||
status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
|
||||
remaining = getattr(stat, "remaining_amount", None)
|
||||
if remaining is None:
|
||||
# fallback to quota.remaining_weight subtraction from totals
|
||||
raise APIException("remaining_amount field missing in OrganizationQuotaStats.")
|
||||
raise DistributionException(
|
||||
"فیلد باقی مانده در مدل میانی وجود ندارد", # noqa
|
||||
status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
|
||||
if amount < 0:
|
||||
raise APIException("Amount must be non-negative.")
|
||||
raise DistributionException("مقدار وزن نباید منفی باشد", status.HTTP_403_FORBIDDEN) # noqa
|
||||
|
||||
if remaining < amount and not allow_zero:
|
||||
raise APIException(
|
||||
f"Assigning {amount} exceeds remaining amount {remaining} for organization {assigner_org}."
|
||||
if update_operation:
|
||||
raise DistributionException(
|
||||
"مقدار وزن ویرایش شده از وزن باقیمانده بیشتر است", # noqa
|
||||
status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
raise DistributionException(
|
||||
f"تخصیص مقدار {amount} بیشتر از مقدار باقیمانده {remaining} برای سازمان {assigner_org.name} میباشد.",
|
||||
# noqa
|
||||
status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
|
||||
Reference in New Issue
Block a user