|
|
|
@ -1379,9 +1379,21 @@ class NarratorRelationSerializer(serializers.ModelSerializer): |
|
|
|
|
|
|
|
# --- سریالایزرهای کمکی تصاویر رفرنس --- |
|
|
|
class CorrectionReferenceImageSerializer(serializers.ModelSerializer): |
|
|
|
image = serializers.SerializerMethodField() |
|
|
|
thumbnail = serializers.SerializerMethodField() |
|
|
|
|
|
|
|
class Meta: |
|
|
|
model = CorrectionReferenceImage |
|
|
|
fields = ['id', 'image', 'priority'] |
|
|
|
fields = ['id', 'image', 'thumbnail', 'priority'] |
|
|
|
|
|
|
|
def get_image(self, obj): |
|
|
|
if obj.image: |
|
|
|
request = self.context.get('request') |
|
|
|
return absolute_https_url(obj.image.url, request) if request else absolute_https_url(obj.image.url) |
|
|
|
return None |
|
|
|
|
|
|
|
def get_thumbnail(self, obj): |
|
|
|
return self.get_image(obj) |
|
|
|
|
|
|
|
class InterpretationReferenceImageSerializer(serializers.ModelSerializer): |
|
|
|
class Meta: |
|
|
|
@ -1417,11 +1429,22 @@ class BaseDetailedReferenceSerializer(serializers.ModelSerializer): |
|
|
|
return [] |
|
|
|
|
|
|
|
def get_edition_info(self, obj): |
|
|
|
if obj.edition: |
|
|
|
edition = obj.edition |
|
|
|
if not edition and getattr(obj, "book_volume", None) and obj.book_volume.edition: |
|
|
|
edition = obj.book_volume.edition |
|
|
|
if not edition and getattr(obj, "book_reference", None) and hasattr(obj.book_reference, "editions"): |
|
|
|
edition = obj.book_reference.editions.first() |
|
|
|
if edition: |
|
|
|
request = self.context.get("request") |
|
|
|
pub = edition.publisher |
|
|
|
if isinstance(pub, list): |
|
|
|
pub = get_localized_text(pub, request) |
|
|
|
return { |
|
|
|
"id": obj.edition.id, |
|
|
|
"edition_number": obj.edition.edition_number, |
|
|
|
"publisher": obj.edition.publisher |
|
|
|
"id": edition.id, |
|
|
|
"edition_number": edition.edition_number, |
|
|
|
"publisher": pub or "", |
|
|
|
"year_of_publication": edition.year_of_publication or "", |
|
|
|
"number_of_volumes": edition.number_of_volumes, |
|
|
|
} |
|
|
|
return None |
|
|
|
|
|
|
|
@ -1434,10 +1457,164 @@ class BaseDetailedReferenceSerializer(serializers.ModelSerializer): |
|
|
|
return None |
|
|
|
|
|
|
|
class DetailedCorrectionReferenceSerializer(BaseDetailedReferenceSerializer): |
|
|
|
book = serializers.SerializerMethodField() |
|
|
|
author = serializers.SerializerMethodField() |
|
|
|
edition = serializers.SerializerMethodField() |
|
|
|
open_book_url = serializers.SerializerMethodField() |
|
|
|
citation_summary = serializers.SerializerMethodField() |
|
|
|
images = CorrectionReferenceImageSerializer(many=True, read_only=True) |
|
|
|
images_count = serializers.SerializerMethodField() |
|
|
|
|
|
|
|
class Meta: |
|
|
|
model = CorrectionReference |
|
|
|
fields = ['id', 'book_title', 'book_authors', 'edition_info', 'volume_info', 'hadith_number', 'volume', 'pages', 'address', 'url', 'images'] |
|
|
|
fields = [ |
|
|
|
'id', |
|
|
|
'book', |
|
|
|
'author', |
|
|
|
'edition', |
|
|
|
'volume_info', |
|
|
|
'hadith_number', |
|
|
|
'volume', |
|
|
|
'pages', |
|
|
|
'address', |
|
|
|
'url', |
|
|
|
'open_book_url', |
|
|
|
'citation_summary', |
|
|
|
'images', |
|
|
|
'images_count', |
|
|
|
'book_title', |
|
|
|
'book_authors', |
|
|
|
'edition_info', |
|
|
|
] |
|
|
|
|
|
|
|
def get_book(self, obj): |
|
|
|
if obj.book_reference: |
|
|
|
request = self.context.get('request') |
|
|
|
first_ed = obj.edition |
|
|
|
if not first_ed and getattr(obj, "book_volume", None) and obj.book_volume.edition: |
|
|
|
first_ed = obj.book_volume.edition |
|
|
|
if not first_ed and hasattr(obj.book_reference, "editions"): |
|
|
|
first_ed = obj.book_reference.editions.first() |
|
|
|
|
|
|
|
pub = getattr(obj.book_reference, "publisher", None) |
|
|
|
if pub is None and first_ed: |
|
|
|
pub = first_ed.publisher |
|
|
|
if isinstance(pub, list): |
|
|
|
pub = get_localized_text(pub, request) |
|
|
|
|
|
|
|
year = getattr(obj.book_reference, "year_of_publication", None) |
|
|
|
if not year and first_ed: |
|
|
|
year = first_ed.year_of_publication |
|
|
|
|
|
|
|
num_vols = getattr(obj.book_reference, "number_of_volumes", None) |
|
|
|
if num_vols is None and first_ed: |
|
|
|
num_vols = first_ed.number_of_volumes |
|
|
|
if num_vols is None and hasattr(obj.book_reference, "volumes"): |
|
|
|
num_vols = obj.book_reference.volumes.count() or None |
|
|
|
|
|
|
|
return { |
|
|
|
'id': obj.book_reference.id, |
|
|
|
'slug': obj.book_reference.slug, |
|
|
|
'title': get_localized_text(obj.book_reference.title, request), |
|
|
|
'publisher': pub or '', |
|
|
|
'year_of_publication': year or '', |
|
|
|
'number_of_volumes': num_vols, |
|
|
|
} |
|
|
|
return None |
|
|
|
|
|
|
|
def get_author(self, obj): |
|
|
|
if obj.book_reference and obj.book_reference.author: |
|
|
|
author = obj.book_reference.author |
|
|
|
request = self.context.get('request') |
|
|
|
return { |
|
|
|
'id': author.id, |
|
|
|
'slug': author.slug, |
|
|
|
'name': get_localized_text(author.name, request) |
|
|
|
} |
|
|
|
return None |
|
|
|
|
|
|
|
def get_edition(self, obj): |
|
|
|
edition = obj.edition |
|
|
|
if not edition and getattr(obj, "book_volume", None) and obj.book_volume.edition: |
|
|
|
edition = obj.book_volume.edition |
|
|
|
if not edition and getattr(obj, "book_reference", None) and hasattr(obj.book_reference, "editions"): |
|
|
|
edition = obj.book_reference.editions.first() |
|
|
|
if edition: |
|
|
|
request = self.context.get('request') |
|
|
|
pub = edition.publisher |
|
|
|
if isinstance(pub, list): |
|
|
|
pub = get_localized_text(pub, request) |
|
|
|
return { |
|
|
|
'id': edition.id, |
|
|
|
'edition_number': edition.edition_number, |
|
|
|
'publisher': pub or '', |
|
|
|
'year_of_publication': edition.year_of_publication or '', |
|
|
|
'number_of_volumes': edition.number_of_volumes, |
|
|
|
} |
|
|
|
return None |
|
|
|
|
|
|
|
def get_open_book_url(self, obj): |
|
|
|
request = self.context.get('request') |
|
|
|
if obj.url: |
|
|
|
return obj.url |
|
|
|
if obj.book_volume and obj.book_volume.file: |
|
|
|
return absolute_https_url(obj.book_volume.file.url, request) if request else obj.book_volume.file.url |
|
|
|
if obj.edition and obj.edition.source_url: |
|
|
|
return obj.edition.source_url |
|
|
|
if obj.book_reference and obj.book_reference.slug: |
|
|
|
return f"/arguments/sources/{obj.book_reference.slug}" |
|
|
|
return None |
|
|
|
|
|
|
|
volume = serializers.SerializerMethodField() |
|
|
|
pages = serializers.SerializerMethodField() |
|
|
|
hadith_number = serializers.SerializerMethodField() |
|
|
|
|
|
|
|
def get_volume(self, obj): |
|
|
|
if obj.volume and str(obj.volume).strip().lower() != "none": |
|
|
|
return obj.volume |
|
|
|
if obj.book_volume and obj.book_volume.title: |
|
|
|
return obj.book_volume.title |
|
|
|
return None |
|
|
|
|
|
|
|
def get_pages(self, obj): |
|
|
|
if obj.pages and str(obj.pages).strip().lower() != "none": |
|
|
|
return obj.pages |
|
|
|
return None |
|
|
|
|
|
|
|
def get_hadith_number(self, obj): |
|
|
|
if obj.hadith_number and str(obj.hadith_number).strip().lower() != "none": |
|
|
|
return obj.hadith_number |
|
|
|
return None |
|
|
|
|
|
|
|
def get_citation_summary(self, obj): |
|
|
|
parts = [] |
|
|
|
vol = self.get_volume(obj) |
|
|
|
pg = self.get_pages(obj) |
|
|
|
num = self.get_hadith_number(obj) |
|
|
|
if vol: |
|
|
|
vol_str = str(vol).strip() |
|
|
|
if vol_str.startswith("جلد"): |
|
|
|
parts.append(vol_str) |
|
|
|
else: |
|
|
|
parts.append(f"جلد {vol_str}") |
|
|
|
if pg: |
|
|
|
pg_str = str(pg).strip() |
|
|
|
if pg_str.startswith("ص") or pg_str.startswith("صفحه"): |
|
|
|
parts.append(pg_str) |
|
|
|
else: |
|
|
|
parts.append(f"صفحه {pg_str}") |
|
|
|
if num: |
|
|
|
num_str = str(num).strip() |
|
|
|
if num_str.startswith("ح") or num_str.startswith("حدیث") or num_str.startswith("شماره"): |
|
|
|
parts.append(num_str) |
|
|
|
else: |
|
|
|
parts.append(f"شماره حدیث {num_str}") |
|
|
|
return " • ".join(parts) if parts else "" |
|
|
|
|
|
|
|
def get_images_count(self, obj): |
|
|
|
if hasattr(obj, '_prefetched_objects_cache') and 'images' in obj._prefetched_objects_cache: |
|
|
|
return len(obj.images.all()) |
|
|
|
return obj.images.count() |
|
|
|
|
|
|
|
class DetailedInterpretationReferenceSerializer(BaseDetailedReferenceSerializer): |
|
|
|
images = InterpretationReferenceImageSerializer(many=True, read_only=True) |
|
|
|
|