ref: move the duration format to utils
Parent
d5f6830bb3
révision
15eb7e600a
|
@ -6,7 +6,7 @@ from django.test.html import parse_html
|
|||
import pytest
|
||||
from wagtail_factories import ImageFactory
|
||||
|
||||
from wagtail_webradio.components.player import PlayerView, Playlist, Song
|
||||
from wagtail_webradio.components.player import PlayerView, Playlist
|
||||
|
||||
from .factories import PodcastFactory
|
||||
from .utils import prettify_html
|
||||
|
@ -20,22 +20,6 @@ def player():
|
|||
return component
|
||||
|
||||
|
||||
class TestSong:
|
||||
@pytest.mark.parametrize(
|
||||
'duration, result',
|
||||
[
|
||||
(None, '--:--'),
|
||||
(timedelta(), '--:--'),
|
||||
(timedelta(seconds=2), '00:02'),
|
||||
(timedelta(minutes=3), '03:00'),
|
||||
(timedelta(minutes=12, seconds=10), '12:10'),
|
||||
(timedelta(hours=1, seconds=10), '01:00:10'),
|
||||
],
|
||||
)
|
||||
def test_duration_str(self, duration, result):
|
||||
assert Song('t', 's', 'u', duration=duration).duration_str == result
|
||||
|
||||
|
||||
class TestPlaylist:
|
||||
def test_iter_items(self):
|
||||
song = {
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
from datetime import timedelta
|
||||
|
||||
import pytest
|
||||
|
||||
from wagtail_webradio.utils import format_duration
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'duration, result',
|
||||
[
|
||||
(None, '--:--'),
|
||||
(timedelta(), '--:--'),
|
||||
(timedelta(seconds=2), '00:02'),
|
||||
(timedelta(minutes=3), '03:00'),
|
||||
(timedelta(minutes=12, seconds=10), '12:10'),
|
||||
(timedelta(hours=1, seconds=10), '01:00:10'),
|
||||
],
|
||||
)
|
||||
def test_format_duration(duration, result):
|
||||
assert format_duration(duration) == result
|
|
@ -9,6 +9,7 @@ from django.utils.functional import cached_property
|
|||
from django_unicorn.components import UnicornView
|
||||
|
||||
from ..models import Podcast
|
||||
from ..utils import format_duration
|
||||
|
||||
|
||||
@dataclass
|
||||
|
@ -22,17 +23,7 @@ class Song:
|
|||
|
||||
@cached_property
|
||||
def duration_str(self):
|
||||
if not self.duration:
|
||||
return '--:--'
|
||||
result = []
|
||||
seconds = self.duration.total_seconds()
|
||||
if seconds >= 3600:
|
||||
hours, seconds = divmod(seconds, 3600)
|
||||
result.append('{:02n}'.format(hours))
|
||||
minutes, seconds = divmod(seconds, 60)
|
||||
result.append('{:02n}'.format(minutes))
|
||||
result.append('{:02n}'.format(seconds))
|
||||
return ':'.join(result)
|
||||
return format_duration(self.duration)
|
||||
|
||||
|
||||
class Playlist(Sequence):
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
from datetime import timedelta
|
||||
|
||||
|
||||
def format_duration(value: timedelta) -> str:
|
||||
"""Format a timedelta to a duration string as HH:MM:SS or MM:SS."""
|
||||
if not value:
|
||||
return '--:--'
|
||||
result = []
|
||||
seconds = value.total_seconds()
|
||||
if seconds >= 3600:
|
||||
hours, seconds = divmod(seconds, 3600)
|
||||
result.append('{:02n}'.format(hours))
|
||||
minutes, seconds = divmod(seconds, 60)
|
||||
result.append('{:02n}'.format(minutes))
|
||||
result.append('{:02n}'.format(seconds))
|
||||
return ':'.join(result)
|
Référencer dans un nouveau ticket