diff --git a/frontend/urls.py b/frontend/urls.py index acc5fbf..18ef1a5 100644 --- a/frontend/urls.py +++ b/frontend/urls.py @@ -1,10 +1,11 @@ from django.urls import path -from .views import PlaylistView, VideoView +from .views import PlaylistView, VideoView, RandomView urlpatterns = [ path('', PlaylistView.as_view(), name="home"), path('video//', VideoView.as_view(), name="video"), path('playlist//', PlaylistView.as_view(), name="playlist"), + path('random/', RandomView.as_view(), name="randomvideo"), ] diff --git a/frontend/views.py b/frontend/views.py index 421586e..7d0afa1 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -1,6 +1,8 @@ -from django.views.generic import ListView, DetailView +from django.views.generic import ListView, DetailView, RedirectView from django.shortcuts import get_object_or_404 +from random import choice + from backend.models import Video, Playlist @@ -49,3 +51,10 @@ class VideoView(DetailView): def get_playlist(self): if (pid := self.request.GET.get("playlist")): return get_object_or_404(Playlist, id=pid) + +class RandomView(RedirectView): + permanent = False + + def get_redirect_url(self, *args, **kwargs): + ids = Video.objects.values_list("id", flat=True) + return Video.objects.get(id=choice(ids)).get_absolute_url() \ No newline at end of file diff --git a/templates/frontend/playlist.html b/templates/frontend/playlist.html index 764b303..acd7bf7 100644 --- a/templates/frontend/playlist.html +++ b/templates/frontend/playlist.html @@ -8,6 +8,8 @@