исправил добавление песни в плейлист

This commit is contained in:
Mike0001-droid 2024-06-21 10:40:05 +05:00
parent 0bbbc9a9f9
commit 5b7f1caedb
2 changed files with 47 additions and 10 deletions

View File

@ -73,6 +73,36 @@ class PlayListSchema(AutoSchema):
name='azura_id',
location='form',
required=False,
schema=coreschema.String(description='ID трека с азуры')
schema=coreschema.String(description='ID трека с Азуры')
),
coreapi.Field(
name='title',
location='form',
required=False,
schema=coreschema.String(description='Название трека')
),
coreapi.Field(
name='artist',
location='form',
required=False,
schema=coreschema.String(description='Исполнитель')
),
coreapi.Field(
name='album',
location='form',
required=False,
schema=coreschema.String(description='Альбом трека')
),
coreapi.Field(
name='genre',
location='form',
required=False,
schema=coreschema.String(description='Жанр трека')
),
coreapi.Field(
name='art',
location='form',
required=False,
schema=coreschema.String(description='Изображение трека')
),
]

View File

@ -72,13 +72,23 @@ class PlayListViewSet(GenericViewSet):
@action(detail=False, methods=['post'], schema=PlayListSchema())
def add_to_playlist(self, request):
song = None
try:
song = list(Song.objects.filter(azura_id=request.data.get('azura_id')).values_list('pk', flat=True))
song = Song.objects.get(azura_id=request.data.get('azura_id')).pk
except ObjectDoesNotExist:
return Response(
{'detail': 'Песни не существует', 'error': {'Song': 'Песни не существует'}},
status=status.HTTP_404_NOT_FOUND)
file_url = f"http://82.97.242.49:10084/api/station/it-radio/file/{request.data['azura_id']}"
API_KEY = "49226d3488aac3f5:18d88659c6c1c5e131a0ce0a94d55235"
headers = {
"Authorization": f"Bearer {API_KEY}"
}
response = requests.get(file_url, headers=headers)
data = request.data
file_play = f"http://82.97.242.49:10084/api/station/it-radio/file/{response.json()['unique_id']}/play"
data.update(unique_id=file_play)
song_serializer = SongSerializer(data=data)
if song_serializer.is_valid():
song_serializer.save()
song = song_serializer.data.get('id')
try:
instance = PlayList.objects.get(pk=request.data.get('playlist_id'))
except ObjectDoesNotExist:
@ -86,7 +96,7 @@ class PlayListViewSet(GenericViewSet):
{'detail': 'Плейлиста не существует', 'error': {'PlayList': 'Плейлиста не существует'}},
status=status.HTTP_404_NOT_FOUND)
songs = list(instance.song.all().values_list('pk', flat=True))+song
songs = list(instance.song.all().values_list('pk', flat=True)) + [song]
data = {
'playlist_id': request.data.get('playlist_id'),
'song': songs
@ -204,9 +214,6 @@ class SongViewSet(GenericViewSet):
for i in response.json():
i['azura_id'] = i.pop('song_id')
data.append(i)
return Response(data, status=status.HTTP_200_OK)