from django.core.cache import cache
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver

from .models import Company
from .views import COMPANIES_STATS_CACHE_KEY


@receiver([post_save, post_delete], sender=Company)
def _bust_company_stats_cache(sender, **kwargs):
    cache.delete(COMPANIES_STATS_CACHE_KEY)
    # company change affects contacts stats too (by_company aggregation)
    cache.delete("contacts:stats")
