Skip to content

Commit

Permalink
🐛 Fix enriched videos in playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienCozeDev committed Nov 6, 2023
1 parent 02634b2 commit 247e22d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 20 deletions.
22 changes: 19 additions & 3 deletions pod/enrichment/views.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
from django.contrib import messages
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.sites.shortcuts import get_current_site
from django.core.exceptions import PermissionDenied
from django.core.exceptions import SuspiciousOperation
from django.http import HttpResponse
from django.http import HttpResponse, HttpResponseNotFound
from django.template.loader import render_to_string
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext as _
from django.views.decorators.csrf import csrf_protect
from django.views.decorators.csrf import ensure_csrf_cookie

from pod.playlist.models import Playlist
from pod.playlist.utils import get_video_list_for_playlist, playlist_can_be_displayed
from pod.video.models import Video
from pod.video.utils import sort_videos_list
from pod.video.views import render_video
from django.contrib.sites.shortcuts import get_current_site

from .models import Enrichment, EnrichmentGroup
from .forms import EnrichmentForm, EnrichmentGroupForm

Expand Down Expand Up @@ -233,6 +238,17 @@ def edit_enrichment_cancel(request, video):
@csrf_protect
@ensure_csrf_cookie
def video_enrichment(request, slug, slug_c=None, slug_t=None, slug_private=None):
params = {}
if request.GET.get("playlist"):
playlist = get_object_or_404(Playlist, slug=request.GET.get("playlist"))
if playlist_can_be_displayed(request, playlist):
videos = sort_videos_list(get_video_list_for_playlist(playlist), "rank")
params = {
"playlist_in_get": playlist,
"videos": videos,
}
else:
return HttpResponseNotFound()
template_video = (
"enrichment/video_enrichment-iframe.html"
if (request.GET.get("is_iframe"))
Expand All @@ -244,7 +260,7 @@ def video_enrichment(request, slug, slug_c=None, slug_t=None, slug_private=None)
except ValueError:
raise SuspiciousOperation("Invalid video id")

return render_video(request, id, slug_c, slug_t, slug_private, template_video)
return render_video(request, id, slug_c, slug_t, slug_private, template_video, params)


"""
Expand Down
5 changes: 4 additions & 1 deletion pod/playlist/static/playlist/js/playlist-player.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Switch to the next video when this exists.
*/
function switchToNextVideo() {
function switchToNextVideo(withRefresh = false) {
const playerElements = Array.from(document.querySelectorAll('.player-element'));
const selectedElement = document.querySelector('.selected');
let currentIndex = playerElements.indexOf(selectedElement);
Expand All @@ -15,6 +15,9 @@ function switchToNextVideo() {
let nextElement = playerElements[currentIndex + 1];
if (!(nextElement.classList.contains('disabled'))) {
const videoSrc = playerElements[currentIndex + 1].getAttribute('href');
if (withRefresh) {
window.location.href = videoSrc;
}
(currentIndex === -1) ? currentIndex = playerElements.length - 1 : "";
if (nextElement.getAttribute('data-chapter') || playerElements[currentIndex].getAttribute('data-chapter')) {
window.location.href = videoSrc;
Expand Down
22 changes: 21 additions & 1 deletion pod/playlist/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,27 @@ def check_password(form_password: str, playlist: Playlist) -> bool:
Returns:
bool: True if the password provided matches the playlist password, False otherwise.
bool: `True` if the password provided matches the playlist password, `False` otherwise.
"""
hashed_password = hashlib.sha256(form_password.encode("utf-8")).hexdigest()
return hashed_password == playlist.password

def playlist_can_be_displayed(request: WSGIRequest, playlist: Playlist) -> bool:
"""
Check if the playlist can be displayed by the current user.
Args:
playlist (Playlist): The playlist.
Returns:
bool: `True` if the current user can be see the playlist, `False` otherwise.
"""
return (
playlist.visibility == "public"
or playlist.visibility == "protected"
or (
playlist.owner == request.user
or playlist in get_playlists_for_additional_owner(request.user)
or request.user.is_staff
)
)
8 changes: 4 additions & 4 deletions pod/video/templates/videos/video-all-info.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ <h1 class="page_title h2">
</a>
<div class="dropdown-menu">
{% if video.get_hashkey in request.get_full_path %}
<a href="{% url 'video:video_private' slug=video.slug slug_private=video.get_hashkey %}?redirect=false" class="dropdown-item" target="_blank">{% trans 'Original version' %}</a>
<a href="{% url 'video:video_private' slug=video.slug slug_private=video.get_hashkey %}?redirect=false&playlist={{ playlist_in_get.slug }}" class="dropdown-item" target="_blank">{% trans 'Original version' %}</a>
{% else %}
<a href="{% url 'video:video' slug=video.slug %}?redirect=false" class="dropdown-item" target="_blank">{% trans 'Original version' %}</a>
<a href="{% url 'video:video' slug=video.slug %}?redirect=false&playlist={{ playlist_in_get.slug }}" class="dropdown-item" target="_blank">{% trans 'Original version' %}</a>
{% endif %}
</div>
</li>
Expand All @@ -43,9 +43,9 @@ <h1 class="page_title h2">
<div class="dropdown-menu">
{% for version in versions %}
{% if video.get_hashkey in request.get_full_path %}
<a href="{{version.url}}{{ video.get_hashkey }}/?redirect=false" title="{{version.app}}" class="dropdown-item">{{version.link}}</a>
<a href="{{version.url}}{{ video.get_hashkey }}/?redirect=false&playlist={{ playlist_in_get.slug }}" title="{{version.app}}" class="dropdown-item">{{version.link}}</a>
{% else %}
<a href="{{version.url}}?redirect=false" title="{{version.app}}" class="dropdown-item">{{version.link}}</a>
<a href="{{version.url}}?redirect=false&playlist={{ playlist_in_get.slug }}" title="{{version.app}}" class="dropdown-item">{{version.link}}</a>
{% endif %}
{% endfor %}
</div>
Expand Down
5 changes: 3 additions & 2 deletions pod/video/templates/videos/video-script.html
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@

/* --- PLAYLIST --- */
{% if playlist_in_get.autoplay %}
console.log({% if "enrichment" in request.path %}true{% endif %});
player.on('ended', function (e) {
{% if COUNTDOWN_PLAYLIST_PLAYER >= 3 %}
const playButtonElement = document.querySelector('.vjs-big-play-button');
Expand All @@ -270,11 +271,11 @@
}
asyncStartCountDown().then(function () {
player.dispose();
switchToNextVideo();
switchToNextVideo({% if "enrichment" in request.path %}true{% endif %});
});
{% else %}
player.dispose();
switchToNextVideo();
switchToNextVideo({% if "enrichment" in request.path %}true{% endif %});
{% endif %}
});
{% endif %}
Expand Down
11 changes: 2 additions & 9 deletions pod/video/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from pod.playlist.utils import (
get_playlists_for_additional_owner,
get_video_list_for_playlist,
playlist_can_be_displayed,
user_can_see_playlist_video,
)
from pod.video.utils import get_videos as video_get_videos
Expand Down Expand Up @@ -880,15 +881,7 @@ def video(request, slug, slug_c=None, slug_t=None, slug_private=None):
template_video = "videos/video-iframe.html"
elif request.GET.get("playlist"):
playlist = get_object_or_404(Playlist, slug=request.GET.get("playlist"))
if (
playlist.visibility == "public"
or playlist.visibility == "protected"
or (
playlist.owner == request.user
or playlist in get_playlists_for_additional_owner(request.user)
or request.user.is_staff
)
):
if playlist_can_be_displayed(request, playlist):
videos = sort_videos_list(get_video_list_for_playlist(playlist), "rank")
params = {
"playlist_in_get": playlist,
Expand Down

0 comments on commit 247e22d

Please sign in to comment.