Skip to content
Snippets Groups Projects
Verified Commit 54972e48 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Fixed unplayable playlists (bis repetita)

parent 5e631440
No related branches found
No related tags found
No related merge requests found
......@@ -147,14 +147,10 @@ class Playlist(models.Model):
class PlaylistTrackQuerySet(models.QuerySet):
def for_nested_serialization(self):
return (
self.select_related()
.select_related("track__album__artist")
.prefetch_related(
"track__tags", "track__uploads", "track__artist__albums__tracks__tags"
)
)
def for_nested_serialization(self, actor=None):
tracks = music_models.Track.objects.annotate_playable_by_actor(actor)
tracks = tracks.select_related("artist", "album__artist")
return self.prefetch_related(models.Prefetch("track", queryset=tracks, to_attr='_prefetched_track'))
def annotate_playable_by_actor(self, actor):
tracks = (
......
......@@ -10,18 +10,16 @@ from . import models
class PlaylistTrackSerializer(serializers.ModelSerializer):
track = TrackSerializer()
is_playable = serializers.SerializerMethodField()
# track = TrackSerializer()
track = serializers.SerializerMethodField()
class Meta:
model = models.PlaylistTrack
fields = ("id", "track", "playlist", "index", "creation_date", "is_playable")
fields = ("id", "track", "playlist", "index", "creation_date")
def get_is_playable(self, obj):
try:
return bool(obj.is_playable_by_actor)
except AttributeError:
return None
def get_track(self, o):
track = o._prefetched_track if hasattr(o, "_prefetched_track") else o.track
return TrackSerializer(track).data
class PlaylistTrackWriteSerializer(serializers.ModelSerializer):
......
......@@ -39,7 +39,9 @@ class PlaylistViewSet(
@detail_route(methods=["get"])
def tracks(self, request, *args, **kwargs):
playlist = self.get_object()
plts = playlist.playlist_tracks.all().for_nested_serialization()
plts = playlist.playlist_tracks.all().for_nested_serialization(
music_utils.get_actor_from_request(request)
)
serializer = serializers.PlaylistTrackSerializer(plts, many=True)
data = {"count": len(plts), "results": serializer.data}
return Response(data, status=200)
......@@ -59,7 +61,7 @@ class PlaylistViewSet(
plts = (
models.PlaylistTrack.objects.filter(pk__in=ids)
.order_by("index")
.for_nested_serialization()
.for_nested_serialization(music_utils.get_actor_from_request(request))
)
serializer = serializers.PlaylistTrackSerializer(plts, many=True)
data = {"count": len(plts), "results": serializer.data}
......@@ -97,7 +99,7 @@ class PlaylistTrackViewSet(
):
serializer_class = serializers.PlaylistTrackSerializer
queryset = models.PlaylistTrack.objects.all().for_nested_serialization()
queryset = models.PlaylistTrack.objects.all()
permission_classes = [
permissions.ConditionalAuthentication,
permissions.OwnerPermission,
......@@ -118,7 +120,7 @@ class PlaylistTrackViewSet(
lookup_field="playlist__privacy_level",
user_field="playlist__user",
)
).annotate_playable_by_actor(music_utils.get_actor_from_request(self.request))
).for_nested_serialization(music_utils.get_actor_from_request(self.request))
def perform_destroy(self, instance):
instance.delete(update_indexes=True)
......@@ -21,7 +21,7 @@
</div>
</h2>
<div class="ui hidden divider"></div>
<play-button class="orange" :tracks="tracks"><translate>Play all</translate></play-button>
<play-button class="orange" :is-playable="playlist.is_playable" :tracks="tracks"><translate>Play all</translate></play-button>
<button
class="ui icon button"
v-if="$store.state.auth.profile && playlist.user.id === $store.state.auth.profile.id"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment