Files
RasadDam_Backend/apps/product/signals.py

21 lines
722 B
Python
Raw Normal View History

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')
)['total'] or 0
2025-06-16 12:05:32 +03:30
quota.remaining_weight = quota.quota_weight - total_distributed
quota.quota_distributed = total_distributed
quota.save(update_fields=["remaining_weight", "quota_distributed"])
@receiver(post_save, sender=QuotaDistribution)
@receiver(post_delete, sender=QuotaDistribution)
def update_quota_remaining(sender, instance, **kwargs):
recalculate_remaining_amount(instance.quota)