diff --git a/akarpov/music/api/serializers.py b/akarpov/music/api/serializers.py index 6a1639e..f57325b 100644 --- a/akarpov/music/api/serializers.py +++ b/akarpov/music/api/serializers.py @@ -1,3 +1,4 @@ +from django.db.models import Q from drf_spectacular.utils import extend_schema_field from rest_framework import serializers @@ -65,6 +66,7 @@ class ListSongSerializer(SetUserModelSerializer): album = serializers.SerializerMethodField(method_name="get_album") authors = serializers.SerializerMethodField(method_name="get_authors") liked = serializers.SerializerMethodField(method_name="get_liked") + image_cropped = serializers.SerializerMethodField(method_name="get_image") @extend_schema_field(serializers.BooleanField) def get_liked(self, obj): @@ -88,6 +90,25 @@ def get_authors(self, obj): ).data return None + @extend_schema_field(serializers.ImageField) + def get_image(self, obj): + img = None + if obj.image_cropped: + img = obj.image_cropped + else: + album = Album.objects.cache().get(id=obj.album_id) + if album.image_cropped: + img = album.image_cropped + else: + authors = Author.objects.cache().filter( + Q(songs__id=obj.id) & ~Q(image="") + ) + if authors: + img = authors.first().image_cropped + if img: + return self.context["request"].build_absolute_uri(img.url) + return None + class Meta: model = Song fields = [ diff --git a/akarpov/music/services/info.py b/akarpov/music/services/info.py index 079d98a..7fe4077 100644 --- a/akarpov/music/services/info.py +++ b/akarpov/music/services/info.py @@ -252,6 +252,7 @@ def update_album_info(album: AlbumModel, author_name: str = None) -> None: save=True, ) os.remove(image_path) + album.save() # Update Album Authors from Spotify data if available if spotify_album_info and "artists" in spotify_album_info: @@ -334,6 +335,7 @@ def update_author_info(author: Author) -> None: save=True, ) os.remove(image_path) + author.save() if generated_name and not Author.objects.filter(slug=generated_name).exists(): if len(generated_name) > 20: