Browse Source
feat(hadis): return raw translations in sync api and invalidate cache on version update
feat(hadis): return raw translations in sync api and invalidate cache on version update
- Return raw, unlocalized translation list in HadisSyncSerializer for Flutter offline multi-language support - Enable hadis signals in HadisConfig.ready() to load cache invalidation listeners - Add ContentRelease to TARGET_MODELS and implement invalidate_hadis_cache helper - Trigger explicit cache invalidation in AdminContentReleaseViewSet upon version increment and CRUD - Update HadisSyncView swagger documentation examplemaster
5 changed files with 77 additions and 39 deletions
-
6apps/hadis/apps.py
-
7apps/hadis/docs.py
-
7apps/hadis/serializers/hadis.py
-
77apps/hadis/signals.py
-
19apps/hadis/views_admin.py
@ -1,33 +1,44 @@ |
|||||
# 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}") |
|
||||
|
# 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, ContentRelease |
||||
|
] |
||||
|
|
||||
|
|
||||
|
def invalidate_hadis_cache(): |
||||
|
""" |
||||
|
Deletes all Redis cache keys matching '*hadis_api*'. |
||||
|
Falls back to cache.clear() if cache backend doesn't support delete_pattern. |
||||
|
""" |
||||
|
try: |
||||
|
if hasattr(cache, 'delete_pattern'): |
||||
|
deleted = cache.delete_pattern("*hadis_api*") |
||||
|
print(f"Cache cleared for hadis_api (deleted {deleted} keys)!") |
||||
|
else: |
||||
|
cache.clear() |
||||
|
print("Cache cleared for hadis_api (fallback clear)!") |
||||
|
except Exception as e: |
||||
|
# Fail silently or log error, don't crash the transaction |
||||
|
print(f"Cache clear failed: {e}") |
||||
|
|
||||
|
|
||||
|
@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() |
||||
|
print(f"Cache cleared for {sender.__name__} update!") |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue