87 lines
2.5 KiB
Python
87 lines
2.5 KiB
Python
from django import views as django_views
|
|
from django.views.generic import ListView
|
|
from django.http import JsonResponse
|
|
from django.http.request import HttpRequest
|
|
from django.shortcuts import render
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from music.models import Track
|
|
from music.models import Artist
|
|
from music.models import Album
|
|
from music.models import MusicLog
|
|
|
|
|
|
class TrackListView(ListView):
|
|
model = Track
|
|
template_name = "music/track_list.html"
|
|
context_object_name = "tracks"
|
|
paginate_by = 25
|
|
|
|
def get_queryset(self):
|
|
return (
|
|
super()
|
|
.get_queryset()
|
|
.select_related("album", "album__artist")
|
|
.order_by("id")
|
|
)
|
|
|
|
|
|
class ArtistListView(ListView):
|
|
model = Artist
|
|
template_name = "music/artist_list.html"
|
|
context_object_name = "artists"
|
|
paginate_by = 25
|
|
|
|
def get_queryset(self):
|
|
return super().get_queryset().order_by("id")
|
|
|
|
|
|
class ArtistDetailView(django_views.View):
|
|
def get(self, request: HttpRequest, pk: int, *args, **kwargs):
|
|
artist = get_object_or_404(Artist, id=pk)
|
|
return render(
|
|
request,
|
|
"music/artist_detail.html",
|
|
{
|
|
"artist": artist,
|
|
"albums": artist.albums.all().order_by("-release_date"),
|
|
},
|
|
)
|
|
|
|
|
|
class AlbumListView(ListView):
|
|
model = Album
|
|
template_name = "music/album_list.html"
|
|
context_object_name = "albums"
|
|
paginate_by = 25
|
|
|
|
def get_queryset(self):
|
|
return super().get_queryset().order_by("id")
|
|
|
|
|
|
class AlbumDetailView(django_views.View):
|
|
def get(self, request: HttpRequest, pk: int, *args, **kwargs):
|
|
album = get_object_or_404(Album, id=pk)
|
|
return render(request, "music/album_detail.html", {"album": album})
|
|
|
|
|
|
class TrackAPIView(django_views.View):
|
|
def get(self, request: HttpRequest, pk: int, *args, **kwargs):
|
|
track = get_object_or_404(Track, id=pk)
|
|
data = {
|
|
"id": track.id,
|
|
"title": track.title,
|
|
"url": track.file.url,
|
|
"album": {
|
|
"id": track.album.id,
|
|
"title": track.album.name,
|
|
"cover_image": track.album.preview_image.url,
|
|
"artist": {
|
|
"id": track.album.artist.id,
|
|
"name": track.album.artist.name,
|
|
},
|
|
},
|
|
}
|
|
MusicLog.objects.create(track=track, user_ip=request.META.get("REMOTE_ADDR", ""))
|
|
return JsonResponse(data)
|