Some checks failed
Build and Deploy Backend / build-and-deploy (push) Failing after 1m36s
87 lines
2.8 KiB
Python
87 lines
2.8 KiB
Python
"""
|
||
音乐模块视图 - App端
|
||
"""
|
||
from rest_framework import viewsets
|
||
from rest_framework.decorators import action
|
||
from rest_framework.permissions import IsAuthenticated
|
||
from drf_spectacular.utils import extend_schema
|
||
|
||
from utils.response import success, error
|
||
from utils.exceptions import ErrorCode
|
||
from apps.admins.authentication import AppJWTAuthentication
|
||
from .models import Track
|
||
from .serializers import TrackSerializer, GenerateMusicSerializer
|
||
|
||
|
||
@extend_schema(tags=['音乐'])
|
||
class MusicViewSet(viewsets.ViewSet):
|
||
"""音乐视图集(App端)"""
|
||
|
||
authentication_classes = [AppJWTAuthentication]
|
||
permission_classes = [IsAuthenticated]
|
||
|
||
@action(detail=False, methods=['get'], url_path='playlist')
|
||
def playlist(self, request):
|
||
"""
|
||
获取播放列表
|
||
GET /api/v1/music/playlist/
|
||
"""
|
||
tracks = Track.objects.filter(user=request.user)
|
||
return success(data={
|
||
'playlist': TrackSerializer(tracks, many=True).data,
|
||
})
|
||
|
||
def destroy(self, request, pk=None):
|
||
"""
|
||
删除音乐
|
||
DELETE /api/v1/music/{id}/
|
||
"""
|
||
try:
|
||
track = Track.objects.get(id=pk, user=request.user)
|
||
except Track.DoesNotExist:
|
||
return error(code=ErrorCode.TRACK_NOT_FOUND, message='曲目不存在')
|
||
track.delete()
|
||
return success(message='删除成功')
|
||
|
||
@action(detail=True, methods=['post'])
|
||
def favorite(self, request, pk=None):
|
||
"""
|
||
收藏/取消收藏
|
||
POST /api/v1/music/{id}/favorite/
|
||
"""
|
||
try:
|
||
track = Track.objects.get(id=pk, user=request.user)
|
||
except Track.DoesNotExist:
|
||
return error(code=ErrorCode.TRACK_NOT_FOUND, message='曲目不存在')
|
||
|
||
track.is_favorite = not track.is_favorite
|
||
track.save(update_fields=['is_favorite'])
|
||
|
||
return success(
|
||
data={'is_favorite': track.is_favorite},
|
||
message='已收藏' if track.is_favorite else '已取消收藏'
|
||
)
|
||
|
||
@action(detail=False, methods=['post'], url_path='generate')
|
||
def generate(self, request):
|
||
"""
|
||
生成音乐 (SSE 流式 - 占位)
|
||
POST /api/v1/music/generate/
|
||
"""
|
||
serializer = GenerateMusicSerializer(data=request.data)
|
||
if not serializer.is_valid():
|
||
return error(message=str(serializer.errors))
|
||
|
||
# TODO: 接入 MiniMax API 实现 SSE 流式生成
|
||
track = Track.objects.create(
|
||
user=request.user,
|
||
title='生成中...',
|
||
mood=serializer.validated_data.get('mood', 'calm'),
|
||
prompt=serializer.validated_data.get('text', ''),
|
||
)
|
||
|
||
return success(data={
|
||
'id': track.id,
|
||
'message': '音乐生成功能待接入 MiniMax API',
|
||
})
|