steeldonutcollection/frontend/templatetags/playlists.py

30 lines
807 B
Python
Raw Normal View History

2022-05-05 16:27:47 +00:00
from random import choices
from django import template
from backend.models import Playlist
register = template.Library()
2022-05-06 07:37:10 +00:00
2022-05-05 16:27:47 +00:00
@register.simple_tag
def other_videos(playlist, video, count=4):
videos = playlist.videos.all().order_by("-published")
2022-05-06 07:37:10 +00:00
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)
2022-05-05 16:27:47 +00:00
2022-05-06 07:37:10 +00:00
newer_out = newer[:count-1]
older_out = older[-count+len(newer_out):]
2022-05-05 16:27:47 +00:00
2022-05-06 07:37:10 +00:00
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