add - new changes on OrganizationQuotaStat & calculative signals on quota/distribution
This commit is contained in:
@@ -243,43 +243,24 @@ def update_quota_stats(instance: Quota):
|
||||
])
|
||||
|
||||
|
||||
def organization_quota_stats(quota: Quota, distribution: QuotaDistribution = None):
|
||||
@receiver(post_save, sender=Quota)
|
||||
def organization_quota_stats(sender, quota: Quota, created: bool, **kwargs):
|
||||
"""
|
||||
set total received distributions for every organization
|
||||
"""
|
||||
if distribution:
|
||||
org_quota_stat, created = OrganizationQuotaStats.objects.get_or_create(
|
||||
quota=quota,
|
||||
organization=distribution.assigned_organization,
|
||||
)
|
||||
org_quota_stat, created = OrganizationQuotaStats.objects.get_or_create(
|
||||
quota=quota,
|
||||
organization=quota.registerer_organization,
|
||||
)
|
||||
org_quota_stat.total_amount = quota.quota_weight
|
||||
org_quota_stat.save(update_fields=['total_amount'])
|
||||
|
||||
# delete distribution
|
||||
# decrease org stat total amount after remove distribution
|
||||
if distribution.trash:
|
||||
org_quota_stat.total_amount -= distribution.weight
|
||||
org_quota_stat.save(update_fields=["total_amount"])
|
||||
else:
|
||||
# if stat was created before or total amount is 0
|
||||
if not created or org_quota_stat.total_amount == 0:
|
||||
org_quota_stat.total_amount += distribution.weight
|
||||
org_quota_stat.save(update_fields=["total_amount"])
|
||||
org_quota_stat.distributions.add(distribution)
|
||||
# delete quota
|
||||
if quota.trash:
|
||||
org_quota_stat.soft_delete()
|
||||
|
||||
# prevent from maximum recursion loop
|
||||
distribution.stat_from_signal = True
|
||||
else:
|
||||
org_quota_stat, created = OrganizationQuotaStats.objects.get_or_create(
|
||||
quota=quota,
|
||||
organization=quota.registerer_organization,
|
||||
total_amount=quota.quota_weight
|
||||
)
|
||||
|
||||
# delete quota
|
||||
if quota.trash:
|
||||
org_quota_stat.soft_delete()
|
||||
|
||||
# prevent from maximum recursion loop
|
||||
quota.stat_from_signal = True
|
||||
# prevent from maximum recursion loop
|
||||
quota.stat_from_signal = True
|
||||
|
||||
|
||||
@receiver([post_save, post_delete], sender=QuotaDistribution)
|
||||
@@ -293,8 +274,6 @@ def update_stats_on_change(sender, instance, **kwargs):
|
||||
if getattr(instance, 'stat_from_signal', False):
|
||||
return
|
||||
|
||||
organization_quota_stats(instance.quota, instance)
|
||||
|
||||
|
||||
@receiver([post_save, post_delete], sender=Quota)
|
||||
def update_quota_stats_on_change(sender, instance, **kwargs):
|
||||
@@ -304,3 +283,45 @@ def update_quota_stats_on_change(sender, instance, **kwargs):
|
||||
return
|
||||
|
||||
organization_quota_stats(instance)
|
||||
|
||||
|
||||
@receiver(post_save, sender=QuotaDistribution)
|
||||
def update_quota_stats_on_distribution(sender, instance: QuotaDistribution, created, **kwargs):
|
||||
if getattr(instance, 'one_time_loop_flag', False):
|
||||
return
|
||||
|
||||
if instance.trash:
|
||||
return
|
||||
|
||||
org = instance.assigned_organization
|
||||
quota = instance.quota
|
||||
|
||||
stats, _ = OrganizationQuotaStats.objects.get_or_create(
|
||||
quota=quota,
|
||||
organization=org,
|
||||
)
|
||||
stats.distributions.add(instance)
|
||||
stats.update_amount()
|
||||
instance.one_time_loop_flag = True
|
||||
|
||||
|
||||
@receiver(post_save, sender=QuotaDistribution)
|
||||
def handle_quota_stats_soft_delete_on_distribution(sender, instance: QuotaDistribution, created, **kwargs):
|
||||
if getattr(instance, 'one_time_loop_flag', False):
|
||||
return
|
||||
|
||||
if instance.trash:
|
||||
org = instance.assigned_organization
|
||||
quota = instance.quota
|
||||
|
||||
stats_qs = OrganizationQuotaStats.objects.filter(
|
||||
quota=quota,
|
||||
organization=org,
|
||||
)
|
||||
|
||||
if stats_qs:
|
||||
for stats in stats_qs:
|
||||
stats.distributions.remove(instance)
|
||||
stats.update_amount()
|
||||
|
||||
instance.one_time_loop_flag = True
|
||||
|
||||
Reference in New Issue
Block a user