steeldonutcollection/frontend/views.py
2022-05-05 17:40:57 +02:00

45 lines
1.2 KiB
Python

from django.views.generic import ListView, DetailView
from django.shortcuts import get_object_or_404
from backend.models import Video, Playlist
class PlaylistView(ListView):
template_name = "frontend/playlist.html"
model = Video
paginate_by = 30
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["title"] = "All Videos"
context["playlists"] = Playlist.objects.all()
return context
def get_playlist(self):
if "pk" in self.kwargs.keys():
return get_object_or_404(Playlist, id=self.kwargs["pk"])
def get_queryset(self):
if playlist := self.get_playlist():
queryset = playlist.videos.all()
else:
queryset = Video.objects.all()
queryset = queryset.order_by(self.get_ordering())
return queryset
def get_ordering(self):
return self.request.GET.get('sort', '-published')
class VideoView(DetailView):
template_name = "frontend/video.html"
model = Video
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["title"] = self.object.title
return context