mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-05 09:29:25 +02:00
Views and logic for custom radios
This commit is contained in:
parent
df63252105
commit
e7f0c1b88b
13 changed files with 774 additions and 18 deletions
|
@ -1,11 +1,15 @@
|
|||
import random
|
||||
from rest_framework import serializers
|
||||
from django.core.exceptions import ValidationError
|
||||
from taggit.models import Tag
|
||||
from funkwhale_api.users.models import User
|
||||
from funkwhale_api.music.models import Track, Artist
|
||||
|
||||
from . import filters
|
||||
from . import models
|
||||
from .registries import registry
|
||||
|
||||
|
||||
class SimpleRadio(object):
|
||||
|
||||
def clean(self, instance):
|
||||
|
@ -50,7 +54,7 @@ class SessionRadio(SimpleRadio):
|
|||
|
||||
def filter_from_session(self, queryset):
|
||||
already_played = self.session.session_tracks.all().values_list('track', flat=True)
|
||||
queryset = queryset.exclude(pk__in=list(already_played))
|
||||
queryset = queryset.exclude(pk__in=already_played)
|
||||
return queryset
|
||||
|
||||
def pick(self, **kwargs):
|
||||
|
@ -64,6 +68,10 @@ class SessionRadio(SimpleRadio):
|
|||
self.session.add(choice)
|
||||
return picked_choices
|
||||
|
||||
def validate_session(self, data, **context):
|
||||
return data
|
||||
|
||||
|
||||
@registry.register(name='random')
|
||||
class RandomRadio(SessionRadio):
|
||||
def get_queryset(self, **kwargs):
|
||||
|
@ -83,6 +91,37 @@ class FavoritesRadio(SessionRadio):
|
|||
return Track.objects.filter(pk__in=track_ids)
|
||||
|
||||
|
||||
@registry.register(name='custom')
|
||||
class CustomRadio(SessionRadio):
|
||||
|
||||
def get_queryset_kwargs(self):
|
||||
kwargs = super().get_queryset_kwargs()
|
||||
kwargs['user'] = self.session.user
|
||||
kwargs['custom_radio'] = self.session.custom_radio
|
||||
return kwargs
|
||||
|
||||
def get_queryset(self, **kwargs):
|
||||
return filters.run(kwargs['custom_radio'].config)
|
||||
|
||||
def validate_session(self, data, **context):
|
||||
data = super().validate_session(data, **context)
|
||||
try:
|
||||
user = data['user']
|
||||
except KeyError:
|
||||
user = context['user']
|
||||
try:
|
||||
assert (
|
||||
data['custom_radio'].user == user or
|
||||
data['custom_radio'].is_public)
|
||||
except KeyError:
|
||||
raise serializers.ValidationError(
|
||||
'You must provide a custom radio')
|
||||
except AssertionError:
|
||||
raise serializers.ValidationError(
|
||||
"You don't have access to this radio")
|
||||
return data
|
||||
|
||||
|
||||
class RelatedObjectRadio(SessionRadio):
|
||||
"""Abstract radio related to an object (tag, artist, user...)"""
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue