You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.9 KiB
128 lines
3.9 KiB
from rest_framework import serializers
|
|
|
|
from ..serializers import HadisListSerializer
|
|
from ..models import BookReference , BookAuthor , BookReferenceImage, HadisReference
|
|
|
|
class BookAuthorSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = BookAuthor
|
|
fields = '__all__'
|
|
|
|
class BookReferenceImageSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = BookReferenceImage
|
|
fields = '__all__'
|
|
|
|
class BookReferenceSerializer(serializers.ModelSerializer):
|
|
image = BookReferenceImageSerializer(
|
|
many= True ,
|
|
read_only = True ,
|
|
source = 'bookreference_set'
|
|
)
|
|
volume_count = serializers.SerializerMethodField()
|
|
class Meta:
|
|
model = BookReference
|
|
fields = ['id','title','description','rate','image','volume_count']
|
|
def get_volume_count(self,obj):
|
|
request = self.context.get('request')
|
|
return BookReference.objects.filter(title=obj.title).count()
|
|
|
|
class BookDetailSerializer(serializers.ModelSerializer):
|
|
|
|
author = BookAuthorSerializer(
|
|
read_only = True ,
|
|
source = 'bookauthor_set'
|
|
)
|
|
image = BookReferenceImageSerializer(
|
|
many= True ,
|
|
read_only = True ,
|
|
source = 'bookreference_set'
|
|
)
|
|
volume_count = serializers.SerializerMethodField()
|
|
|
|
hadis = HadisListSerializer(
|
|
many=True,
|
|
read_only=True,
|
|
source='hadisreference_set'
|
|
)
|
|
|
|
class Meta:
|
|
model = BookReference
|
|
fields = '__all__'
|
|
def get_volume_count(self,obj):
|
|
request = self.context.get('request')
|
|
return BookReference.objects.filter(title=obj.title).count()
|
|
|
|
# def create(self , validated_data):
|
|
# author = validated_data.pop('author')
|
|
# book = BookReference.objects.create(**validated_data)
|
|
# for author in author
|
|
|
|
|
|
class BookReferenceSyncSerializer(serializers.ModelSerializer):
|
|
"""Serializer for syncing all book reference data for offline mode"""
|
|
|
|
# Basic information
|
|
basic_info = serializers.SerializerMethodField()
|
|
|
|
# Information group (detailed publication info)
|
|
information = serializers.SerializerMethodField()
|
|
|
|
# Hadis group (related hadises)
|
|
hadis = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = BookReference
|
|
fields = [
|
|
'id', 'title', 'basic_info', 'information', 'hadis'
|
|
]
|
|
|
|
def get_basic_info(self, obj):
|
|
"""Get basic book information including authors and rating"""
|
|
authors = []
|
|
try:
|
|
for author in obj.authors.all():
|
|
authors.append({
|
|
'id': author.id,
|
|
'name': author.name
|
|
})
|
|
except:
|
|
authors = []
|
|
|
|
return {
|
|
'title': obj.title,
|
|
'authors': authors,
|
|
'rating': obj.rate,
|
|
'description': obj.description,
|
|
'volume': obj.volume
|
|
}
|
|
|
|
def get_information(self, obj):
|
|
"""Get detailed publication information"""
|
|
return {
|
|
'language': obj.language,
|
|
'isbn': obj.isbn,
|
|
'year_of_publication': obj.year_of_publication,
|
|
'number_of_pages': obj.number_page,
|
|
'volume_info': obj.volume,
|
|
'rating': obj.rate
|
|
}
|
|
|
|
def get_hadis(self, obj):
|
|
"""Get all hadises related to this book reference"""
|
|
hadis_list = []
|
|
try:
|
|
for hadis_ref in obj.hadis_references.all():
|
|
hadis = hadis_ref.hadis
|
|
hadis_list.append({
|
|
'id': hadis.id,
|
|
'title': hadis.title,
|
|
'title_narrator': hadis.title_narrator,
|
|
'text': hadis.text,
|
|
'translation': hadis.get_translation(self.context.get('request').LANGUAGE_CODE if self.context.get('request') else 'en'),
|
|
'share_link': hadis.share_link
|
|
})
|
|
except:
|
|
pass
|
|
|
|
return hadis_list
|