Browse Source

media time pattern reformat

new utils file for handling formatting methods

update podcast and video time formats with new helper methods
master
Mohsen Taba 4 weeks ago
parent
commit
7792b4e04d
  1. 39
      apps/podcast/serializers.py
  2. 30
      apps/video/serializers.py
  3. 23
      utils/time_formatters.py

39
apps/podcast/serializers.py

@ -2,6 +2,7 @@ from rest_framework import serializers
from utils import get_thumbs from utils import get_thumbs
from apps.podcast.models import * from apps.podcast.models import *
from apps.bookmark.serializers import * from apps.bookmark.serializers import *
from utils.time_formatters import format_media_time, format_duration
class PodcastCategoryListSerializer(serializers.ModelSerializer): class PodcastCategoryListSerializer(serializers.ModelSerializer):
@ -40,13 +41,7 @@ class PodcastListSerializer(serializers.ModelSerializer):
return None return None
def get_audio_time(self, obj): 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): def get_in_user_playlist(self, obj):
""" """
@ -98,12 +93,7 @@ class PodcastDetailSerializer(serializers.ModelSerializer):
return book_mark.get('is_bookmarked', False) return book_mark.get('is_bookmarked', False)
def get_audio_time(self, obj): 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): def get_user_rate(self, obj):
""" """
@ -218,19 +208,7 @@ class PodcastPlaylistListSerializer(serializers.ModelSerializer):
return get_thumbs(obj.thumbnail, self.context.get('request')) return get_thumbs(obj.thumbnail, self.context.get('request'))
def get_total_time_formatted(self, obj): 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): def get_episodes_count(self, obj):
"""Return the number of episodes (podcasts) in this playlist""" """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')) return get_thumbs(obj.thumbnail, self.context.get('request'))
def get_total_time_formatted(self, obj): 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): def get_bookmark(self, obj):
"""Get bookmark information for this playlist.""" """Get bookmark information for this playlist."""

30
apps/video/serializers.py

@ -2,6 +2,7 @@ from rest_framework import serializers
from utils import get_thumbs from utils import get_thumbs
from .models import VideoCategory, Video, VideoCollection, VideoPlaylist, PlaylistItem, PinnedVideoCollection from .models import VideoCategory, Video, VideoCollection, VideoPlaylist, PlaylistItem, PinnedVideoCollection
from apps.bookmark.serializers import * from apps.bookmark.serializers import *
from utils.time_formatters import format_media_time, format_duration
class VideoCategoryListSerializer(serializers.ModelSerializer): class VideoCategoryListSerializer(serializers.ModelSerializer):
@ -19,6 +20,7 @@ class VideoListSerializer(serializers.ModelSerializer):
thumbnail = serializers.SerializerMethodField() thumbnail = serializers.SerializerMethodField()
video_file = serializers.SerializerMethodField() video_file = serializers.SerializerMethodField()
share_link = serializers.CharField(read_only=True) share_link = serializers.CharField(read_only=True)
video_time = serializers.SerializerMethodField()
class Meta: class Meta:
model = Video model = Video
@ -37,6 +39,9 @@ class VideoListSerializer(serializers.ModelSerializer):
return obj.video_file.url return obj.video_file.url
return None return None
def get_video_time(self, obj):
return format_media_time(obj.video_time)
class VideoPlaylistListSerializer(serializers.ModelSerializer): class VideoPlaylistListSerializer(serializers.ModelSerializer):
thumbnail = serializers.SerializerMethodField() thumbnail = serializers.SerializerMethodField()
@ -50,15 +55,7 @@ class VideoPlaylistListSerializer(serializers.ModelSerializer):
return get_thumbs(obj.thumbnail, self.context.get('request')) return get_thumbs(obj.thumbnail, self.context.get('request'))
def get_total_time_formatted(self, obj): 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')) return get_thumbs(obj.thumbnail, self.context.get('request'))
def get_total_time_formatted(self, obj): 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): def get_bookmark(self, obj):
"""Get bookmark information for this playlist.""" """Get bookmark information for this playlist."""
@ -156,13 +146,17 @@ class VideoDetailSerializer(serializers.ModelSerializer):
is_in_playlist = serializers.SerializerMethodField() is_in_playlist = serializers.SerializerMethodField()
playlist_videos = serializers.SerializerMethodField() playlist_videos = serializers.SerializerMethodField()
share_link = serializers.CharField(read_only=True) share_link = serializers.CharField(read_only=True)
video_time = serializers.SerializerMethodField()
class Meta: class Meta:
model = Video model = Video
fields = ['id', 'title', 'slug', 'thumbnail', 'description', 'video_type', fields = ['id', 'title', 'slug', 'thumbnail', 'description', 'video_type',
'video_file', 'video_url', 'video_time', 'view_count', 'video_file', 'video_url', 'video_time', 'view_count',
'categories', 'created_at', 'user_rate', 'average_rate', 'bookmark', '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): def get_thumbnail(self, obj):
return get_thumbs(obj.thumbnail, self.context.get('request')) return get_thumbs(obj.thumbnail, self.context.get('request'))

23
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}"
Loading…
Cancel
Save