|
|
|
@ -1,6 +1,7 @@ |
|
|
|
from turtle import title |
|
|
|
from rest_framework import serializers |
|
|
|
from django.utils.translation import get_language |
|
|
|
from .category import get_localized_text |
|
|
|
|
|
|
|
from ..serializers import HadisListSerializer,HadisBasicSerializer,LocalizedField |
|
|
|
from ..models import BookReference , BookAuthor , BookReferenceImage, HadisReference , BookAttribute |
|
|
|
@ -139,36 +140,37 @@ class BookReferenceSyncSerializer(serializers.ModelSerializer): |
|
|
|
|
|
|
|
# Basic information |
|
|
|
detail = serializers.SerializerMethodField() |
|
|
|
|
|
|
|
# Hadis group (related hadises) |
|
|
|
hadises = serializers.SerializerMethodField() |
|
|
|
title = LocalizedField() |
|
|
|
|
|
|
|
# authors = serializers.SerializerMethodField() |
|
|
|
|
|
|
|
class Meta: |
|
|
|
model = BookReference |
|
|
|
fields = [ |
|
|
|
'id', 'title','rate' , 'author' ,'detail','image','attribute', '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_authors(self,obj): |
|
|
|
# request = self.context.get('request') |
|
|
|
# authors = [] |
|
|
|
# try: |
|
|
|
# for author in obj.authors.all(): |
|
|
|
# authors.append({ |
|
|
|
# 'id': author.id, |
|
|
|
# 'name': get_localized_text(author.name,request) |
|
|
|
# }) |
|
|
|
# except: |
|
|
|
# authors = [] |
|
|
|
# return authors |
|
|
|
|
|
|
|
|
|
|
|
def get_detail(self, obj): |
|
|
|
"""Get basic book information including authors and rating""" |
|
|
|
request = self.context.get('request') |
|
|
|
|
|
|
|
return { |
|
|
|
'description': obj.description, |
|
|
|
'description': get_localized_text(obj.description,request), |
|
|
|
'volume': obj.volume, |
|
|
|
'language': obj.language, |
|
|
|
'language': get_localized_text(obj.language,request), |
|
|
|
'isbn': obj.isbn, |
|
|
|
'number_page':obj.number_page, |
|
|
|
'year_of_publication': obj.year_of_publication, |
|
|
|
@ -179,16 +181,18 @@ class BookReferenceSyncSerializer(serializers.ModelSerializer): |
|
|
|
|
|
|
|
def get_hadises(self, obj): |
|
|
|
"""Get all hadises related to this book reference""" |
|
|
|
request = self.context.get('request') |
|
|
|
|
|
|
|
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'), |
|
|
|
'title': get_localized_text(hadis.title,request), |
|
|
|
'title_narrator': get_localized_text(hadis.title_narrator,request), |
|
|
|
'text': get_localized_text(hadis.text,request), |
|
|
|
'translation': get_localized_text(hadis.get_translation,request), |
|
|
|
'share_link': hadis.share_link |
|
|
|
}) |
|
|
|
except: |
|
|
|
|