import structlog
from django.core.cache import caches
from django.db.models.signals import post_delete, post_save
from wagtail.models import Page

logger = structlog.get_logger(__name__)


def invalidate_learning_path_cache(sender, **kwargs):
    logger.debug('invalidate learning_path_cache', label='learning_path_cache')
    caches['learning_path_cache'].clear()


for subclass in Page.__subclasses__():
    post_save.connect(invalidate_learning_path_cache, subclass)
    post_delete.connect(invalidate_learning_path_cache, subclass)
