|
|
@ -0,0 +1,33 @@ |
|
|
|
|
|
# hadith_app/signals.py |
|
|
|
|
|
|
|
|
|
|
|
from django.db.models.signals import post_save, post_delete |
|
|
|
|
|
from django.dispatch import receiver |
|
|
|
|
|
from django.core.cache import cache |
|
|
|
|
|
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 ] |
|
|
|
|
|
|
|
|
|
|
|
@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: |
|
|
|
|
|
# This is the magic command from django-redis |
|
|
|
|
|
# It finds ALL keys starting with the prefix and deletes them |
|
|
|
|
|
# *:1: is the default django version prefix |
|
|
|
|
|
try: |
|
|
|
|
|
# Delete any key that contains our prefix "hadis_api" |
|
|
|
|
|
# The pattern "*hadis_api*" ensures we catch all variations (headers, pages, etc) |
|
|
|
|
|
cache.delete_pattern("*hadis_api*") |
|
|
|
|
|
print(f"Cache cleared for {sender.__name__} update!") |
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
# Fail silently or log error, don't crash the save transaction |
|
|
|
|
|
print(f"Cache clear failed: {e}") |