added distinct images to playlist, authors to album list, minor bug fixes

This commit is contained in:
Alexander Karpov 2024-02-22 16:02:41 +03:00
parent 87fc3a906f
commit 3acd858598
2 changed files with 46 additions and 8 deletions

View File

@ -22,9 +22,17 @@ class Meta:
class ListAlbumSerializer(serializers.ModelSerializer): class ListAlbumSerializer(serializers.ModelSerializer):
authors = serializers.SerializerMethodField(method_name="get_authors")
@extend_schema_field(ListAuthorSerializer(many=True))
def get_authors(self, obj):
return ListAuthorSerializer(
Author.objects.cache().filter(albums__id=obj.id), many=True
).data
class Meta: class Meta:
model = Album model = Album
fields = ["name", "slug", "image_cropped"] fields = ["name", "slug", "image_cropped", "authors"]
class SongSerializer(serializers.ModelSerializer): class SongSerializer(serializers.ModelSerializer):
@ -133,10 +141,32 @@ class Meta:
class PlaylistSerializer(SetUserModelSerializer): class PlaylistSerializer(SetUserModelSerializer):
creator = UserPublicInfoSerializer(read_only=True) creator = UserPublicInfoSerializer(read_only=True)
images = serializers.SerializerMethodField(method_name="get_images")
@extend_schema_field(serializers.ListField(child=serializers.ImageField()))
def get_images(self, obj):
# Get distinct album images from songs
images = (
Song.objects.cache()
.filter(
playlists__id__in=PlaylistSong.objects.cache()
.filter(playlist=obj)
.values("id")
)
.values_list("album__image", flat=True)
.distinct()[:4]
)
# Build absolute URI for each image
images = [
self.context["request"].build_absolute_uri(image)
for image in images
if image
]
return images
class Meta: class Meta:
model = Playlist model = Playlist
fields = ["name", "length", "slug", "private", "creator"] fields = ["name", "length", "slug", "images", "private", "creator"]
extra_kwargs = { extra_kwargs = {
"slug": {"read_only": True}, "slug": {"read_only": True},
"creator": {"read_only": True}, "creator": {"read_only": True},
@ -144,13 +174,21 @@ class Meta:
} }
class FullPlaylistSerializer(serializers.ModelSerializer): class FullPlaylistSerializer(PlaylistSerializer):
songs = ListSongSerializer(many=True, read_only=True) songs = serializers.SerializerMethodField(method_name="get_songs")
creator = UserPublicInfoSerializer(read_only=True) creator = UserPublicInfoSerializer(read_only=True)
@extend_schema_field(ListSongSerializer(many=True))
def get_songs(self, obj):
return ListSongSerializer(
Song.objects.cache().filter(playlists__id=obj.id),
many=True,
context=self.context,
).data
class Meta: class Meta:
model = Playlist model = Playlist
fields = ["name", "private", "creator", "songs"] fields = ["name", "private", "creator", "images", "songs"]
extra_kwargs = { extra_kwargs = {
"slug": {"read_only": True}, "slug": {"read_only": True},
"creator": {"read_only": True}, "creator": {"read_only": True},
@ -158,8 +196,8 @@ class Meta:
class AddSongToPlaylistSerializer(serializers.ModelSerializer): class AddSongToPlaylistSerializer(serializers.ModelSerializer):
song = serializers.SlugField() song = serializers.SlugField(write_only=True)
playlist = serializers.SlugField() playlist = serializers.SlugField(write_only=True)
class Meta: class Meta:
model = Playlist model = Playlist

View File

@ -5,4 +5,4 @@ set -o nounset
/install_preview_dependencies /install_preview_dependencies
celery -A config.celery_app worker --loglevel=info -c 5 celery -A config.celery_app worker --autoscale 20 -l INFO