from django.db.models.signals import post_save, post_delete, m2m_changed from django.dispatch import receiver from utils.cache import invalidate_cache_patterns from .models import * # 1. Define all models that affect the list # If a Category title changes, the list must update. # If a Status changes, the list must update. TARGET_MODELS = [ Hadis, HadisCategory, HadisCollection, HadisCorrection, HadisInCollection, HadisReference, HadisSect, HadisStatus, HadisTag, HadisTransmitter, Transmitters, TransmitterOpinion, TransmitterReliability, TransmitterOriginalText, ReferenceImage, BookReference, BookAttribute, BookAuthor, BookReferenceImage, NarratorLayer, OpinionStatus, ContentRelease, CorrectionReference, BookEdition, BookVolume, CorrectionReferenceImage ] def invalidate_hadis_cache(): """ Deletes all Redis cache keys matching '*hadis_api*'. """ return invalidate_cache_patterns("*hadis_api*") @receiver(post_save) @receiver(post_delete) def clear_hadis_cache(sender, instance, **kwargs): """ Clears the API cache whenever a Hadith or related model is saved/deleted. """ if sender in TARGET_MODELS: invalidate_hadis_cache() @receiver(m2m_changed) def clear_hadis_cache_on_m2m(sender, instance, action, **kwargs): if action in ('post_add', 'post_remove', 'post_clear'): if isinstance(instance, tuple(TARGET_MODELS)): invalidate_hadis_cache()