2025-06-16 10:44:12 +03:30
|
|
|
from django.db.models import Sum
|
|
|
|
|
from django.db.models.signals import post_save, post_delete
|
|
|
|
|
from django.dispatch import receiver
|
|
|
|
|
from .models import QuotaDistribution, Quota
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def recalculate_remaining_amount(quota):
|
2025-06-16 12:05:32 +03:30
|
|
|
total_distributed = quota.distributions_assigned.aggregate(
|
|
|
|
|
total=Sum('weight')
|
2025-06-16 10:44:12 +03:30
|
|
|
)['total'] or 0
|
|
|
|
|
|
2025-06-16 12:05:32 +03:30
|
|
|
quota.remaining_weight = quota.quota_weight - total_distributed
|
2025-07-02 15:42:51 +03:30
|
|
|
quota.quota_distributed = total_distributed
|
|
|
|
|
quota.save(update_fields=["remaining_weight", "quota_distributed"])
|
2025-06-16 10:44:12 +03:30
|
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=QuotaDistribution)
|
|
|
|
|
@receiver(post_delete, sender=QuotaDistribution)
|
|
|
|
|
def update_quota_remaining(sender, instance, **kwargs):
|
|
|
|
|
recalculate_remaining_amount(instance.quota)
|