add - import validator for organization quota stats
This commit is contained in:
@@ -1,15 +1,23 @@
|
||||
from apps.product.models import QuotaDistribution, OrganizationQuotaStats
|
||||
from apps.product.validators.quota_stats_validator import QuotaStatsValidator
|
||||
|
||||
|
||||
class QuotaStatsService:
|
||||
@staticmethod
|
||||
def apply_distribution(distribution: QuotaDistribution):
|
||||
quota = distribution.quota
|
||||
|
||||
print("ssss")
|
||||
# origin org
|
||||
assigner = distribution.assigner_organization
|
||||
# destination org
|
||||
assigned = distribution.assigned_organization
|
||||
|
||||
QuotaStatsValidator.validate_assigner_has_enough(
|
||||
assigner_org=assigner,
|
||||
quota=quota,
|
||||
amount=distribution.weight
|
||||
)
|
||||
|
||||
# ================ origin ================
|
||||
assigner_stat, created = OrganizationQuotaStats.objects.get_or_create(
|
||||
organization=assigner,
|
||||
@@ -46,6 +54,14 @@ class QuotaStatsService:
|
||||
assigner = distribution.assigner_organization
|
||||
assigned = distribution.assigned_organization
|
||||
|
||||
QuotaStatsValidator.validate_distribution_update(
|
||||
assigner_org=assigner,
|
||||
assigned_org=assigned,
|
||||
quota=quota,
|
||||
old_amount=old_weight,
|
||||
new_amount=distribution.weight
|
||||
)
|
||||
|
||||
assigner_stat = OrganizationQuotaStats.objects.get(
|
||||
organization=assigner,
|
||||
quota=quota
|
||||
@@ -57,13 +73,9 @@ class QuotaStatsService:
|
||||
if assigner_stat.stat_type == 'distribution':
|
||||
# if diff > 0 it is added to destination
|
||||
assigner_stat.remaining_amount -= diff
|
||||
print("distributed : ", assigner_stat.total_distributed)
|
||||
assigner_stat.total_distributed += diff
|
||||
print(assigner_stat.total_distributed, diff)
|
||||
assigner_stat.save()
|
||||
print(assigned_stat.total_amount)
|
||||
assigned_stat.total_amount += diff
|
||||
print(assigned_stat.total_amount)
|
||||
assigned_stat.remaining_amount += diff
|
||||
assigned_stat.save()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user