|
|
|
@ -20,6 +20,7 @@ class PodcastListSerializer(serializers.ModelSerializer): |
|
|
|
audio_file = serializers.SerializerMethodField() |
|
|
|
in_user_playlist = serializers.SerializerMethodField() |
|
|
|
share_link = serializers.CharField(read_only=True) |
|
|
|
audio_time = serializers.SerializerMethodField() |
|
|
|
|
|
|
|
class Meta: |
|
|
|
model = Podcast |
|
|
|
@ -38,6 +39,15 @@ class PodcastListSerializer(serializers.ModelSerializer): |
|
|
|
return obj.audio_file.url |
|
|
|
return None |
|
|
|
|
|
|
|
def get_audio_time(self, obj): |
|
|
|
if not obj.audio_time: |
|
|
|
return None |
|
|
|
|
|
|
|
# obj.audio_time is a datetime.time object |
|
|
|
if obj.audio_time.hour == 0: |
|
|
|
return obj.audio_time.strftime('%M:%S') # Returns "44:00" |
|
|
|
return obj.audio_time.strftime('%H:%M:%S') # Returns "01:44:00" |
|
|
|
|
|
|
|
def get_in_user_playlist(self, obj): |
|
|
|
""" |
|
|
|
Check if the podcast is in the user's personal playlist. |
|
|
|
@ -62,7 +72,7 @@ class PodcastDetailSerializer(serializers.ModelSerializer): |
|
|
|
playlist_podcasts = serializers.SerializerMethodField() |
|
|
|
in_user_playlist = serializers.SerializerMethodField() |
|
|
|
share_link = serializers.CharField(read_only=True) |
|
|
|
|
|
|
|
audio_time = serializers.SerializerMethodField() |
|
|
|
class Meta: |
|
|
|
model = Podcast |
|
|
|
fields = ['id', 'title', 'slug', 'thumbnail', 'description', |
|
|
|
@ -87,6 +97,14 @@ class PodcastDetailSerializer(serializers.ModelSerializer): |
|
|
|
) |
|
|
|
return book_mark.get('is_bookmarked', False) |
|
|
|
|
|
|
|
def get_audio_time(self, obj): |
|
|
|
if not obj.audio_time: |
|
|
|
return None |
|
|
|
|
|
|
|
if obj.audio_time.hour == 0: |
|
|
|
return obj.audio_time.strftime('%M:%S') |
|
|
|
return obj.audio_time.strftime('%H:%M:%S') |
|
|
|
|
|
|
|
def get_user_rate(self, obj): |
|
|
|
""" |
|
|
|
Get rate information for this podcast from the current user. |
|
|
|
@ -200,14 +218,19 @@ class PodcastPlaylistListSerializer(serializers.ModelSerializer): |
|
|
|
return get_thumbs(obj.thumbnail, self.context.get('request')) |
|
|
|
|
|
|
|
def get_total_time_formatted(self, obj): |
|
|
|
"""Format total_time as HH:MM:SS string""" |
|
|
|
"""Format total_time dynamically as MM:SS or HH:MM:SS""" |
|
|
|
if obj.total_time: |
|
|
|
total_seconds = int(obj.total_time.total_seconds()) |
|
|
|
hours = total_seconds // 3600 |
|
|
|
minutes = (total_seconds % 3600) // 60 |
|
|
|
seconds = total_seconds % 60 |
|
|
|
return f"{hours:02d}:{minutes:02d}:{seconds:02d}" |
|
|
|
return "00:00:00" |
|
|
|
|
|
|
|
# Strip hours if the playlist is under 60 mins |
|
|
|
if hours > 0: |
|
|
|
return f"{hours:02d}:{minutes:02d}:{seconds:02d}" |
|
|
|
return f"{minutes:02d}:{seconds:02d}" |
|
|
|
|
|
|
|
return "00:00" |
|
|
|
|
|
|
|
def get_episodes_count(self, obj): |
|
|
|
"""Return the number of episodes (podcasts) in this playlist""" |
|
|
|
|