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.8 KiB

from rest_framework import serializers
from ..serializers import HadisListSerializer
from ..models import BookReference , BookAuthor , BookReferenceImage, HadisReference , BookAttribute
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'
)
author = serializers.SerializerMethodField()
class Meta:
model = BookReference
fields = ['id','title','rate','author','description','image','volume']
def get_author (self,obj):
author = obj.bookauthor_set
return author.name
class BookAttributeSerializer(serializers.ModelSerializer):
class Meta:
model = BookAttribute
fields = ['id', 'title', 'value', 'book_reference', 'created_at', 'updated_at']
read_only_fields = ['id', 'created_at', 'updated_at']
class BookDetailSerializer(serializers.ModelSerializer):
attribute = BookAttributeSerializer(
read_only = True,
source = 'bookattribute_set'
)
author = BookAuthorSerializer(
read_only = True ,
source = 'bookauthor_set'
)
image = BookReferenceImageSerializer(
many= True ,
read_only = True ,
source = 'bookreference_set'
)
hadis = HadisListSerializer(
many=True,
read_only=True,
source='hadisreference_set'
)
class Meta:
model = BookReference
fields = '__all__'
# 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
detail = serializers.SerializerMethodField()
# Hadis group (related hadises)
hadises = serializers.SerializerMethodField()
authors = serializers.SerializerMethodField()
class Meta:
model = BookReference
fields = [
'id', 'title','rate' , 'authors' ,'detail', 'hadises'
]
def get_authors(self,obj):
authors = []
try:
for author in obj.authors.all():
authors.append({
'id': author.id,
'name': author.name
})
except:
authors = []
return authors
def get_detail(self, obj):
"""Get basic book information including authors and rating"""
return {
'description': obj.description,
'volume': obj.volume,
'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_hadises(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