Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display player errors and prepare for client websockets #155

Merged
merged 18 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,17 @@ Any important notes regarding the update.

- MacOS support.
- Added Python 3.12 support.
- Player errors can be accessed in a library fashion (using pagination).

### Changed

- Playlist is integrated to the digest view in its minimal form.
- Played and queuing playlist entries can be accessed in a library fashion (using pagination), their routes are `playlist/played` and `playlist/queuing`.

### Removed

- Dropped Python 3.7 support.
- Playlist entries in digest view don't have pre-calculated date of play any more. This data has to be calculated by the front now.

## 1.8.1 - 2023-12-17

Expand Down
18 changes: 9 additions & 9 deletions dakara_server/dakara_server/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@
name="playlist-digest",
),
path(
"api/playlist/entries/",
playlist_views.PlaylistEntryListView.as_view(),
name="playlist-entries-list",
"api/playlist/queuing/",
playlist_views.PlaylistQueuingListView.as_view(),
name="playlist-queuing-list",
),
path(
"api/playlist/entries/<int:pk>/",
playlist_views.PlaylistEntryView.as_view(),
name="playlist-entries",
"api/playlist/queuing/<int:pk>/",
playlist_views.PlaylistQueuingView.as_view(),
name="playlist-queuing",
),
path(
"api/playlist/played-entries/",
playlist_views.PlaylistPlayedEntryListView.as_view(),
name="playlist-played-entries-list",
"api/playlist/played/",
playlist_views.PlaylistPlayedListView.as_view(),
name="playlist-played-list",
),
path(
"api/playlist/karaoke/",
Expand Down
13 changes: 12 additions & 1 deletion dakara_server/library/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class Meta:
"work_type",
"song_count",
)
read_only_fiels = ("id", "song_count")
read_only_fields = ("id", "song_count")

@staticmethod
def get_song_count(work):
Expand Down Expand Up @@ -440,6 +440,17 @@ class Meta:
read_only_fields = ("id", "filename", "directory")


class SongForDigestSerializer(serializers.ModelSerializer):
"""Song serializer for playlist digest info."""

duration = SecondsDurationField()

class Meta:
model = Song
fields = ("id", "title", "duration")
read_only_fields = ("id", "title", "duration")


class WorkForFeederSerializer(serializers.ModelSerializer):
"""Work serializer for the feeder."""

Expand Down
4 changes: 2 additions & 2 deletions dakara_server/playlist/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def connect(self):
# reset current playing playlist entry if any
current_playlist_entry = models.PlaylistEntry.objects.get_playing()
if current_playlist_entry is not None:
current_playlist_entry.date_played = None
current_playlist_entry.date_play = None
current_playlist_entry.save()

# register the channel in database
Expand All @@ -136,7 +136,7 @@ def disconnect(self, close_code):
# reset the current playing song if any
entry = models.PlaylistEntry.objects.get_playing()
if entry:
entry.date_played = None
entry.date_play = None
entry.save()

# reset the player
Expand Down
18 changes: 18 additions & 0 deletions dakara_server/playlist/migrations/0016_playlist_entry_date_play.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.12 on 2022-04-13 17:08

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("playlist", "0015_id_fields"),
]

operations = [
migrations.RenameField(
model_name="playlistentry",
old_name="date_played",
new_name="date_play",
),
]
16 changes: 8 additions & 8 deletions dakara_server/playlist/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PlaylistManager(OrderedModelManager):

def get_playing(self):
"""Get the current playlist entry."""
playlist = self.filter(was_played=False, date_played__isnull=False)
playlist = self.filter(was_played=False, date_play__isnull=False)

if not playlist:
return None
Expand All @@ -33,15 +33,15 @@ def get_playing(self):

return playlist.first()

def get_playlist(self):
def get_queuing(self):
"""Get the playlist of ongoing entries."""
queryset = self.exclude(
models.Q(was_played=True) | models.Q(date_played__isnull=False)
models.Q(was_played=True) | models.Q(date_play__isnull=False)
)

return queryset

def get_playlist_played(self):
def get_played(self):
"""Get the playlist of passed entries."""
playlist = self.filter(was_played=True)

Expand All @@ -62,10 +62,10 @@ def get_next(self, entry_id=None):

else:
# do not process a played entry
if self.get_playlist_played().filter(pk=entry_id):
if self.get_played().filter(pk=entry_id):
return None

playlist = self.get_playlist().exclude(pk=entry_id)
playlist = self.get_queuing().exclude(pk=entry_id)

if not playlist:
return None
Expand All @@ -83,7 +83,7 @@ class PlaylistEntry(OrderedModel):
date_created = models.DateTimeField(auto_now_add=True)
owner = models.ForeignKey(DakaraUser, null=False, on_delete=models.CASCADE)
was_played = models.BooleanField(default=False, null=False)
date_played = models.DateTimeField(null=True)
date_play = models.DateTimeField(null=True)

class Meta(OrderedModel.Meta):
pass
Expand All @@ -98,7 +98,7 @@ def set_playing(self):
raise RuntimeError("A playlist entry is currently in play")

# set the playlist entry
self.date_played = datetime.now(tz)
self.date_play = datetime.now(tz)
self.save()

def set_finished(self):
Expand Down
117 changes: 81 additions & 36 deletions dakara_server/playlist/serializers.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
from datetime import datetime

from django.utils import timezone
from rest_framework import serializers

from library.models import Song
from library.serializers import (
SecondsDurationField,
SongForDigestSerializer,
SongForPlayerSerializer,
SongSerializer,
)
from playlist.models import Karaoke, Player, PlayerError, PlayerToken, PlaylistEntry
from users.serializers import UserForPublicSerializer

tz = timezone.get_default_timezone()


