|
|
|
@ -77,6 +77,10 @@ class AdminPodcastListSerializer(serializers.ModelSerializer): |
|
|
|
slug = serializers.CharField(required=False, allow_blank=True, allow_null=True) |
|
|
|
categories_detail = AdminPodcastCategorySerializer(source="categories", many=True, read_only=True) |
|
|
|
collections_detail = AdminPodcastCollectionSerializer(source="collections", many=True, read_only=True) |
|
|
|
is_external = serializers.SerializerMethodField(read_only=True) |
|
|
|
|
|
|
|
def get_is_external(self, obj): |
|
|
|
return bool(not obj.audio_file and getattr(obj, 'external_audio_url', None)) |
|
|
|
|
|
|
|
class Meta: |
|
|
|
model = Podcast |
|
|
|
@ -95,10 +99,22 @@ class AdminPodcastListSerializer(serializers.ModelSerializer): |
|
|
|
"audio_file", |
|
|
|
"external_audio_url", |
|
|
|
"external_thumbnail_url", |
|
|
|
"is_external", |
|
|
|
"rss_guid" |
|
|
|
] |
|
|
|
read_only_fields = ["id", "view_count", "download_count", "created_at", "updated_at"] |
|
|
|
|
|
|
|
def to_representation(self, instance): |
|
|
|
data = super().to_representation(instance) |
|
|
|
from .serializers import build_podcast_audio_url |
|
|
|
request = self.context.get("request") |
|
|
|
if not data.get("thumbnail") and instance.external_thumbnail_url: |
|
|
|
data["thumbnail"] = instance.external_thumbnail_url |
|
|
|
if not data.get("audio_file") and instance.external_audio_url: |
|
|
|
data["audio_file"] = build_podcast_audio_url(instance, request) |
|
|
|
data["is_external"] = bool(not instance.audio_file and instance.external_audio_url) |
|
|
|
return data |
|
|
|
|
|
|
|
class AdminPodcastDetailSerializer(serializers.ModelSerializer): |
|
|
|
thumbnail = AbsoluteImageField(required=False, allow_null=True) |
|
|
|
audio_file = AbsoluteFileField(required=False, allow_null=True) |
|
|
|
@ -108,10 +124,18 @@ class AdminPodcastDetailSerializer(serializers.ModelSerializer): |
|
|
|
collections = serializers.PrimaryKeyRelatedField(queryset=PodcastCollection.objects.all(), many=True, required=False) |
|
|
|
categories_detail = AdminPodcastCategorySerializer(source="categories", many=True, read_only=True) |
|
|
|
collections_detail = AdminPodcastCollectionSerializer(source="collections", many=True, read_only=True) |
|
|
|
is_external = serializers.SerializerMethodField(read_only=True) |
|
|
|
|
|
|
|
def get_is_external(self, obj): |
|
|
|
return bool(not obj.audio_file and getattr(obj, 'external_audio_url', None)) |
|
|
|
remove_thumbnail = serializers.BooleanField(write_only=True, required=False, default=False) |
|
|
|
remove_audio_file = serializers.BooleanField(write_only=True, required=False, default=False) |
|
|
|
|
|
|
|
bookmark_count = serializers.SerializerMethodField() |
|
|
|
is_external = serializers.SerializerMethodField(read_only=True) |
|
|
|
|
|
|
|
def get_is_external(self, obj): |
|
|
|
return bool(not obj.audio_file and getattr(obj, 'external_audio_url', None)) |
|
|
|
|
|
|
|
class Meta: |
|
|
|
model = Podcast |
|
|
|
@ -135,12 +159,24 @@ class AdminPodcastDetailSerializer(serializers.ModelSerializer): |
|
|
|
"collections_detail", |
|
|
|
"external_audio_url", |
|
|
|
"external_thumbnail_url", |
|
|
|
"is_external", |
|
|
|
"rss_guid", |
|
|
|
"remove_thumbnail", |
|
|
|
"remove_audio_file", |
|
|
|
] |
|
|
|
read_only_fields = ["id", "view_count", "download_count", "bookmark_count", "created_at", "updated_at"] |
|
|
|
|
|
|
|
def to_representation(self, instance): |
|
|
|
data = super().to_representation(instance) |
|
|
|
from .serializers import build_podcast_audio_url |
|
|
|
request = self.context.get("request") |
|
|
|
if not data.get("thumbnail") and instance.external_thumbnail_url: |
|
|
|
data["thumbnail"] = instance.external_thumbnail_url |
|
|
|
if not data.get("audio_file") and instance.external_audio_url: |
|
|
|
data["audio_file"] = build_podcast_audio_url(instance, request) |
|
|
|
data["is_external"] = bool(not instance.audio_file and instance.external_audio_url) |
|
|
|
return data |
|
|
|
|
|
|
|
def get_bookmark_count(self, obj): |
|
|
|
from apps.bookmark.models.bookmark import Bookmark |
|
|
|
return Bookmark.objects.filter(service=Bookmark.ServiceChoices.PODCAST, content_id=obj.id, status=True).count() |
|
|
|
@ -224,6 +260,10 @@ class AdminPodcastPlaylistListSerializer(serializers.ModelSerializer): |
|
|
|
slug = serializers.CharField(required=False, allow_blank=True, allow_null=True) |
|
|
|
categories_detail = AdminPodcastCategorySerializer(source="categories", many=True, read_only=True) |
|
|
|
collections_detail = AdminPodcastCollectionSerializer(source="collections", many=True, read_only=True) |
|
|
|
is_external = serializers.SerializerMethodField(read_only=True) |
|
|
|
|
|
|
|
def get_is_external(self, obj): |
|
|
|
return bool(not obj.audio_file and getattr(obj, 'external_audio_url', None)) |
|
|
|
items_count = serializers.SerializerMethodField() |
|
|
|
|
|
|
|
class Meta: |
|
|
|
@ -261,10 +301,18 @@ class AdminPodcastPlaylistDetailSerializer(serializers.ModelSerializer): |
|
|
|
collections = serializers.PrimaryKeyRelatedField(queryset=PodcastCollection.objects.all(), many=True, required=False) |
|
|
|
categories_detail = AdminPodcastCategorySerializer(source="categories", many=True, read_only=True) |
|
|
|
collections_detail = AdminPodcastCollectionSerializer(source="collections", many=True, read_only=True) |
|
|
|
is_external = serializers.SerializerMethodField(read_only=True) |
|
|
|
|
|
|
|
def get_is_external(self, obj): |
|
|
|
return bool(not obj.audio_file and getattr(obj, 'external_audio_url', None)) |
|
|
|
playlist_items_detail = AdminPodcastPlaylistItemSerializer(source="playlist_items", many=True, read_only=True) |
|
|
|
remove_thumbnail = serializers.BooleanField(write_only=True, required=False, default=False) |
|
|
|
|
|
|
|
bookmark_count = serializers.SerializerMethodField() |
|
|
|
is_external = serializers.SerializerMethodField(read_only=True) |
|
|
|
|
|
|
|
def get_is_external(self, obj): |
|
|
|
return bool(not obj.audio_file and getattr(obj, 'external_audio_url', None)) |
|
|
|
|
|
|
|
class Meta: |
|
|
|
model = PodcastPlaylist |
|
|
|
|