steeldonutcollection/frontend/templatetags/playlists.py

30 lines
807 B
Python

from random import choices
from django import template
from backend.models import Playlist
register = template.Library()
@register.simple_tag
def other_videos(playlist, video, count=4):
videos = playlist.videos.all().order_by("-published")
older = sorted(
list(videos.filter(published__lt=video.published)), key=lambda x: x.published)
newer = sorted(
list(videos.filter(published__gt=video.published)), key=lambda x: x.published)
newer_out = newer[:count-1]
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