class PlaylistEntrySerializer(serializers.ModelSerializer):
"""Playlist entry serializer."""
Expand All @@ -27,8 +33,17 @@

class Meta:
model = PlaylistEntry
fields = ("id", "date_created", "owner", "song", "song_id", "use_instrumental")
read_only_fields = ("date_created",)
fields = (
"id",
"date_created",
"owner",
"song",
"song_id",
"use_instrumental",
"date_play",
"was_played",
)
read_only_fields = ("date_created", "date_play", "was_played")

def validate(self, data):
if data.get("use_instrumental") and not data["song"].has_instrumental:
Expand All @@ -49,56 +64,44 @@
read_only_fields = ("date_created",)


class PlaylistEntryWithDatePlaySerializer(PlaylistEntrySerializer):
"""Playlist entry serializer.

Reserved for song that will be played.
"""
class PlaylistEntryForDigestSerializer(serializers.ModelSerializer):
"""Played playlist entry serializer for playlist digest info."""

# date of play in the future, added manually to the PlaylistEntry object
date_play = serializers.DateTimeField(read_only=True)
song = SongForDigestSerializer(many=False, read_only=True)
owner = UserForPublicSerializer(read_only=True)

class Meta(PlaylistEntrySerializer.Meta):
class Meta:
model = PlaylistEntry
fields = (
"id",
"date_created",
"date_play",
"owner",
"song",
"use_instrumental",
"date_play",
"was_played",
"owner",
)


class PlaylistPlayedEntryWithDatePlayedSerializer(PlaylistEntrySerializer):
"""Playlist entry serializer.

Reserved for song that were played.
"""

class Meta(PlaylistEntrySerializer.Meta):
fields = (
read_only_fields = (
"id",
"date_created",
"date_played",
"owner",
"song",
"use_instrumental",
"date_play",
"was_played",
"owner",
)
read_only_fields = ("date_created", "date_played")


class PlaylistEntriesWithDateEndSerializer(serializers.Serializer):
"""Playlist entries with playlist end date."""

results = PlaylistEntryWithDatePlaySerializer(many=True, read_only=True)
results = PlaylistEntrySerializer(many=True, read_only=True)
date_end = serializers.DateTimeField(read_only=True)


class PlayerStatusSerializer(serializers.ModelSerializer):
"""Player status serializer."""

# Read only fields for front
playlist_entry = PlaylistPlayedEntryWithDatePlayedSerializer(
playlist_entry = PlaylistEntrySerializer(
many=False, read_only=True, allow_null=True
)

Expand Down Expand Up @@ -129,6 +132,27 @@
read_only_fields = ("paused", "in_transition", "date", "playlist_entry")
to_update_fields = ("timing",)

def to_representation(self, instance, *args, **kwargs):
# override the representation method to force recalculation of player
# timing
self.recalculate_timing(instance)
return super().to_representation(instance, *args, **kwargs)

def recalculate_timing(self, player):
"""Manually update the player timing.

Args:
player (playlist.models.Player): Instance of the current player.
"""
if player is None or not isinstance(player, Player):
return

Check warning on line 148 in dakara_server/playlist/serializers.py

View check run for this annotation

Codecov / codecov/patch

dakara_server/playlist/serializers.py#L148

Added line #L148 was not covered by tests

now = datetime.now(tz)
if player.playlist_entry:
if not player.paused and not player.in_transition:
player.timing += now - player.date
player.date = now

def update(self, instance, validated_data):
# filter out read only values
curated_data = {
Expand Down Expand Up @@ -204,9 +228,7 @@
"""Player errors."""

# get related entry field
playlist_entry = PlaylistPlayedEntryWithDatePlayedSerializer(
many=False, read_only=True
)
playlist_entry = PlaylistEntrySerializer(many=False, read_only=True)

# set related entry field
playlist_entry_id = serializers.PrimaryKeyRelatedField(
Expand All @@ -215,15 +237,21 @@

class Meta:
model = PlayerError
fields = ("playlist_entry", "playlist_entry_id", "error_message")
fields = (
"id",
"playlist_entry",
"playlist_entry_id",
"error_message",
"date_created",
)
read_only_fields = ("date_created",)

def validate_playlist_entry_id(self, playlist_entry):
# check the playlist entry is currently playing, or was played, or is
# about to be played
if not (
playlist_entry == PlaylistEntry.objects.get_playing()
or playlist_entry in PlaylistEntry.objects.get_playlist_played()
or playlist_entry in PlaylistEntry.objects.get_played()
or PlaylistEntry.objects.get_playing() is None
and playlist_entry == PlaylistEntry.objects.get_next()
):
Expand All @@ -235,6 +263,22 @@
return playlist_entry


class PlayerErrorForDigestSerializer(serializers.ModelSerializer):
"""Player error serializers for playlist digest info."""

playlist_entry = PlaylistEntryForDigestSerializer(many=False, read_only=True)

class Meta:
model = PlayerError
fields = (
"id",
"playlist_entry",
"error_message",
"date_created",
)
read_only_fields = ("id", "playlist_entry", "error_message", "date_created")


class PlayerCommandSerializer(serializers.Serializer):
"""Player command serializer."""

Expand Down Expand Up @@ -269,9 +313,10 @@
class DigestSerializer(serializers.Serializer):
"""Combine player info and kara status."""

player_status = PlayerStatusSerializer() # TODO test this
player_errors = PlayerErrorSerializer(many=True)
player_status = PlayerStatusSerializer()
karaoke = KaraokeSerializer()
player_errors = PlayerErrorForDigestSerializer(many=True)
playlist_entries = PlaylistEntryForDigestSerializer(many=True)


class PlaylistReorderSerializer(serializers.Serializer):
Expand Down
Loading
Loading