Files
muzak/playlist/utils.py
T
mark fcf31cb896 Template refactor, task update
Banter no longer fails if AI offline. Refactoring of templates.
2024-02-18 23:33:09 +01:00

21 lines
719 B
Python

from .models import Track, Vote
from random import choice, seed
def from_json(cls, json):
try:
return cls.objects.get(pk=json["id"])
except cls.DoesNotExist:
keys = [f.name for f in cls._meta.get_fields()]
subset = {key:json[key] for key in set(keys) & set(json.keys())}
return cls.objects.create(**subset)
def get_unvoted(user):
votes = set(Vote.objects.filter(user=user).values_list('track_id', flat=True))
tracks = set(Track.objects.filter(banter_done=True).values_list('pk', flat=True))
seed(user.username)
try:
random_track = choice(list(tracks - votes))
return Track.objects.get(pk=random_track)
except IndexError:
return None