Merge remote-tracking branch 'origin/master'

This commit is contained in:
Stepan Fedyanin 2024-06-11 17:49:48 +05:00
commit 97cfdb508a
4 changed files with 43 additions and 71 deletions

View File

@ -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/

View File

@ -50,6 +50,6 @@ class DeleteSongSchema(AutoSchema):
name='song_id',
location='form',
required=False,
schema=coreschema.Integer(description='ID трека')
schema=coreschema.String(description='ID трека')
),
]

View File

@ -1,8 +1,9 @@
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.decorators import action
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 rest_framework.permissions import IsAuthenticated
import requests
@ -11,42 +12,47 @@ from .schemas import SongSchema, DeleteSongSchema
from .models import Song, FavoriteSong
from .serializers import SongSerializer, FavoriteSongSerializer
class SongViewSet(ViewSet):
permission_classes_by_action = {
'list': [IsAuthenticated],
'retrieve': [IsAuthenticated],
'create_song': [IsAuthenticated]
}
class SongViewSet(GenericViewSet):
queryset = Song.objects.all()
serializer_class = SongSerializer
permission_classes = (IsAuthenticated,)
def list(self, request):
user_pk = request.user.pk
songs_pk = [i.song.pk for i in get_list_or_404(FavoriteSong, user=user_pk)]
queryset = Song.objects.filter(id__in = songs_pk)
serializer = SongSerializer(queryset, many=True)
songs_pk = FavoriteSong.objects.filter(user=request.user, song__isnull=False).values_list('song_id', flat=True)
songs = Song.objects.filter(id__in=songs_pk)
serializer = SongSerializer(songs, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
user_pk = request.user.pk
song_obj = get_object_or_404(Song, azura_id=pk).pk
queryset = FavoriteSong.objects.filter(user=user_pk, song=song_obj)
serializer = FavoriteSongSerializer(queryset[0])
@action(
detail=False,
methods=['get'],
url_path='check_is_favorite/(?P<azura_id>[a-zA-Z0-9_]+)',
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)
@action(detail=False, methods=['post'], schema=SongSchema())
def create_song(self, request):
song = Song.objects.filter(azura_id = request.data['azura_id'])
def add_favorite(self, request):
data = None
#Если трек есть в базе
if song:
try:
song = Song.objects.get(azura_id=request.data.get('azura_id'))
data = {
"song": song[0].id,
"song": song.pk,
"user": request.user.pk
}
#Если трека нету в базе
else:
except ObjectDoesNotExist:
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 = {
"Authorization": f"Bearer {api_key}"
"Authorization": f"Bearer {API_KEY}"
}
response = requests.get(file_url, headers=headers)
data = request.data
@ -56,10 +62,9 @@ class SongViewSet(ViewSet):
if serializer.is_valid():
serializer.save()
data = {
"song": song[0].id,
"song": serializer.data['id'],
"user": request.user.pk
}
serializer = FavoriteSongSerializer(data=data)
if serializer.is_valid():
serializer.save()
@ -69,15 +74,13 @@ class SongViewSet(ViewSet):
@action(detail=False, methods=['post'], schema=DeleteSongSchema())
def delete_song(self, request):
song_id = request.data['song_id']
item = get_object_or_404(FavoriteSong, pk=song_id)
try:
item = FavoriteSong.objects.get(user=request.user, song=request.data.get('song_id'))
item.delete()
return Response(status=status.HTTP_202_ACCEPTED)
def get_permissions(self):
try:
return [permission() for permission in self.permission_classes_by_action[self.action]]
except KeyError:
return [permission() for permission in self.permission_classes]
except ObjectDoesNotExist:
return Response({"error": 'Объекта не существует'}, status=status.HTTP_404_NOT_FOUND)

View File

@ -1,5 +1,6 @@
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
ROOT_DIR = BASE_DIR.parent.parent.parent