From 9ce5b287635d16dbe0ade4aacd4a89536d7d0584 Mon Sep 17 00:00:00 2001 From: Kumi Date: Sun, 8 May 2022 13:56:37 +0200 Subject: [PATCH] Implement autoplay and autonext for playlists --- frontend/templatetags/playlists.py | 7 +++++ frontend/views.py | 9 +++++- static/js/nextvideo.js | 8 +++++ templates/frontend/base.html | 2 ++ templates/frontend/playlist.html | 48 ++++++++++++++++-------------- templates/frontend/video.html | 21 +++++++++---- 6 files changed, 65 insertions(+), 30 deletions(-) create mode 100644 static/js/nextvideo.js diff --git a/frontend/templatetags/playlists.py b/frontend/templatetags/playlists.py index 61d6742..995b752 100644 --- a/frontend/templatetags/playlists.py +++ b/frontend/templatetags/playlists.py @@ -21,3 +21,10 @@ def other_videos(playlist, video, count=4): older_out = older[-count+len(newer_out):] return sorted(older_out + newer_out, key=lambda x: x.published) + +@register.simple_tag +def next_video(playlist, video): + try: + return playlist.videos.filter(published__gt=video.published).order_by('published').first() + except: + return None \ No newline at end of file diff --git a/frontend/views.py b/frontend/views.py index f1d03b3..4a126ed 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -14,6 +14,7 @@ class PlaylistView(ListView): context = super().get_context_data(**kwargs) context["title"] = "All Videos" context["playlists"] = Playlist.objects.all() + context["playlist"] = self.get_playlist() return context def get_playlist(self): @@ -31,7 +32,8 @@ class PlaylistView(ListView): return queryset def get_ordering(self): - return self.request.GET.get('sort', '-published') + ordering = "published" if self.get_playlist() else "-published" + return self.request.GET.get('sort', ordering) class VideoView(DetailView): @@ -41,4 +43,9 @@ class VideoView(DetailView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["title"] = self.object.title + context["playlist"] = self.get_playlist() return context + + def get_playlist(self): + if (pid := self.request.GET.get("playlist")): + return get_object_or_404(Playlist, id=pid) diff --git a/static/js/nextvideo.js b/static/js/nextvideo.js new file mode 100644 index 0000000..dcd048a --- /dev/null +++ b/static/js/nextvideo.js @@ -0,0 +1,8 @@ +var player = document.getElementById("videoplayer"); + +var current = player.getAttribute("data-current"); +var next = player.getAttribute("data-next"); + +if (next) { + player.onended = function () { document.location.href = document.location.href.replace(current, next); }; +}; \ No newline at end of file diff --git a/templates/frontend/base.html b/templates/frontend/base.html index a688365..5d5a8c8 100644 --- a/templates/frontend/base.html +++ b/templates/frontend/base.html @@ -32,6 +32,8 @@ +{% block scripts %} +{% endblock %} \ No newline at end of file diff --git a/templates/frontend/playlist.html b/templates/frontend/playlist.html index 239274b..764b303 100644 --- a/templates/frontend/playlist.html +++ b/templates/frontend/playlist.html @@ -9,15 +9,16 @@
{% for video in object_list %} - {% endfor %} @@ -25,27 +26,28 @@
diff --git a/templates/frontend/video.html b/templates/frontend/video.html index 8679ec7..f7cfdf0 100644 --- a/templates/frontend/video.html +++ b/templates/frontend/video.html @@ -7,8 +7,12 @@

{{object.title}}

-
+
+ {% if playlist %}{% next_video playlist object as next %}{% endif %} +

Description

@@ -17,24 +21,26 @@
{% if object.playlist_set.all %}
Part of Playlists - {% for playlist in object.playlist_set.all %} - {{playlist.title}} + {% for pl in object.playlist_set.all %} + {{pl.title}} {% endfor %}

{% endif %} + {% if object.tags %}
Tags {% for tag in object.tags %} {{tag}} {% endfor %}

+ {% endif %}
Date {{ object.published.date }}
- {% for playlist in object.playlist_set.all %} + {% if playlist %}

Other videos in {{playlist.title}}

- {% endfor %} + {% endif %}

Random Videos

+{% endblock %} +{% block scripts %} + {% endblock %} \ No newline at end of file