mirror of
https://github.com/Alexander-D-Karpov/akarpov
synced 2024-11-22 07:26:33 +03:00
updated music image handling in serializers
This commit is contained in:
parent
fb2b611dd8
commit
cb7bbbf49f
|
@ -1,3 +1,4 @@
|
||||||
|
from django.db.models import Q
|
||||||
from drf_spectacular.utils import extend_schema_field
|
from drf_spectacular.utils import extend_schema_field
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
@ -65,6 +66,7 @@ class ListSongSerializer(SetUserModelSerializer):
|
||||||
album = serializers.SerializerMethodField(method_name="get_album")
|
album = serializers.SerializerMethodField(method_name="get_album")
|
||||||
authors = serializers.SerializerMethodField(method_name="get_authors")
|
authors = serializers.SerializerMethodField(method_name="get_authors")
|
||||||
liked = serializers.SerializerMethodField(method_name="get_liked")
|
liked = serializers.SerializerMethodField(method_name="get_liked")
|
||||||
|
image_cropped = serializers.SerializerMethodField(method_name="get_image")
|
||||||
|
|
||||||
@extend_schema_field(serializers.BooleanField)
|
@extend_schema_field(serializers.BooleanField)
|
||||||
def get_liked(self, obj):
|
def get_liked(self, obj):
|
||||||
|
@ -88,6 +90,25 @@ def get_authors(self, obj):
|
||||||
).data
|
).data
|
||||||
return None
|
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:
|
class Meta:
|
||||||
model = Song
|
model = Song
|
||||||
fields = [
|
fields = [
|
||||||
|
|
|
@ -252,6 +252,7 @@ def update_album_info(album: AlbumModel, author_name: str = None) -> None:
|
||||||
save=True,
|
save=True,
|
||||||
)
|
)
|
||||||
os.remove(image_path)
|
os.remove(image_path)
|
||||||
|
album.save()
|
||||||
|
|
||||||
# Update Album Authors from Spotify data if available
|
# Update Album Authors from Spotify data if available
|
||||||
if spotify_album_info and "artists" in spotify_album_info:
|
if spotify_album_info and "artists" in spotify_album_info:
|
||||||
|
@ -334,6 +335,7 @@ def update_author_info(author: Author) -> None:
|
||||||
save=True,
|
save=True,
|
||||||
)
|
)
|
||||||
os.remove(image_path)
|
os.remove(image_path)
|
||||||
|
author.save()
|
||||||
|
|
||||||
if generated_name and not Author.objects.filter(slug=generated_name).exists():
|
if generated_name and not Author.objects.filter(slug=generated_name).exists():
|
||||||
if len(generated_name) > 20:
|
if len(generated_name) > 20:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user