@ -455,8 +455,12 @@ class HadisListSerializer(serializers.ModelSerializer):
def get_bookmark ( self , obj ) :
""" Get bookmark information for this playlist. """
if ' user_bookmarked_hadis_ids ' in self . context :
return obj . id in self . context [ ' user_bookmarked_hadis_ids ' ]
request = self . context . get ( ' request ' )
user = request . user if request else None
user = request . user if request else None
if not user or not getattr ( user , ' is_authenticated ' , False ) :
return False
book_mark = BookmarkStatusSerializer . get_bookmark_info (
obj = obj ,
user = user ,
@ -1424,66 +1428,11 @@ class HadisReferenceSerializer(serializers.ModelSerializer):
' address ' , ' url ' , ' open_book_url ' , ' citation_summary ' , ' images ' , ' images_count '
]
def get_edition_info ( self , obj ) :
edition = obj . edition
if not edition and getattr ( obj , " book_volume " , None ) and obj . book_volume . edition :
edition = obj . book_volume . edition
if not edition and getattr ( obj , " book_reference " , None ) and hasattr ( obj . book_reference , " editions " ) :
edition = obj . book_reference . editions . first ( )
if edition :
request = self . context . get ( " request " )
pub_src = edition . publisher_item . title if ( getattr ( edition , " publisher_item_id " , None ) and edition . publisher_item and edition . publisher_item . title ) else edition . publisher
pub = get_localized_text ( pub_src , request ) if isinstance ( pub_src , list ) else pub_src
ed_title = get_localized_text ( edition . title , request ) if isinstance ( getattr ( edition , " title " , None ) , list ) else getattr ( edition , " title " , None )
city_src = (
edition . city_of_publication_city . title
if ( getattr ( edition , " city_of_publication_city_id " , None ) and edition . city_of_publication_city and edition . city_of_publication_city . title )
else getattr ( edition , " city_of_publication " , None )
)
country_src = (
edition . country_of_publication_country . title
if ( getattr ( edition , " country_of_publication_country_id " , None ) and edition . country_of_publication_country and edition . country_of_publication_country . title )
else getattr ( edition , " country_of_publication " , None )
)
city = get_localized_text ( city_src , request ) if city_src else " "
country = get_localized_text ( country_src , request ) if country_src else " "
vol_count = edition . volumes . count ( ) if hasattr ( edition , " volumes " ) else 0
num_vols = vol_count if vol_count > 0 else edition . number_of_volumes
def _serialize_contributor ( c ) :
name = get_localized_text ( c . name , request ) if isinstance ( getattr ( c , " name " , None ) , list ) else ( getattr ( c , " name " , None ) or " " )
author_obj = getattr ( c , " author " , None )
author_data = None
if author_obj :
author_data = {
" id " : author_obj . id ,
" slug " : author_obj . slug ,
" name " : get_localized_text ( author_obj . name , request ) if isinstance ( author_obj . name , list ) else ( author_obj . name or " " ) ,
}
return {
" id " : c . id ,
" name " : name ,
" slug " : author_obj . slug if ( author_obj and author_obj . slug ) else getattr ( c , " slug " , None ) ,
" author_id " : getattr ( c , " author_id " , None ) ,
" author " : author_data ,
}
researchers = [ _serialize_contributor ( r ) for r in edition . researchers . all ( ) ] if hasattr ( edition , " researchers " ) else [ ]
editors = [ _serialize_contributor ( e ) for e in edition . editors . all ( ) ] if hasattr ( edition , " editors " ) else [ ]
def _get_resolved_edition ( self , obj ) :
return _resolve_reference_edition_fast ( obj )
return {
" id " : edition . id ,
" title " : ed_title or " " ,
" edition_number " : edition . edition_number ,
" publisher " : pub or " " ,
" publication_place " : { " city " : city or " " , " country " : country or " " } ,
" year_of_publication " : edition . year_of_publication or " " ,
" number_of_volumes " : num_vols ,
" source_url " : getattr ( edition , " source_url " , None ) or " " ,
" researchers " : researchers ,
" editors " : editors ,
}
return None
def get_edition_info ( self , obj ) :
return _build_edition_info_dict ( obj , self . _get_resolved_edition ( obj ) , self . context . get ( " request " ) )
def get_volume_info ( self , obj ) :
if obj . book_volume :
@ -1544,11 +1493,7 @@ class HadisReferenceSerializer(serializers.ModelSerializer):
return absolute_https_url ( obj . book_volume . file . url , request ) if request else absolute_https_url ( obj . book_volume . file . url )
if getattr ( obj , ' url ' , None ) :
return obj . url
edition = obj . edition
if not edition and getattr ( obj , ' book_volume ' , None ) and obj . book_volume . edition :
edition = obj . book_volume . edition
if not edition and getattr ( obj , ' book_reference ' , None ) and hasattr ( obj . book_reference , ' editions ' ) :
edition = obj . book_reference . editions . first ( )
edition = self . _get_resolved_edition ( obj )
if edition and getattr ( edition , ' source_url ' , None ) :
return edition . source_url
return None
@ -1556,11 +1501,7 @@ class HadisReferenceSerializer(serializers.ModelSerializer):
def get_book ( self , obj ) :
if obj . book_reference :
request = self . context . get ( ' request ' )
first_ed = obj . edition
if not first_ed and getattr ( obj , " book_volume " , None ) and obj . book_volume . edition :
first_ed = obj . book_volume . edition
if not first_ed and hasattr ( obj . book_reference , " editions " ) :
first_ed = obj . book_reference . editions . first ( )
first_ed = self . _get_resolved_edition ( obj )
pub = getattr ( obj . book_reference , " publisher " , None )
if pub is None and first_ed :
@ -1746,8 +1687,12 @@ class HadisCorrectionSerializer(serializers.ModelSerializer):
def get_bookmark ( self , obj ) :
""" Get bookmark information for this correction. """
if ' user_bookmarked_correction_ids ' in self . context :
return obj . id in self . context [ ' user_bookmarked_correction_ids ' ]
request = self . context . get ( ' request ' )
user = request . user if request else None
if not user or not getattr ( user , ' is_authenticated ' , False ) :
return False
book_mark = BookmarkStatusSerializer . get_bookmark_info (
obj = obj ,
user = user ,
@ -2038,6 +1983,186 @@ class OriginalTextReferenceImageSerializer(serializers.ModelSerializer):
# --- سریالایزرهای جامع رفرنسها (شامل اطلاعات کتاب، ادیشن و ولوم) ---
_EDITION_METADATA_MEM = None
_EDITION_METADATA_LOADED_AT = 0.0
_EDITION_SIGNALS_CONNECTED = False
def _invalidate_edition_metadata_cache ( * * kwargs ) :
global _EDITION_METADATA_MEM , _EDITION_METADATA_LOADED_AT
_EDITION_METADATA_MEM = None
_EDITION_METADATA_LOADED_AT = 0.0
def _ensure_edition_cache_signals ( ) :
global _EDITION_SIGNALS_CONNECTED
if _EDITION_SIGNALS_CONNECTED :
return
_EDITION_SIGNALS_CONNECTED = True
from django.db.models.signals import post_save , post_delete
from ..models import (
BookEdition , BookVolume , BookResearcher , BookEditor ,
BookAuthor , BookPublisher , City , Country
)
for model_cls in (
BookEdition , BookVolume , BookResearcher , BookEditor ,
BookAuthor , BookPublisher , City , Country
) :
post_save . connect ( _invalidate_edition_metadata_cache , sender = model_cls , dispatch_uid = f " inv_ed_save_{model_cls.__name__} " )
post_delete . connect ( _invalidate_edition_metadata_cache , sender = model_cls , dispatch_uid = f " inv_ed_del_{model_cls.__name__} " )
def get_cached_edition_metadata ( ) :
global _EDITION_METADATA_MEM , _EDITION_METADATA_LOADED_AT
_ensure_edition_cache_signals ( )
import time
now = time . monotonic ( )
if _EDITION_METADATA_MEM is not None and ( now - _EDITION_METADATA_LOADED_AT ) < 300.0 :
return _EDITION_METADATA_MEM
from django.db.models import Count , Prefetch
from ..models import BookEdition , BookVolume , BookResearcher , BookEditor
editions = list (
BookEdition . objects . select_related (
' publisher_item ' ,
' city_of_publication_city ' ,
' country_of_publication_country ' ,
) . annotate (
_cached_vol_count = Count ( ' volumes ' )
) . prefetch_related (
Prefetch ( ' researchers ' , queryset = BookResearcher . objects . select_related ( ' author ' ) ) ,
Prefetch ( ' editors ' , queryset = BookEditor . objects . select_related ( ' author ' ) ) ,
)
)
by_id = { }
first_by_book_id = { }
for ed in editions :
ed . _cached_researchers = list ( ed . researchers . all ( ) )
ed . _cached_editors = list ( ed . editors . all ( ) )
by_id [ ed . id ] = ed
if ed . book_reference_id and ed . book_reference_id not in first_by_book_id :
first_by_book_id [ ed . book_reference_id ] = ed
book_volumes_count = dict (
BookVolume . objects . values ( ' book_reference_id ' )
. annotate ( cnt = Count ( ' id ' ) )
. values_list ( ' book_reference_id ' , ' cnt ' )
)
_EDITION_METADATA_MEM = {
' by_id ' : by_id ,
' first_by_book_id ' : first_by_book_id ,
' book_volumes_count ' : book_volumes_count ,
}
_EDITION_METADATA_LOADED_AT = now
return _EDITION_METADATA_MEM
def _resolve_reference_edition_fast ( obj ) :
if hasattr ( obj , ' _resolved_edition_cache ' ) :
return obj . _resolved_edition_cache
meta = get_cached_edition_metadata ( )
by_id = meta [ ' by_id ' ]
first_by_book_id = meta [ ' first_by_book_id ' ]
edition = None
ed_id = getattr ( obj , ' edition_id ' , None )
if ed_id and ed_id in by_id :
edition = by_id [ ed_id ]
if not edition and getattr ( obj , ' book_volume_id ' , None ) and getattr ( obj , ' book_volume ' , None ) :
bv_ed_id = getattr ( obj . book_volume , ' edition_id ' , None )
if bv_ed_id and bv_ed_id in by_id :
edition = by_id [ bv_ed_id ]
if not edition and getattr ( obj , ' book_reference_id ' , None ) :
edition = first_by_book_id . get ( obj . book_reference_id )
if not edition :
if getattr ( obj , ' edition_id ' , None ) and obj . edition :
edition = obj . edition
elif getattr ( obj , ' book_volume ' , None ) and obj . book_volume . edition :
edition = obj . book_volume . edition
elif getattr ( obj , ' book_reference ' , None ) and hasattr ( obj . book_reference , ' editions ' ) :
if hasattr ( obj . book_reference , ' _prefetched_objects_cache ' ) and ' editions ' in obj . book_reference . _prefetched_objects_cache :
editions_list = list ( obj . book_reference . editions . all ( ) )
edition = editions_list [ 0 ] if editions_list else None
else :
edition = obj . book_reference . editions . first ( )
obj . _resolved_edition_cache = edition
return edition
def _build_edition_info_dict ( obj , edition , request ) :
if hasattr ( obj , ' _edition_info_cache ' ) :
return obj . _edition_info_cache
if not edition :
obj . _edition_info_cache = None
return None
pub_src = edition . publisher_item . title if ( getattr ( edition , " publisher_item_id " , None ) and edition . publisher_item and edition . publisher_item . title ) else edition . publisher
pub = get_localized_text ( pub_src , request ) if isinstance ( pub_src , list ) else pub_src
ed_title = get_localized_text ( edition . title , request ) if isinstance ( getattr ( edition , " title " , None ) , list ) else getattr ( edition , " title " , None )
city_src = (
edition . city_of_publication_city . title
if ( getattr ( edition , " city_of_publication_city_id " , None ) and edition . city_of_publication_city and edition . city_of_publication_city . title )
else getattr ( edition , " city_of_publication " , None )
)
country_src = (
edition . country_of_publication_country . title
if ( getattr ( edition , " country_of_publication_country_id " , None ) and edition . country_of_publication_country and edition . country_of_publication_country . title )
else getattr ( edition , " country_of_publication " , None )
)
city = get_localized_text ( city_src , request ) if city_src else " "
country = get_localized_text ( country_src , request ) if country_src else " "
if hasattr ( edition , ' _cached_vol_count ' ) :
vol_count = edition . _cached_vol_count
elif hasattr ( edition , ' _prefetched_objects_cache ' ) and ' volumes ' in edition . _prefetched_objects_cache :
vol_count = len ( edition . volumes . all ( ) )
elif hasattr ( edition , " volumes " ) :
vol_count = edition . volumes . count ( )
else :
vol_count = 0
num_vols = vol_count if vol_count > 0 else edition . number_of_volumes
def _serialize_contributor ( c ) :
name = get_localized_text ( c . name , request ) if isinstance ( getattr ( c , " name " , None ) , list ) else ( getattr ( c , " name " , None ) or " " )
author_obj = getattr ( c , " author " , None )
author_data = None
if author_obj :
author_data = {
" id " : author_obj . id ,
" slug " : author_obj . slug ,
" name " : get_localized_text ( author_obj . name , request ) if isinstance ( author_obj . name , list ) else ( author_obj . name or " " ) ,
}
return {
" id " : c . id ,
" name " : name ,
" slug " : author_obj . slug if ( author_obj and author_obj . slug ) else getattr ( c , " slug " , None ) ,
" author_id " : getattr ( c , " author_id " , None ) ,
" author " : author_data ,
}
raw_researchers = edition . _cached_researchers if hasattr ( edition , ' _cached_researchers ' ) else ( edition . researchers . all ( ) if hasattr ( edition , " researchers " ) else [ ] )
raw_editors = edition . _cached_editors if hasattr ( edition , ' _cached_editors ' ) else ( edition . editors . all ( ) if hasattr ( edition , " editors " ) else [ ] )
researchers = [ _serialize_contributor ( r ) for r in raw_researchers ]
editors = [ _serialize_contributor ( e ) for e in raw_editors ]
result = {
" id " : edition . id ,
" title " : ed_title or " " ,
" edition_number " : edition . edition_number ,
" publisher " : pub or " " ,
" publication_place " : { " city " : city or " " , " country " : country or " " } ,
" year_of_publication " : edition . year_of_publication or " " ,
" number_of_volumes " : num_vols ,
" source_url " : getattr ( edition , " source_url " , None ) or " " ,
" researchers " : researchers ,
" editors " : editors ,
}
obj . _edition_info_cache = result
return result
class BaseDetailedReferenceSerializer ( serializers . ModelSerializer ) :
book_title = serializers . SerializerMethodField ( )
book_authors = serializers . SerializerMethodField ( )
@ -2059,66 +2184,11 @@ class BaseDetailedReferenceSerializer(serializers.ModelSerializer):
]
return [ ]
def get_edition_info ( self , obj ) :
edition = obj . edition
if not edition and getattr ( obj , " book_volume " , None ) and obj . book_volume . edition :
edition = obj . book_volume . edition
if not edition and getattr ( obj , " book_reference " , None ) and hasattr ( obj . book_reference , " editions " ) :
edition = obj . book_reference . editions . first ( )
if edition :
request = self . context . get ( " request " )
pub_src = edition . publisher_item . title if ( getattr ( edition , " publisher_item_id " , None ) and edition . publisher_item and edition . publisher_item . title ) else edition . publisher
pub = get_localized_text ( pub_src , request ) if isinstance ( pub_src , list ) else pub_src
ed_title = get_localized_text ( edition . title , request ) if isinstance ( getattr ( edition , " title " , None ) , list ) else getattr ( edition , " title " , None )
city_src = (
edition . city_of_publication_city . title
if ( getattr ( edition , " city_of_publication_city_id " , None ) and edition . city_of_publication_city and edition . city_of_publication_city . title )
else getattr ( edition , " city_of_publication " , None )
)
country_src = (
edition . country_of_publication_country . title
if ( getattr ( edition , " country_of_publication_country_id " , None ) and edition . country_of_publication_country and edition . country_of_publication_country . title )
else getattr ( edition , " country_of_publication " , None )
)
city = get_localized_text ( city_src , request ) if city_src else " "
country = get_localized_text ( country_src , request ) if country_src else " "
vol_count = edition . volumes . count ( ) if hasattr ( edition , " volumes " ) else 0
num_vols = vol_count if vol_count > 0 else edition . number_of_volumes
def _serialize_contributor ( c ) :
name = get_localized_text ( c . name , request ) if isinstance ( getattr ( c , " name " , None ) , list ) else ( getattr ( c , " name " , None ) or " " )
author_obj = getattr ( c , " author " , None )
author_data = None
if author_obj :
author_data = {
" id " : author_obj . id ,
" slug " : author_obj . slug ,
" name " : get_localized_text ( author_obj . name , request ) if isinstance ( author_obj . name , list ) else ( author_obj . name or " " ) ,
}
return {
" id " : c . id ,
" name " : name ,
" slug " : author_obj . slug if ( author_obj and author_obj . slug ) else getattr ( c , " slug " , None ) ,
" author_id " : getattr ( c , " author_id " , None ) ,
" author " : author_data ,
}
researchers = [ _serialize_contributor ( r ) for r in edition . researchers . all ( ) ] if hasattr ( edition , " researchers " ) else [ ]
editors = [ _serialize_contributor ( e ) for e in edition . editors . all ( ) ] if hasattr ( edition , " editors " ) else [ ]
def _get_resolved_edition ( self , obj ) :
return _resolve_reference_edition_fast ( obj )
return {
" id " : edition . id ,
" title " : ed_title or " " ,
" edition_number " : edition . edition_number ,
" publisher " : pub or " " ,
" publication_place " : { " city " : city or " " , " country " : country or " " } ,
" year_of_publication " : edition . year_of_publication or " " ,
" number_of_volumes " : num_vols ,
" source_url " : getattr ( edition , " source_url " , None ) or " " ,
" researchers " : researchers ,
" editors " : editors ,
}
return None
def get_edition_info ( self , obj ) :
return _build_edition_info_dict ( obj , self . _get_resolved_edition ( obj ) , self . context . get ( " request " ) )
def get_volume_info ( self , obj ) :
if obj . book_volume :
@ -2162,11 +2232,7 @@ class DetailedCorrectionReferenceSerializer(BaseDetailedReferenceSerializer):
def get_book ( self , obj ) :
if obj . book_reference :
request = self . context . get ( ' request ' )
first_ed = obj . edition
if not first_ed and getattr ( obj , " book_volume " , None ) and obj . book_volume . edition :
first_ed = obj . book_volume . edition
if not first_ed and hasattr ( obj . book_reference , " editions " ) :
first_ed = obj . book_reference . editions . first ( )
first_ed = self . _get_resolved_edition ( obj )
pub = getattr ( obj . book_reference , " publisher " , None )
if pub is None and first_ed :
@ -2181,8 +2247,8 @@ class DetailedCorrectionReferenceSerializer(BaseDetailedReferenceSerializer):
num_vols = getattr ( obj . book_reference , " number_of_volumes " , None )
if num_vols is None and first_ed :
num_vols = first_ed . number_of_volumes
if num_vols is None and hasattr ( obj . book_reference , " volumes " ) :
num_vols = obj . book_reference . volumes . count ( ) or None
if num_vols is None and getattr ( obj , ' book_reference_id ' , None ) :
num_vols = get_cached_edition_metadata ( ) [ ' book_volumes_count ' ] . get ( obj . book_reference_id ) or None
return {
' id ' : obj . book_reference . id ,
@ -2214,11 +2280,7 @@ class DetailedCorrectionReferenceSerializer(BaseDetailedReferenceSerializer):
return absolute_https_url ( obj . book_volume . file . url , request ) if request else absolute_https_url ( obj . book_volume . file . url )
if getattr ( obj , ' url ' , None ) :
return obj . url
edition = obj . edition
if not edition and getattr ( obj , ' book_volume ' , None ) and obj . book_volume . edition :
edition = obj . book_volume . edition
if not edition and getattr ( obj , ' book_reference ' , None ) and hasattr ( obj . book_reference , ' editions ' ) :
edition = obj . book_reference . editions . first ( )
edition = self . _get_resolved_edition ( obj )
if edition and getattr ( edition , ' source_url ' , None ) :
return edition . source_url
return None