Compare commits

..

3 Commits

3 changed files with 40 additions and 9 deletions

View File

@ -239,10 +239,26 @@ class Meta:
class FullAlbumSerializer(serializers.ModelSerializer):
songs = ListSongSerializer(many=True, read_only=True)
artists = serializers.SerializerMethodField("get_artists")
@extend_schema_field(AuthorSerializer(many=True))
def get_artists(self, obj):
artists = []
qs = Author.objects.cache().filter(
songs__id__in=obj.songs.cache().all().values("id").distinct()
)
for artist in qs:
if artist not in artists:
artists.append(artist)
return AuthorSerializer(
artists,
many=True,
).data
class Meta:
model = Album
fields = ["name", "link", "image", "songs"]
fields = ["name", "link", "image", "songs", "artists"]
extra_kwargs = {
"link": {"read_only": True},
"image": {"read_only": True},
@ -251,10 +267,27 @@ class Meta:
class FullAuthorSerializer(serializers.ModelSerializer):
songs = ListSongSerializer(many=True, read_only=True)
albums = serializers.SerializerMethodField(method_name="get_albums")
@extend_schema_field(AlbumSerializer(many=True))
def get_albums(self, obj):
qs = Album.objects.cache().filter(
songs__id__in=obj.songs.cache().all().values("id").distinct()
)
albums = []
for album in qs:
# TODO: rewrite to filter
if album not in albums:
albums.append(album)
return AlbumSerializer(
albums,
many=True,
).data
class Meta:
model = Author
fields = ["name", "link", "image", "songs"]
fields = ["name", "link", "image", "songs", "albums"]
extra_kwargs = {
"link": {"read_only": True},
"image": {"read_only": True},

View File

@ -272,9 +272,7 @@ class ListAlbumsAPIView(generics.ListAPIView):
serializer_class = AlbumSerializer
pagination_class = StandardResultsSetPagination
permission_classes = [permissions.AllowAny]
def get_queryset(self):
return Album.objects.all()
queryset = Album.objects.cache().all()
class RetrieveUpdateDestroyAlbumAPIView(
@ -284,15 +282,14 @@ class RetrieveUpdateDestroyAlbumAPIView(
lookup_url_kwarg = "slug"
permission_classes = [IsAdminOrReadOnly]
serializer_class = FullAlbumSerializer
queryset = Album.objects.cache().all()
class ListAuthorsAPIView(generics.ListAPIView):
serializer_class = AuthorSerializer
pagination_class = StandardResultsSetPagination
permission_classes = [permissions.AllowAny]
def get_queryset(self):
return Author.objects.all()
queryset = Author.objects.cache().all()
class RetrieveUpdateDestroyAuthorAPIView(
@ -302,6 +299,7 @@ class RetrieveUpdateDestroyAuthorAPIView(
lookup_url_kwarg = "slug"
permission_classes = [IsAdminOrReadOnly]
serializer_class = FullAuthorSerializer
queryset = Author.objects.cache().all()
class ListenSongAPIView(generics.GenericAPIView):

View File

@ -74,7 +74,7 @@
"blog.post": {"ops": ("fetch", "get"), "timeout": 20 * 15},
"themes.theme": {"ops": ("fetch", "get"), "timeout": 60 * 60},
"gallery.*": {"ops": ("fetch", "get", "list"), "timeout": 60 * 15},
"files.*": {"ops": ("fetch", "get"), "timeout": 60},
"files.*": {"ops": ("fetch", "get", "list"), "timeout": 60},
"auth.permission": {"ops": "all", "timeout": 60 * 15},
"music.*": {"ops": ("fetch", "get", "list"), "timeout": 60 * 15},
}