@ -1,5 +1,6 @@
from rest_framework import serializers
from django.utils.translation import gettext_lazy as _
from rest_framework.fields import SerializerMethodField
from urllib3 import fields
from ..models import (
@ -27,26 +28,25 @@ class HadisCollectionListSerializer(serializers.ModelSerializer):
class HadisSyncSerializer ( serializers . ModelSerializer ) :
""" Serializer for syncing all hadis data """
""" Serializer for syncing all hadis data (grouped fields) """
translations = serializers . SerializerMethodField ( )
hadis_status = serializers . SerializerMethodField ( )
tags = serializers . SerializerMethodField ( )
transmitters = serializers . SerializerMethodField ( )
description = serializers . SerializerMethodField ( )
references = serializers . SerializerMethodField ( )
detail = serializers . SerializerMethodField ( )
narrators = serializers . SerializerMethodField ( )
explanations = serializers . SerializerMethodField ( )
corrections = serializers . SerializerMethodField ( )
class Meta :
model = Hadis
fields = [
' id ' , ' number ' , ' category_id ' , ' title ' , ' title_narrator ' ,
' text ' , ' translations ' , ' explanation ' , ' address ' , ' description ' ,
' hadis_status ' , ' hadis_status_text ' , ' share_link ' , ' tags ' , ' links ' ,
' transmitte rs' , ' reference s' , ' corrections '
# header (no-extend)
' id ' , ' number ' , ' category_id ' , ' title ' , ' title_narrator ' , ' text ' , ' translations ' ,
# grouped sections
' detail ' , ' narrato rs' , ' explanation s' , ' corrections ' ,
]
def get_translations ( self , obj ) :
""" Get all translations """
""" Get all translations as {lang: text} """
translations_dict = { }
if obj . translation and isinstance ( obj . translation , list ) :
for tr in obj . translation :
@ -56,88 +56,92 @@ class HadisSyncSerializer(serializers.ModelSerializer):
if lang_code :
translations_dict [ lang_code ] = title
return translations_dict
def get_hadis_status ( self , obj ) :
""" Get hadis status info """
def get_detail ( self , obj ) :
""" Detail group: address, status, share, links, tags, references, reference_images """
request = self . context . get ( ' request ' )
# status
status_block = None
if obj . hadis_status :
return {
status_block = {
' id ' : obj . hadis_status . id ,
' title ' : obj . hadis_status . title ,
' color ' : obj . hadis_status . color
}
return None
def get_tags ( self , obj ) :
""" Get tags """
return [ { ' id ' : tag . id , ' title ' : tag . title } for tag in obj . tags . all ( ) ]
def get_transmitters ( self , obj ) :
""" Get transmitters with their details """
# tags
tags_block = [ { ' id ' : tag . id , ' title ' : tag . title } for tag in obj . tags . all ( ) ]
# references and reference images
references_block = [ ]
reference_images_block = [ ]
for reference in obj . references . select_related ( ' book_reference ' ) . prefetch_related ( ' book_reference__authors ' , ' images ' ) :
book = reference . book_reference
references_block . append ( {
' id ' : reference . id ,
' title ' : book . title if book else None ,
' authors ' : [ { ' id ' : a . id , ' name ' : a . name } for a in book . authors . all ( ) ] if book else [ ] ,
' description ' : book . description if book else None ,
} )
for img in reference . images . all ( ) . order_by ( ' priority ' ) :
thumb_url = None
if img . thumbnail :
thumb_url = request . build_absolute_uri ( img . thumbnail . url ) if request else img . thumbnail . url
reference_images_block . append ( {
' id ' : img . id ,
' thumbnail ' : thumb_url ,
' priority ' : img . priority ,
} )
return {
' address ' : obj . address ,
' hadis_status ' : status_block ,
' status_text ' : obj . hadis_status_text ,
' share_link ' : obj . share_link ,
' links ' : obj . links ,
' tags ' : tags_block ,
' references ' : references_block ,
' reference_images ' : reference_images_block ,
}
def get_narrators ( self , obj ) :
""" Narrators group: description + transmitters """
transmitters_data = [ ]
for transmitter_rel in obj . transmitters . all ( ) . order_by ( ' order ' ) :
transmitter_info = {
' id ' : transmitter_rel . id ,
' order ' : transmitter_rel . order ,
for transmitter_rel in obj . transmitters . select_related ( ' transmitter ' , ' narrator_layer ' ) . order_by ( ' order ' ) :
t = transmitter_rel . transmitter
transmitters_data . append ( {
' id ' : t . id ,
' name ' : t . full_name ,
' reliability ' : t . reliability ,
' layer_level ' : transmitter_rel . narrator_layer . number if transmitter_rel . narrator_layer else None ,
' layer_name ' : transmitter_rel . narrator_layer . name if transmitter_rel . narrator_layer else None ,
' is_gap ' : transmitter_rel . is_gap ,
' narrator_layer ' : transmitter_rel . narrator_layer ,
' transmitter ' : {
' id ' : transmitter_rel . transmitter . id ,
' full_name ' : transmitter_rel . transmitter . full_name ,
' birth_year_hijri ' : transmitter_rel . transmitter . birth_year_hijri ,
' death_year_hijri ' : transmitter_rel . transmitter . death_year_hijri ,
' madhhab ' : transmitter_rel . transmitter . madhhab ,
' description ' : transmitter_rel . transmitter . description ,
' reliability ' : transmitter_rel . transmitter . reliability
}
}
transmitters_data . append ( transmitter_info )
return transmitters_data
' birth_year_hijri ' : t . birth_year_hijri ,
' death_year_hijri ' : t . death_year_hijri ,
' order ' : transmitter_rel . order ,
} )
def get_description ( self , obj ) :
""" Get hadis description """
return getattr ( obj , ' description ' , None )
return {
' description ' : getattr ( obj , ' description ' , None ) ,
' transmitters ' : transmitters_data
}
def get_references ( self , obj ) :
""" Get references with book information """
references_data = [ ]
for reference in obj . references . all ( ) :
try :
book = reference . book_reference
reference_info = {
' id ' : reference . id ,
' title ' : book . title if book else None ,
' images ' : [
{
' id ' : img . id ,
' image ' : img . image . url if img . image else None ,
' order ' : img . order ,
' description ' : img . description
} for img in book . images . all ( ) if book
] if book else [ ] ,
' authors ' : [
{
' id ' : author . id ,
' name ' : author . name
} for author in book . authors . all ( ) if book
] if book else [ ] ,
' description ' : book . description if book else None
}
references_data . append ( reference_info )
except :
continue
return references_data
def get_explanations ( self , obj ) :
""" Explanations group """
return obj . explanation
def get_corrections ( self , obj ) :
""" Get hadis corrections """
""" Corrections group """
corrections_data = [ ]
for correction in obj . hadiscorrection_set . all ( ) :
correction_info = {
corrections_data . append ( {
' id ' : correction . id ,
' title ' : correction . title ,
' description ' : correction . description ,
' translation ' : correction . translation
}
corrections_data . append ( correction_info )
' translation ' : correction . translation ,
' share_link ' : correction . share_link ,
} )
return corrections_data
@ -232,43 +236,39 @@ class TransmitterDetailSerializer(serializers.ModelSerializer):
class TransmitterSyncSerializer ( serializers . ModelSerializer ) :
""" Serializer for syncing all transmitter data for offline mode """
# Biographical data group
# Biographical data group (flattened)
biographical = serializers . SerializerMethodField ( )
# Scholar's opinions group
scholars_opinions = serializers . SerializerMethodField ( )
# Original texts group
original_texts = serializers . SerializerMethodField ( )
class Meta :
model = Transmitters
fields = [
' id ' , ' full_name ' , ' biographical ' , ' scholars_opinions '
' id ' , ' full_name ' , ' biographical ' , ' scholars_opinions ' , ' original_texts '
]
def get_biographical ( self , obj ) :
""" Get biographical information """
""" Get biographical information (flattened) """
return {
' personal_info ' : {
' full_name ' : obj . full_name ,
' kunya ' : obj . kunya ,
' known_as ' : obj . known_as ,
' nickname ' : obj . nickname ,
} ,
' dates ' : {
' birth_year_hijri ' : obj . birth_year_hijri ,
' death_year_hijri ' : obj . death_year_hijri ,
' age_at_death ' : obj . age_at_death ,
} ,
' locations ' : {
' origin ' : obj . origin ,
' lived_in ' : obj . lived_in ,
' died_in ' : obj . died_in ,
} ,
' religious_profile ' : {
' reliability ' : obj . reliability ,
' madhhab ' : obj . madhhab ,
' in_sahih_muslim ' : obj . in_sahih_muslim ,
' in_sahih_bukhari ' : obj . in_sahih_bukhari ,
} ,
' full_name ' : obj . full_name ,
' kunya ' : obj . kunya ,
' known_as ' : obj . known_as ,
' nickname ' : obj . nickname ,
' origin ' : obj . origin ,
' lived_in ' : obj . lived_in ,
' died_in ' : obj . died_in ,
' birth_year_hijri ' : obj . birth_year_hijri ,
' death_year_hijri ' : obj . death_year_hijri ,
' age_at_death ' : obj . age_at_death ,
' generation ' : obj . generation ,
' reliability ' : obj . reliability ,
' madhhab ' : obj . madhhab ,
' in_sahih_muslim ' : obj . in_sahih_muslim ,
' in_sahih_bukhari ' : obj . in_sahih_bukhari ,
' description ' : obj . description ,
' thumbnail ' : obj . thumbnail . url if obj . thumbnail else None ,
}
@ -285,7 +285,20 @@ class TransmitterSyncSerializer(serializers.ModelSerializer):
' created_at ' : opinion . created_at . isoformat ( ) if opinion . created_at else None ,
' updated_at ' : opinion . updated_at . isoformat ( ) if opinion . updated_at else None ,
} )
return opinions
return opinions
def get_original_texts ( self , obj ) :
""" Get original texts of the transmitter """
texts = [ ]
for t in obj . originaltexts . all ( ) :
texts . append ( {
' id ' : t . id ,
' title ' : t . title ,
' text ' : t . text ,
' translation ' : t . translation ,
' share_link ' : t . share_link ,
} )
return texts
@ -310,24 +323,23 @@ class ReferenceImageSerializer(serializers.ModelSerializer):
def get_thumbnail ( self , obj ) :
""" Get thumbnail URL """
if obj . image :
if obj . thumbnail :
request = self . context . get ( ' request ' )
if request :
return request . build_absolute_uri ( obj . image . url )
return obj . image . url
return request . build_absolute_uri ( obj . thumbnail . url )
return obj . thumbnail . url
return None
class HadisReferenceSerializer ( serializers . ModelSerializer ) :
""" Serializer for HadisReference with book and images """
book_title = serializers . SerializerMethodField ( )
book_images = serializers . SerializerMethodField ( )
book_authors = serializers . SerializerMethodField ( )
book_description = serializers . SerializerMethodField ( )
class Meta :
model = HadisReference
fields = [
' id ' , ' book_title ' , ' book_images ' , ' book_authors ' , ' book_description '
' id ' , ' book_title ' , ' book_authors ' , ' book_description '
]
def get_book_title ( self , obj ) :
@ -338,13 +350,13 @@ class HadisReferenceSerializer(serializers.ModelSerializer):
except :
return None
def get_book_images ( self , obj ) :
""" Get book images """
try :
images = obj . book_reference . images . all ( )
return images
except :
return None
# def get_book_images(self, obj) :
# """Get book images """
# try :
# images = obj.book_reference.images.all( )
# return images
# except :
# return None
def get_book_authors ( self , obj ) :
""" Get book authors """
@ -414,16 +426,25 @@ class HadisDetailSerializer(serializers.ModelSerializer):
many = True ,
read_only = True
)
# category = serializers.SerializerMethodField( )
reference_images = SerializerMethodField ( )
class Meta :
model = Hadis
fields = [
' id ' , ' number ' ,
' hadis_status_text ' , ' hadis_status ' , ' links ' , ' share_link ' ,
' tags ' , ' references ' , ' address '
' tags ' , ' references ' , ' reference_images ' , ' address'
]
def get_reference_images ( self , obj ) :
""" Get all reference images from all references """
all_images = [ ]
for reference in obj . references . all ( ) :
images = reference . images . all ( ) . order_by ( ' priority ' )
serializer = ReferenceImageSerializer ( images , many = True , context = self . context )
all_images . extend ( serializer . data )
return all_images
# def get_category(self, obj):
# """Get category details"""
# if obj.category: