|
|
@ -1,5 +1,6 @@ |
|
|
from rest_framework import serializers |
|
|
from rest_framework import serializers |
|
|
from django.core.files.uploadedfile import SimpleUploadedFile |
|
|
from django.core.files.uploadedfile import SimpleUploadedFile |
|
|
|
|
|
from django.utils.translation import gettext_lazy as _ |
|
|
|
|
|
|
|
|
from utils.image_compression import maybe_compress_uploaded_file |
|
|
from utils.image_compression import maybe_compress_uploaded_file |
|
|
from utils import absolute_https_url |
|
|
from utils import absolute_https_url |
|
|
@ -41,11 +42,30 @@ class AdminHadisCategorySerializer(serializers.ModelSerializer): |
|
|
sect_type = serializers.CharField(source='sect.sect_type', read_only=True) |
|
|
sect_type = serializers.CharField(source='sect.sect_type', read_only=True) |
|
|
title = LocalizedField() |
|
|
title = LocalizedField() |
|
|
description = LocalizedField() |
|
|
description = LocalizedField() |
|
|
|
|
|
has_children = serializers.SerializerMethodField() |
|
|
|
|
|
is_leaf = serializers.SerializerMethodField() |
|
|
|
|
|
has_hadith = serializers.SerializerMethodField() |
|
|
|
|
|
|
|
|
|
|
|
def get_has_children(self, obj): |
|
|
|
|
|
if hasattr(obj, "annotated_has_children"): |
|
|
|
|
|
return bool(obj.annotated_has_children) |
|
|
|
|
|
return obj.children.exists() |
|
|
|
|
|
|
|
|
|
|
|
def get_is_leaf(self, obj): |
|
|
|
|
|
if hasattr(obj, "annotated_has_children"): |
|
|
|
|
|
return not bool(obj.annotated_has_children) |
|
|
|
|
|
return not obj.children.exists() |
|
|
|
|
|
|
|
|
|
|
|
def get_has_hadith(self, obj): |
|
|
|
|
|
if hasattr(obj, "annotated_has_hadith"): |
|
|
|
|
|
return bool(obj.annotated_has_hadith) |
|
|
|
|
|
return obj.hadis_set.exists() |
|
|
|
|
|
|
|
|
class Meta: |
|
|
class Meta: |
|
|
model = HadisCategory |
|
|
model = HadisCategory |
|
|
fields = [ |
|
|
fields = [ |
|
|
"id", |
|
|
"id", |
|
|
|
|
|
"parent", |
|
|
"title", |
|
|
"title", |
|
|
"slug", |
|
|
"slug", |
|
|
"source_type", |
|
|
"source_type", |
|
|
@ -53,6 +73,9 @@ class AdminHadisCategorySerializer(serializers.ModelSerializer): |
|
|
"sect", |
|
|
"sect", |
|
|
"sect_type", |
|
|
"sect_type", |
|
|
"description", |
|
|
"description", |
|
|
|
|
|
"has_children", |
|
|
|
|
|
"is_leaf", |
|
|
|
|
|
"has_hadith", |
|
|
] |
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -240,6 +263,13 @@ class AdminHadisDetailSerializer(serializers.ModelSerializer): |
|
|
from apps.bookmark.models.bookmark import Bookmark |
|
|
from apps.bookmark.models.bookmark import Bookmark |
|
|
return Bookmark.objects.filter(service=Bookmark.ServiceChoices.HADITH, content_id=obj.id, status=True).count() |
|
|
return Bookmark.objects.filter(service=Bookmark.ServiceChoices.HADITH, content_id=obj.id, status=True).count() |
|
|
|
|
|
|
|
|
|
|
|
def validate_category(self, value): |
|
|
|
|
|
if value and value.children.exists(): |
|
|
|
|
|
raise serializers.ValidationError( |
|
|
|
|
|
_("Cannot assign a hadith/item to a category that has subcategories. Only leaf categories can contain hadiths.") |
|
|
|
|
|
) |
|
|
|
|
|
return value |
|
|
|
|
|
|
|
|
def get_collections_detail(self, obj): |
|
|
def get_collections_detail(self, obj): |
|
|
collections = [item.collection for item in obj.collection_items.all()] |
|
|
collections = [item.collection for item in obj.collection_items.all()] |
|
|
return AdminHadisCollectionSerializer(collections, many=True).data |
|
|
return AdminHadisCollectionSerializer(collections, many=True).data |
|
|
@ -607,6 +637,8 @@ class AdminHadisCategoryArchiveSerializer(serializers.ModelSerializer): |
|
|
xmind_file = AbsoluteFileField(required=False, allow_null=True) |
|
|
xmind_file = AbsoluteFileField(required=False, allow_null=True) |
|
|
remove_xmind_file = serializers.BooleanField(write_only=True, required=False, default=False) |
|
|
remove_xmind_file = serializers.BooleanField(write_only=True, required=False, default=False) |
|
|
has_hadith = serializers.SerializerMethodField() |
|
|
has_hadith = serializers.SerializerMethodField() |
|
|
|
|
|
has_children = serializers.SerializerMethodField() |
|
|
|
|
|
is_leaf = serializers.SerializerMethodField() |
|
|
|
|
|
|
|
|
parent = serializers.PrimaryKeyRelatedField( |
|
|
parent = serializers.PrimaryKeyRelatedField( |
|
|
queryset=HadisCategory.objects.all(), |
|
|
queryset=HadisCategory.objects.all(), |
|
|
@ -619,8 +651,27 @@ class AdminHadisCategoryArchiveSerializer(serializers.ModelSerializer): |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
def get_has_hadith(self, obj): |
|
|
def get_has_hadith(self, obj): |
|
|
|
|
|
if hasattr(obj, "annotated_has_hadith"): |
|
|
|
|
|
return bool(obj.annotated_has_hadith) |
|
|
return obj.hadis_set.exists() |
|
|
return obj.hadis_set.exists() |
|
|
|
|
|
|
|
|
|
|
|
def get_has_children(self, obj): |
|
|
|
|
|
if hasattr(obj, "annotated_has_children"): |
|
|
|
|
|
return bool(obj.annotated_has_children) |
|
|
|
|
|
return obj.children.exists() |
|
|
|
|
|
|
|
|
|
|
|
def get_is_leaf(self, obj): |
|
|
|
|
|
if hasattr(obj, "annotated_has_children"): |
|
|
|
|
|
return not bool(obj.annotated_has_children) |
|
|
|
|
|
return not obj.children.exists() |
|
|
|
|
|
|
|
|
|
|
|
def validate_parent(self, value): |
|
|
|
|
|
if value and value.hadis_set.exists(): |
|
|
|
|
|
raise serializers.ValidationError( |
|
|
|
|
|
_("Cannot set parent to a category that already contains hadiths/content items.") |
|
|
|
|
|
) |
|
|
|
|
|
return value |
|
|
|
|
|
|
|
|
class Meta: |
|
|
class Meta: |
|
|
model = HadisCategory |
|
|
model = HadisCategory |
|
|
fields = [ |
|
|
fields = [ |
|
|
@ -639,6 +690,8 @@ class AdminHadisCategoryArchiveSerializer(serializers.ModelSerializer): |
|
|
"share_link", |
|
|
"share_link", |
|
|
"xmind_share_link", |
|
|
"xmind_share_link", |
|
|
"has_hadith", |
|
|
"has_hadith", |
|
|
|
|
|
"has_children", |
|
|
|
|
|
"is_leaf", |
|
|
] |
|
|
] |
|
|
read_only_fields = ["id", "share_link", "xmind_share_link"] |
|
|
read_only_fields = ["id", "share_link", "xmind_share_link"] |
|
|
|
|
|
|
|
|
|