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.
282 lines
10 KiB
282 lines
10 KiB
from rest_framework import serializers
|
|
from rest_framework.pagination import PageNumberPagination
|
|
from ..models import BookReference, BookAuthor, BookEdition, BookVolume, BookTag
|
|
from .category import get_localized_text
|
|
from apps.hadis.serializers import HadisListSerializer, HadisShortSerializer
|
|
from ..serializers.category import LocalizedField
|
|
from ..serializers.reference import BookAttributeSerializer, BookReferenceImageSerializer
|
|
|
|
class BookTagV2Serializer(serializers.ModelSerializer):
|
|
title = LocalizedField()
|
|
class Meta:
|
|
model = BookTag
|
|
fields = ['id', 'title']
|
|
|
|
class BookAuthorV2Serializer(serializers.ModelSerializer):
|
|
name = LocalizedField()
|
|
class Meta:
|
|
model = BookAuthor
|
|
fields = ['id', 'name', 'slug']
|
|
|
|
class BookReferenceV2ListSerializer(serializers.ModelSerializer):
|
|
title = LocalizedField()
|
|
authors = BookAuthorV2Serializer(many=True, read_only=True)
|
|
tags = BookTagV2Serializer(many=True, read_only=True)
|
|
number_of_volumes = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = BookReference
|
|
fields = ['id', 'slug', 'title', 'authors', 'number_of_volumes', 'tags']
|
|
|
|
def get_number_of_volumes(self, obj):
|
|
return obj.volumes.count()
|
|
|
|
class BookEditionV2Serializer(serializers.ModelSerializer):
|
|
publisher = LocalizedField()
|
|
notes = LocalizedField()
|
|
tags = LocalizedField()
|
|
publication_place = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = BookEdition
|
|
fields = [
|
|
'id', 'publisher', 'publication_place', 'edition_number',
|
|
'year_of_publication', 'number_of_volumes', 'notes', 'source_url', 'tags'
|
|
]
|
|
|
|
def get_publication_place(self, obj):
|
|
request = self.context.get("request")
|
|
city = get_localized_text(obj.city_of_publication, request) if obj.city_of_publication else ""
|
|
country = get_localized_text(obj.country_of_publication, request) if obj.country_of_publication else ""
|
|
return {
|
|
"city": city,
|
|
"country": country
|
|
}
|
|
|
|
class BookVolumeV2Serializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = BookVolume
|
|
fields = ['id', 'title']
|
|
|
|
class BookReferenceV2DetailSerializer(serializers.ModelSerializer):
|
|
title = LocalizedField()
|
|
authors = BookAuthorV2Serializer(many=True, read_only=True)
|
|
|
|
information = serializers.SerializerMethodField()
|
|
excerpts = serializers.SerializerMethodField()
|
|
volumes = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = BookReference
|
|
fields = ['id', 'title', 'authors', 'information', 'excerpts', 'volumes']
|
|
|
|
def get_information(self, obj):
|
|
request = self.context.get("request")
|
|
summary = get_localized_text(obj.description, request) if obj.description else ""
|
|
editions = BookEditionV2Serializer(obj.editions.all(), many=True, context=self.context).data
|
|
|
|
return {
|
|
"summary": summary,
|
|
"editions": editions
|
|
}
|
|
|
|
def get_excerpts(self, obj):
|
|
from apps.hadis.models import Hadis
|
|
hadis_ids = obj.hadis_references.values_list('hadis', flat=True)
|
|
hadis_qs = Hadis.objects.filter(id__in=hadis_ids, status=True).order_by('id')
|
|
|
|
paginator = PageNumberPagination()
|
|
paginator.page_size = 20
|
|
request = self.context.get("request")
|
|
|
|
paginated_hadis = paginator.paginate_queryset(hadis_qs, request)
|
|
|
|
if paginated_hadis is not None:
|
|
serializer = HadisListSerializer(paginated_hadis, many=True, context=self.context)
|
|
return {
|
|
'count': paginator.page.paginator.count,
|
|
'next': paginator.get_next_link(),
|
|
'previous': paginator.get_previous_link(),
|
|
'results': serializer.data
|
|
}
|
|
|
|
return {
|
|
'count': hadis_qs.count(),
|
|
'next': None,
|
|
'previous': None,
|
|
'results': HadisListSerializer(hadis_qs, many=True, context=self.context).data
|
|
}
|
|
|
|
def get_volumes(self, obj):
|
|
editions = obj.editions.all()
|
|
if editions.exists():
|
|
editions_data = []
|
|
for ed in editions:
|
|
editions_data.append({
|
|
"edition_id": ed.id,
|
|
"edition_statement": ed.edition_number or f"Edition {ed.id}",
|
|
"volumes": BookVolumeV2Serializer(ed.volumes.all(), many=True).data
|
|
})
|
|
return {
|
|
"has_editions": True,
|
|
"editions_volumes": editions_data,
|
|
"all_volumes": []
|
|
}
|
|
else:
|
|
return {
|
|
"has_editions": False,
|
|
"editions_volumes": [],
|
|
"all_volumes": BookVolumeV2Serializer(obj.volumes.all(), many=True).data
|
|
}
|
|
|
|
class BookReferenceV2SyncSerializer(serializers.ModelSerializer):
|
|
title = LocalizedField()
|
|
authors = BookAuthorV2Serializer(many=True, read_only=True)
|
|
attributes = BookAttributeSerializer(many=True, read_only=True)
|
|
images = BookReferenceImageSerializer(many=True, read_only=True)
|
|
|
|
information = serializers.SerializerMethodField()
|
|
hadises = serializers.SerializerMethodField()
|
|
volumes = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = BookReference
|
|
fields = [
|
|
'id', 'title', 'slug', 'share_link', 'rate', 'authors',
|
|
'attributes', 'images', 'information', 'hadises', 'volumes'
|
|
]
|
|
|
|
def get_information(self, obj):
|
|
request = self.context.get("request")
|
|
summary = get_localized_text(obj.description, request) if obj.description else ""
|
|
editions = BookEditionV2Serializer(obj.editions.all(), many=True, context=self.context).data
|
|
return {
|
|
"summary": summary,
|
|
"editions": editions,
|
|
"language": get_localized_text(obj.language, request) if obj.language else "",
|
|
"number_page": obj.number_page
|
|
}
|
|
|
|
def get_hadises(self, obj):
|
|
references = obj.hadis_references.all()
|
|
hadis_list = [ref.hadis for ref in references if ref.hadis]
|
|
return HadisShortSerializer(hadis_list, many=True, context=self.context).data
|
|
|
|
def get_volumes(self, obj):
|
|
editions = obj.editions.all()
|
|
if editions.exists():
|
|
editions_data = []
|
|
for ed in editions:
|
|
editions_data.append({
|
|
"edition_id": ed.id,
|
|
"edition_statement": ed.edition_number or f"Edition {ed.id}",
|
|
"volumes": BookVolumeV2Serializer(ed.volumes.all(), many=True).data
|
|
})
|
|
return {
|
|
"has_editions": True,
|
|
"editions_volumes": editions_data,
|
|
"all_volumes": []
|
|
}
|
|
else:
|
|
return {
|
|
"has_editions": False,
|
|
"editions_volumes": [],
|
|
"all_volumes": BookVolumeV2Serializer(obj.volumes.all(), many=True).data
|
|
}
|
|
|
|
from apps.hadis.models.reference import BookAuthor, BookReference
|
|
from .category import LocalizedField
|
|
|
|
class BookAuthorListSerializer(serializers.ModelSerializer):
|
|
"""سریالایزر برای لیست نویسندهها"""
|
|
name = LocalizedField()
|
|
|
|
class Meta:
|
|
model = BookAuthor
|
|
fields = ['id', 'slug', 'name', 'birth_year_hijri', 'birth_year_miladi']
|
|
|
|
|
|
class BookAuthorDetailSerializer(serializers.ModelSerializer):
|
|
"""سریالایزر برای دیتیل یک نویسنده"""
|
|
name = LocalizedField()
|
|
known_as = LocalizedField()
|
|
scholarity = LocalizedField()
|
|
tabaqa = LocalizedField()
|
|
rutba = LocalizedField()
|
|
features = LocalizedField()
|
|
|
|
class Meta:
|
|
model = BookAuthor
|
|
fields = [
|
|
'id', 'slug', 'name', 'known_as', 'scholarity',
|
|
'tabaqa', 'rutba', 'features',
|
|
'birth_year_hijri', 'death_year_hijri',
|
|
'birth_year_miladi', 'death_year_miladi'
|
|
]
|
|
|
|
|
|
class AuthorBookReferenceSerializer(serializers.ModelSerializer):
|
|
"""سریالایزر برای لیست رفرنسهای (کتابهای) یک نویسنده"""
|
|
title = LocalizedField()
|
|
authors = serializers.SerializerMethodField()
|
|
thumbnail = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = BookReference
|
|
fields = ['id', 'slug', 'title', 'thumbnail', 'authors']
|
|
|
|
def get_authors(self, obj):
|
|
field = LocalizedField()
|
|
field._context = self.context
|
|
return [field.to_representation(author.name) for author in obj.authors.all()]
|
|
|
|
def get_thumbnail(self, obj):
|
|
# گرفتن اولین عکس آپلود شده برای این رفرنس
|
|
first_image = obj.images.order_by('order').first()
|
|
if first_image and first_image.image:
|
|
request = self.context.get('request')
|
|
if request:
|
|
return request.build_absolute_uri(first_image.image.url)
|
|
return first_image.image.url
|
|
return None
|
|
|
|
# یک سریالایزر مینیمال برای کتابها داخل سینک
|
|
class BookAuthorSyncReferenceSerializer(serializers.ModelSerializer):
|
|
title = LocalizedField()
|
|
|
|
class Meta:
|
|
model = BookReference
|
|
fields = ['id', 'slug', 'title']
|
|
|
|
class BookAuthorSyncSerializer(serializers.ModelSerializer):
|
|
"""
|
|
سریالایزر برای سینک آفلاین تمام نویسندهها.
|
|
شامل تمام اطلاعات و لیست کتابهای متصل به این نویسنده.
|
|
"""
|
|
name = LocalizedField()
|
|
known_as = LocalizedField()
|
|
scholarity = LocalizedField()
|
|
tabaqa = LocalizedField()
|
|
rutba = LocalizedField()
|
|
features = LocalizedField()
|
|
|
|
# اضافه کردن لیست کتابها
|
|
books = BookAuthorSyncReferenceSerializer(source='book_references', many=True, read_only=True)
|
|
|
|
class Meta:
|
|
model = BookAuthor
|
|
fields = [
|
|
'id',
|
|
'slug',
|
|
'name',
|
|
'known_as',
|
|
'scholarity',
|
|
'tabaqa',
|
|
'rutba',
|
|
'features',
|
|
'birth_year_hijri',
|
|
'death_year_hijri',
|
|
'birth_year_miladi',
|
|
'death_year_miladi',
|
|
'books' # 👈 لیست کتابهای هر نویسنده
|
|
]
|