Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
97cfdb508a
32
README.md
32
README.md
|
|
@ -1,32 +0,0 @@
|
||||||
# ITRadio Project
|
|
||||||
|
|
||||||
## ITRadio Backend
|
|
||||||
|
|
||||||
|
|
||||||
1. Start the virtual environment:
|
|
||||||
```
|
|
||||||
pip install -r requirements.txt
|
|
||||||
```
|
|
||||||
2. cd
|
|
||||||
```
|
|
||||||
cd ITRadioBackend
|
|
||||||
```
|
|
||||||
3. Run the server:
|
|
||||||
```
|
|
||||||
python manage.py runserver
|
|
||||||
```
|
|
||||||
|
|
||||||
## ITRadio Frontend
|
|
||||||
|
|
||||||
To run the frontend, navigate to the `ITRadioFrontend` directory and run the development server:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
cd ITRadioFrontendVue/ITRadioFrontend
|
|
||||||
npm run dev
|
|
||||||
```
|
|
||||||
|
|
||||||
## AzuraCast
|
|
||||||
AzuraCast is used in this project. For installation and getting started, visit the AzuraCast Documentation.
|
|
||||||
https://www.azuracast.com/docs/getting-started/installation/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -50,6 +50,6 @@ class DeleteSongSchema(AutoSchema):
|
||||||
name='song_id',
|
name='song_id',
|
||||||
location='form',
|
location='form',
|
||||||
required=False,
|
required=False,
|
||||||
schema=coreschema.Integer(description='ID трека')
|
schema=coreschema.String(description='ID трека')
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from rest_framework.viewsets import ViewSet
|
from rest_framework.viewsets import ViewSet, GenericViewSet
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
|
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
|
||||||
from django.shortcuts import get_object_or_404, get_list_or_404
|
from django.shortcuts import get_object_or_404, get_list_or_404
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
import requests
|
import requests
|
||||||
|
|
@ -11,55 +12,59 @@ from .schemas import SongSchema, DeleteSongSchema
|
||||||
from .models import Song, FavoriteSong
|
from .models import Song, FavoriteSong
|
||||||
from .serializers import SongSerializer, FavoriteSongSerializer
|
from .serializers import SongSerializer, FavoriteSongSerializer
|
||||||
|
|
||||||
class SongViewSet(ViewSet):
|
class SongViewSet(GenericViewSet):
|
||||||
permission_classes_by_action = {
|
queryset = Song.objects.all()
|
||||||
'list': [IsAuthenticated],
|
serializer_class = SongSerializer
|
||||||
'retrieve': [IsAuthenticated],
|
permission_classes = (IsAuthenticated,)
|
||||||
'create_song': [IsAuthenticated]
|
|
||||||
}
|
|
||||||
def list(self, request):
|
def list(self, request):
|
||||||
user_pk = request.user.pk
|
songs_pk = FavoriteSong.objects.filter(user=request.user, song__isnull=False).values_list('song_id', flat=True)
|
||||||
songs_pk = [i.song.pk for i in get_list_or_404(FavoriteSong, user=user_pk)]
|
songs = Song.objects.filter(id__in=songs_pk)
|
||||||
queryset = Song.objects.filter(id__in = songs_pk)
|
serializer = SongSerializer(songs, many=True)
|
||||||
serializer = SongSerializer(queryset, many=True)
|
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
def retrieve(self, request, pk=None):
|
@action(
|
||||||
user_pk = request.user.pk
|
detail=False,
|
||||||
song_obj = get_object_or_404(Song, azura_id=pk).pk
|
methods=['get'],
|
||||||
queryset = FavoriteSong.objects.filter(user=user_pk, song=song_obj)
|
url_path='check_is_favorite/(?P<azura_id>[a-zA-Z0-9_]+)',
|
||||||
serializer = FavoriteSongSerializer(queryset[0])
|
url_name='check_is_favorite',
|
||||||
|
)
|
||||||
|
def check_is_favorite(self, request, azura_id):
|
||||||
|
try:
|
||||||
|
song_obj = Song.objects.get(azura_id=azura_id)
|
||||||
|
favorite_songs = FavoriteSong.objects.get(user=request.user, song=song_obj)
|
||||||
|
except ObjectDoesNotExist:
|
||||||
|
return Response({"error": 'Объекта не существует'}, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
serializer = FavoriteSongSerializer(favorite_songs)
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
|
|
||||||
@action(detail=False, methods=['post'], schema=SongSchema())
|
@action(detail=False, methods=['post'], schema=SongSchema())
|
||||||
def create_song(self, request):
|
def add_favorite(self, request):
|
||||||
song = Song.objects.filter(azura_id = request.data['azura_id'])
|
|
||||||
data = None
|
data = None
|
||||||
#Если трек есть в базе
|
try:
|
||||||
if song:
|
song = Song.objects.get(azura_id=request.data.get('azura_id'))
|
||||||
data = {
|
data = {
|
||||||
"song": song[0].id,
|
"song": song.pk,
|
||||||
"user": request.user.pk
|
"user": request.user.pk
|
||||||
}
|
}
|
||||||
#Если трека нету в базе
|
except ObjectDoesNotExist:
|
||||||
else:
|
|
||||||
file_url = f"http://82.97.242.49:10084/api/station/it-radio/file/{request.data['azura_id']}"
|
file_url = f"http://82.97.242.49:10084/api/station/it-radio/file/{request.data['azura_id']}"
|
||||||
api_key = "49226d3488aac3f5:18d88659c6c1c5e131a0ce0a94d55235"
|
API_KEY = "49226d3488aac3f5:18d88659c6c1c5e131a0ce0a94d55235"
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": f"Bearer {api_key}"
|
"Authorization": f"Bearer {API_KEY}"
|
||||||
}
|
}
|
||||||
response = requests.get(file_url, headers=headers)
|
response = requests.get(file_url, headers=headers)
|
||||||
data = request.data
|
data = request.data
|
||||||
file_play = f"http://82.97.242.49:10084/api/station/it-radio/file/{response.json()['unique_id']}/play"
|
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)
|
data.update(unique_id=file_play)
|
||||||
serializer = SongSerializer(data=data)
|
serializer = SongSerializer(data=data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
serializer.save()
|
serializer.save()
|
||||||
data = {
|
data = {
|
||||||
"song": song[0].id,
|
"song": serializer.data['id'],
|
||||||
"user": request.user.pk
|
"user": request.user.pk
|
||||||
}
|
}
|
||||||
|
|
||||||
serializer = FavoriteSongSerializer(data=data)
|
serializer = FavoriteSongSerializer(data=data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
serializer.save()
|
serializer.save()
|
||||||
|
|
@ -69,15 +74,13 @@ class SongViewSet(ViewSet):
|
||||||
|
|
||||||
@action(detail=False, methods=['post'], schema=DeleteSongSchema())
|
@action(detail=False, methods=['post'], schema=DeleteSongSchema())
|
||||||
def delete_song(self, request):
|
def delete_song(self, request):
|
||||||
song_id = request.data['song_id']
|
|
||||||
item = get_object_or_404(FavoriteSong, pk=song_id)
|
|
||||||
item.delete()
|
|
||||||
return Response(status=status.HTTP_202_ACCEPTED)
|
|
||||||
|
|
||||||
def get_permissions(self):
|
|
||||||
try:
|
try:
|
||||||
return [permission() for permission in self.permission_classes_by_action[self.action]]
|
item = FavoriteSong.objects.get(user=request.user, song=request.data.get('song_id'))
|
||||||
except KeyError:
|
item.delete()
|
||||||
return [permission() for permission in self.permission_classes]
|
return Response(status=status.HTTP_202_ACCEPTED)
|
||||||
|
except ObjectDoesNotExist:
|
||||||
|
return Response({"error": 'Объекта не существует'}, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import os
|
import os
|
||||||
|
|
||||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||||
ROOT_DIR = BASE_DIR.parent.parent.parent
|
ROOT_DIR = BASE_DIR.parent.parent.parent
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue