added track slug api view

This commit is contained in:
Alexander Karpov 2024-01-17 16:00:17 +03:00
parent 6f70c38ecf
commit c26a2ea8d0
4 changed files with 78 additions and 5 deletions

View File

@ -271,6 +271,10 @@ def create(self, validated_data):
return song_user_rating return song_user_rating
class ListSongSlugsSerializer(serializers.Serializer):
slugs = serializers.ListField(child=serializers.SlugField())
class ListPlaylistSerializer(serializers.ModelSerializer): class ListPlaylistSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Playlist model = Playlist

View File

@ -14,6 +14,7 @@
ListLikedSongsAPIView, ListLikedSongsAPIView,
ListPublicPlaylistAPIView, ListPublicPlaylistAPIView,
ListSongPlaylistsAPIView, ListSongPlaylistsAPIView,
ListSongSlugsAPIView,
ListUserListenedSongsAPIView, ListUserListenedSongsAPIView,
RemoveSongFromPlaylistAPIView, RemoveSongFromPlaylistAPIView,
RetrieveUpdateDestroyAlbumAPIView, RetrieveUpdateDestroyAlbumAPIView,
@ -41,6 +42,7 @@
name="retrieve_update_delete_playlist", name="retrieve_update_delete_playlist",
), ),
path("song/", ListCreateSongAPIView.as_view(), name="list_create_song"), path("song/", ListCreateSongAPIView.as_view(), name="list_create_song"),
path("song/slugs/", ListSongSlugsAPIView.as_view(), name="list_songs_slugs"),
path( path(
"song/<str:slug>", "song/<str:slug>",
RetrieveUpdateDestroySongAPIView.as_view(), RetrieveUpdateDestroySongAPIView.as_view(),

View File

@ -16,6 +16,7 @@
ListenSongSerializer, ListenSongSerializer,
ListPlaylistSerializer, ListPlaylistSerializer,
ListSongSerializer, ListSongSerializer,
ListSongSlugsSerializer,
PlaylistSerializer, PlaylistSerializer,
SongSerializer, SongSerializer,
) )
@ -80,11 +81,7 @@ def get_queryset(self):
return qs.select_related("creator") return qs.select_related("creator")
class ListCreateSongAPIView(LikedSongsContextMixin, generics.ListCreateAPIView): class ListBaseSongAPIView(generics.ListAPIView):
serializer_class = ListSongSerializer
permission_classes = [IsAdminOrReadOnly]
pagination_class = StandardResultsSetPagination
def get_queryset(self): def get_queryset(self):
search = self.request.query_params.get("search", None) search = self.request.query_params.get("search", None)
if search: if search:
@ -116,6 +113,14 @@ def get_queryset(self):
) )
return qs return qs
class ListCreateSongAPIView(
LikedSongsContextMixin, generics.ListCreateAPIView, ListBaseSongAPIView
):
serializer_class = ListSongSerializer
permission_classes = [IsAdminOrReadOnly]
pagination_class = StandardResultsSetPagination
@extend_schema( @extend_schema(
parameters=[ parameters=[
OpenApiParameter( OpenApiParameter(
@ -168,6 +173,67 @@ def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs) return self.list(request, *args, **kwargs)
class ListSongSlugsAPIView(ListBaseSongAPIView):
serializer_class = ListSongSlugsSerializer
permission_classes = [permissions.AllowAny]
@extend_schema(
parameters=[
OpenApiParameter(
name="search",
description="Search query",
required=False,
type=str,
),
OpenApiParameter(
name="sort",
description="Sorting algorithm",
required=False,
type=str,
examples=[
OpenApiExample(
"Default",
description="by date added",
value=None,
),
OpenApiExample(
"played",
description="by total times played",
value="played",
),
OpenApiExample(
"likes",
description="by total likes",
value="likes",
),
OpenApiExample(
"likes reversed",
description="by total likes",
value="-likes",
),
OpenApiExample(
"length",
description="by track length",
value="length",
),
OpenApiExample(
"uploaded",
description="by date uploaded",
value="uploaded",
),
],
),
]
)
def get(self, request, *args, **kwargs):
songs = self.get_queryset()
return Response(
data={
"songs": songs.values_list("slug", flat=True),
}
)
class RetrieveUpdateDestroySongAPIView(generics.RetrieveUpdateDestroyAPIView): class RetrieveUpdateDestroySongAPIView(generics.RetrieveUpdateDestroyAPIView):
lookup_field = "slug" lookup_field = "slug"
lookup_url_kwarg = "slug" lookup_url_kwarg = "slug"

View File

@ -48,6 +48,7 @@ class Song(BaseImageModel, ShortLinkModel):
creator = models.ForeignKey( creator = models.ForeignKey(
"users.User", related_name="songs", on_delete=models.SET_NULL, null=True "users.User", related_name="songs", on_delete=models.SET_NULL, null=True
) )
created = models.DateTimeField(auto_now_add=True)
meta = models.JSONField(blank=True, null=True) meta = models.JSONField(blank=True, null=True)
likes = models.IntegerField(default=0) likes = models.IntegerField(default=0)
volume = ArrayField(models.IntegerField(), null=True) volume = ArrayField(models.IntegerField(), null=True)