utils verplaatst, vote model toegevoegd
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
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.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
|
||||
Reference in New Issue
Block a user