создал функционал для сохранения треков

This commit is contained in:
Mike0001-droid 2024-06-06 16:48:50 +05:00
parent e00dab5aa8
commit 53ea3f2aa9
6 changed files with 91 additions and 18 deletions

View File

@ -1,3 +1,6 @@
from django.contrib import admin
from .models import Song
# Register your models here.
@admin.register(Song)
class SongAdmin(admin.ModelAdmin):
list_display = ('title', 'artist', 'genre', 'album')

View File

@ -1,14 +1,12 @@
from django.db import models
# class Song(models.Model):
# name = models.CharField(max_length=255)
# artist = models.CharField(max_length=255)
# album = models.ForeignKey('Album', on_delete=models.CASCADE)
# ...
class Song(models.Model):
azura_id = models.CharField('ID трека с Азуры', max_length=255)
title = models.CharField('Название трека', max_length=255)
artist = models.CharField('Исполнитель', max_length=255)
album = models.CharField('Альбом трека', blank=True, null=True, max_length=255)
genre = models.CharField('Жанр трека', blank=True, null=True, max_length=50)
art = models.ImageField('Изображение трека', blank=True, null=True, upload_to="song_images/")
# class Playlist(models.Model):
# songs = models.ManyToManyField(Song)
# class Podcast(models.Model):
# name = models.CharField(max_length=255)
# hosts = models.CharField(max_length=255)
def __str__(self, request):
return f"{self.artist} - {self.title}"

View File

@ -0,0 +1,45 @@
from rest_framework.schemas import AutoSchema
import coreapi
import coreschema
class SongSchema(AutoSchema):
def get_serializer_fields(self, path, method):
return [
coreapi.Field(
name='azura_id',
location='form',
required=False,
schema=coreschema.String(description='ID трека с Азуры')
),
coreapi.Field(
name='title',
location='form',
required=False,
schema=coreschema.Array(description='Название трека')
),
coreapi.Field(
name='artist',
location='form',
required=False,
schema=coreschema.Integer(description='Исполнитель')
),
coreapi.Field(
name='album',
location='form',
required=False,
schema=coreschema.Integer(description='Альбом трека')
),
coreapi.Field(
name='genre',
location='form',
required=False,
schema=coreschema.Integer(description='Жанр трека')
),
coreapi.Field(
name='art',
location='form',
required=False,
schema=coreschema.Integer(description='Изображение трека')
),
]

View File

@ -0,0 +1,7 @@
from rest_framework import serializers
from .models import Song
class SongSerializer(serializers.ModelSerializer):
class Meta:
model = Song
fields = ('id', 'azura_id', 'title', 'artist', 'album', 'genre', 'art')

View File

@ -1,3 +1,24 @@
from django.shortcuts import render
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework import status
# Create your views here.
from .schemas import SongSchema
from .models import Song
from .serializers import SongSerializer
class SongViewSet(ViewSet):
def list(self, request):
queryset = Song.objects.all()
serializer = SongSerializer(queryset, many=True)
return Response(serializer.data)
@action(detail=False, methods=['post'], schema=SongSchema())
def create_song(self, request):
song = SongSerializer(data=request.data)
if song.is_valid():
song.save()
return Response(song.data)
else:
return Response(status=status.HTTP_404_NOT_FOUND)

View File

@ -1,8 +1,6 @@
from django.contrib import admin
from rest_framework.documentation import include_docs_urls
from django.urls import path, include
# from api.views import AzuraNowPlayingWebhookView
#from loginApi.views import login, register
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
@ -10,20 +8,21 @@ from rest_framework_simplejwt.views import (
)
from django.conf.urls.static import static
from news import views as newsViews
from rest_framework import routers, serializers, viewsets
#from userProfile.views import ProfileViewSet, TeamViewSet
from rest_framework import routers
from userProfile.views import TeamViewSet
from django.conf import settings
from rubricks.views import RubricViewSet
from api.views import FetchAndServeFile
from audio.views import SongViewSet
router = routers.DefaultRouter()
router.register(r'news', newsViews.NewsViewSet)
#router.register(r'profiles', ProfileViewSet, basename='profiles')
router.register(r'teams', TeamViewSet, basename='teams')
router.register(r'rubriks', RubricViewSet, basename='rubriks')
router.register(r'song', SongViewSet, basename='song')
router.register(r'fetchandservefile', FetchAndServeFile, basename='fetchandservefile')
urlpatterns = [
path('api/admin/', admin.site.urls),
#path('api/fetch-file/<str:station>/<str:song_id>/', FetchAndServeFile.as_view(), name='fetch-file'),