From 7792b4e04de414d883bf0265791796ed185d6fdc Mon Sep 17 00:00:00 2001 From: mohsentaba Date: Sun, 26 Apr 2026 09:36:59 +0330 Subject: [PATCH] media time pattern reformat new utils file for handling formatting methods update podcast and video time formats with new helper methods --- apps/podcast/serializers.py | 39 +++++-------------------------------- apps/video/serializers.py | 30 ++++++++++++---------------- utils/time_formatters.py | 23 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 52 deletions(-) create mode 100644 utils/time_formatters.py diff --git a/apps/podcast/serializers.py b/apps/podcast/serializers.py index 12f8650..9797873 100755 --- a/apps/podcast/serializers.py +++ b/apps/podcast/serializers.py @@ -2,6 +2,7 @@ from rest_framework import serializers from utils import get_thumbs from apps.podcast.models import * from apps.bookmark.serializers import * +from utils.time_formatters import format_media_time, format_duration class PodcastCategoryListSerializer(serializers.ModelSerializer): @@ -40,13 +41,7 @@ class PodcastListSerializer(serializers.ModelSerializer): 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" + return format_media_time(obj.audio_time) def get_in_user_playlist(self, obj): """ @@ -98,12 +93,7 @@ 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') + return format_media_time(obj.audio_time) def get_user_rate(self, obj): """ @@ -218,19 +208,7 @@ class PodcastPlaylistListSerializer(serializers.ModelSerializer): return get_thumbs(obj.thumbnail, self.context.get('request')) def get_total_time_formatted(self, obj): - """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 - - # 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" + return format_duration(obj.total_time) def get_episodes_count(self, obj): """Return the number of episodes (podcasts) in this playlist""" @@ -256,14 +234,7 @@ class PodcastPlaylistDetailSerializer(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""" - 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" + return format_duration(obj.total_time) def get_bookmark(self, obj): """Get bookmark information for this playlist.""" diff --git a/apps/video/serializers.py b/apps/video/serializers.py index 0563257..daffbbd 100644 --- a/apps/video/serializers.py +++ b/apps/video/serializers.py @@ -2,6 +2,7 @@ from rest_framework import serializers from utils import get_thumbs from .models import VideoCategory, Video, VideoCollection, VideoPlaylist, PlaylistItem, PinnedVideoCollection from apps.bookmark.serializers import * +from utils.time_formatters import format_media_time, format_duration class VideoCategoryListSerializer(serializers.ModelSerializer): @@ -19,6 +20,7 @@ class VideoListSerializer(serializers.ModelSerializer): thumbnail = serializers.SerializerMethodField() video_file = serializers.SerializerMethodField() share_link = serializers.CharField(read_only=True) + video_time = serializers.SerializerMethodField() class Meta: model = Video @@ -37,6 +39,9 @@ class VideoListSerializer(serializers.ModelSerializer): return obj.video_file.url return None + def get_video_time(self, obj): + return format_media_time(obj.video_time) + class VideoPlaylistListSerializer(serializers.ModelSerializer): thumbnail = serializers.SerializerMethodField() @@ -50,15 +55,7 @@ class VideoPlaylistListSerializer(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""" - 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" - + return format_duration(obj.total_time) @@ -83,14 +80,7 @@ class VideoPlaylistDetailSerializer(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""" - 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" + return format_duration(obj.total_time) def get_bookmark(self, obj): """Get bookmark information for this playlist.""" @@ -156,13 +146,17 @@ class VideoDetailSerializer(serializers.ModelSerializer): is_in_playlist = serializers.SerializerMethodField() playlist_videos = serializers.SerializerMethodField() share_link = serializers.CharField(read_only=True) + video_time = serializers.SerializerMethodField() class Meta: model = Video fields = ['id', 'title', 'slug', 'thumbnail', 'description', 'video_type', 'video_file', 'video_url', 'video_time', 'view_count', 'categories', 'created_at', 'user_rate', 'average_rate', 'bookmark', - 'is_in_playlist', 'playlist_videos', 'share_link'] + 'is_in_playlist', 'playlist_videos', 'share_link', 'video_time'] + + def get_video_time(self, obj): + return format_media_time(obj.video_time) def get_thumbnail(self, obj): return get_thumbs(obj.thumbnail, self.context.get('request')) diff --git a/utils/time_formatters.py b/utils/time_formatters.py new file mode 100644 index 0000000..5604dde --- /dev/null +++ b/utils/time_formatters.py @@ -0,0 +1,23 @@ +# backend/utils/formatters.py + +def format_media_time(time_obj): + """Formats a datetime.time object to MM:SS or HH:MM:SS""" + if not time_obj: + return None + if time_obj.hour == 0: + return time_obj.strftime('%M:%S') + return time_obj.strftime('%H:%M:%S') + +def format_duration(timedelta_obj): + """Formats a datetime.timedelta object to MM:SS or HH:MM:SS""" + if not timedelta_obj: + return "00:00" + + total_seconds = int(timedelta_obj.total_seconds()) + hours = total_seconds // 3600 + minutes = (total_seconds % 3600) // 60 + seconds = total_seconds % 60 + + if hours > 0: + return f"{hours:02d}:{minutes:02d}:{seconds:02d}" + return f"{minutes:02d}:{seconds:02d}" \ No newline at end of file