From 749260079c5b697864dd72795acb99df8b4902ed Mon Sep 17 00:00:00 2001 From: mohsentaba Date: Wed, 2 Sep 2026 14:53:15 +0330 Subject: [PATCH] feat(videos and podcasts):thumbnail returns from playlist objects --- apps/podcast/models.py | 16 ++++++++++++++++ apps/podcast/serializers.py | 6 ++++-- apps/video/models.py | 16 ++++++++++++++++ apps/video/serializers.py | 6 ++++-- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/apps/podcast/models.py b/apps/podcast/models.py index 5366cb7..9157c0e 100644 --- a/apps/podcast/models.py +++ b/apps/podcast/models.py @@ -196,6 +196,22 @@ class PodcastPlaylist(LowercaseSlugMixin, models.Model): return timedelta(seconds=total_seconds) + @property + def effective_thumbnail(self): + """ + Get the thumbnail for this playlist. + If playlist has no explicit thumbnail, fall back to the first podcast with a thumbnail. + """ + if self.thumbnail: + return self.thumbnail + first_item = self.playlist_items.filter( + podcast__thumbnail__isnull=False, + podcast__status=True + ).exclude(podcast__thumbnail='').select_related('podcast').order_by('priority').first() + if first_item and first_item.podcast and first_item.podcast.thumbnail: + return first_item.podcast.thumbnail + return None + def save(self, *args, **kwargs): super().save(*args, **kwargs) diff --git a/apps/podcast/serializers.py b/apps/podcast/serializers.py index 9642fa3..adc5cd0 100755 --- a/apps/podcast/serializers.py +++ b/apps/podcast/serializers.py @@ -205,7 +205,8 @@ class PodcastPlaylistListSerializer(serializers.ModelSerializer): fields = ['id', 'title', 'slug', 'thumbnail', 'slogan', 'view_count', 'total_time_formatted', 'episodes_count', 'order', 'created_at'] def get_thumbnail(self, obj): - return get_thumbs(obj.thumbnail, self.context.get('request')) + thumb = getattr(obj, 'effective_thumbnail', obj.thumbnail) + return get_thumbs(thumb, self.context.get('request')) def get_total_time_formatted(self, obj): return format_duration(obj.total_time) @@ -231,7 +232,8 @@ class PodcastPlaylistDetailSerializer(serializers.ModelSerializer): 'categories', 'created_at', 'user_rate', 'average_rate', 'bookmark', 'podcasts'] def get_thumbnail(self, obj): - return get_thumbs(obj.thumbnail, self.context.get('request')) + thumb = getattr(obj, 'effective_thumbnail', obj.thumbnail) + return get_thumbs(thumb, self.context.get('request')) def get_total_time_formatted(self, obj): return format_duration(obj.total_time) diff --git a/apps/video/models.py b/apps/video/models.py index aeb8766..dd7bd79 100644 --- a/apps/video/models.py +++ b/apps/video/models.py @@ -269,6 +269,22 @@ class VideoPlaylist(LowercaseSlugMixin, models.Model): return timedelta(seconds=total_seconds) + @property + def effective_thumbnail(self): + """ + Get the thumbnail for this playlist. + If playlist has no explicit thumbnail, fall back to the first video with a thumbnail. + """ + if self.thumbnail: + return self.thumbnail + first_item = self.playlist_items.filter( + video__thumbnail__isnull=False, + video__status=True + ).exclude(video__thumbnail='').select_related('video').order_by('priority').first() + if first_item and first_item.video and first_item.video.thumbnail: + return first_item.video.thumbnail + return None + class Meta: verbose_name = _('Video Playlist') verbose_name_plural = _('Video Playlists') diff --git a/apps/video/serializers.py b/apps/video/serializers.py index 526fece..9f632c2 100644 --- a/apps/video/serializers.py +++ b/apps/video/serializers.py @@ -75,7 +75,8 @@ class VideoPlaylistListSerializer(serializers.ModelSerializer): fields = ['id', 'title', 'slug', 'thumbnail', 'slogan', 'view_count', 'video_time', 'total_time_formatted', 'order', 'created_at'] def get_thumbnail(self, obj): - return get_thumbs(obj.thumbnail, self.context.get('request')) + thumb = getattr(obj, 'effective_thumbnail', obj.thumbnail) + return get_thumbs(thumb, self.context.get('request')) def get_video_time(self, obj): return format_duration(obj.total_time) @@ -104,7 +105,8 @@ class VideoPlaylistDetailSerializer(serializers.ModelSerializer): 'categories', 'created_at', 'user_rate', 'average_rate', 'bookmark', 'videos'] def get_thumbnail(self, obj): - return get_thumbs(obj.thumbnail, self.context.get('request')) + thumb = getattr(obj, 'effective_thumbnail', obj.thumbnail) + return get_thumbs(thumb, self.context.get('request')) def get_video_time(self, obj): return format_duration(obj.total_time)