2018-01-07 21:13:32 +00:00
|
|
|
from django.db.models import Q
|
2018-06-10 08:55:16 +00:00
|
|
|
from rest_framework import mixins, permissions, status, viewsets
|
2018-01-07 21:13:32 +00:00
|
|
|
from rest_framework.decorators import detail_route, list_route
|
2018-06-10 08:55:16 +00:00
|
|
|
from rest_framework.response import Response
|
2017-06-23 21:00:42 +00:00
|
|
|
|
2018-06-16 12:42:26 +00:00
|
|
|
from funkwhale_api.common import permissions as common_permissions
|
2018-05-22 19:10:48 +00:00
|
|
|
from funkwhale_api.music.serializers import TrackSerializer
|
2017-06-23 21:00:42 +00:00
|
|
|
|
2018-06-10 08:55:16 +00:00
|
|
|
from . import filters, filtersets, models, serializers
|
2017-06-23 21:00:42 +00:00
|
|
|
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
class RadioViewSet(
|
2018-06-09 13:36:16 +00:00
|
|
|
mixins.CreateModelMixin,
|
|
|
|
mixins.RetrieveModelMixin,
|
|
|
|
mixins.UpdateModelMixin,
|
|
|
|
mixins.ListModelMixin,
|
|
|
|
mixins.DestroyModelMixin,
|
|
|
|
viewsets.GenericViewSet,
|
|
|
|
):
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
serializer_class = serializers.RadioSerializer
|
2018-06-16 12:42:26 +00:00
|
|
|
permission_classes = [
|
|
|
|
permissions.IsAuthenticated,
|
|
|
|
common_permissions.OwnerPermission,
|
|
|
|
]
|
2018-01-07 21:13:32 +00:00
|
|
|
filter_class = filtersets.RadioFilter
|
2018-06-16 12:42:26 +00:00
|
|
|
owner_field = "user"
|
|
|
|
owner_checks = ["write"]
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2018-06-16 12:42:26 +00:00
|
|
|
queryset = models.Radio.objects.all()
|
2018-01-07 21:13:32 +00:00
|
|
|
query = Q(is_public=True)
|
|
|
|
if self.request.user.is_authenticated:
|
|
|
|
query |= Q(user=self.request.user)
|
2018-06-16 12:42:26 +00:00
|
|
|
return queryset.filter(query)
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
return serializer.save(user=self.request.user)
|
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
return serializer.save(user=self.request.user)
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
@detail_route(methods=["get"])
|
2018-03-31 11:59:45 +00:00
|
|
|
def tracks(self, request, *args, **kwargs):
|
|
|
|
radio = self.get_object()
|
|
|
|
tracks = radio.get_candidates().for_nested_serialization()
|
2018-03-31 15:40:55 +00:00
|
|
|
|
|
|
|
page = self.paginate_queryset(tracks)
|
|
|
|
if page is not None:
|
2018-05-22 19:10:48 +00:00
|
|
|
serializer = TrackSerializer(page, many=True)
|
2018-03-31 15:40:55 +00:00
|
|
|
return self.get_paginated_response(serializer.data)
|
2018-03-31 11:59:45 +00:00
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
@list_route(methods=["get"])
|
2018-01-07 21:13:32 +00:00
|
|
|
def filters(self, request, *args, **kwargs):
|
|
|
|
serializer = serializers.FilterSerializer(
|
2018-06-09 13:36:16 +00:00
|
|
|
filters.registry.exposed_filters, many=True
|
|
|
|
)
|
2018-01-07 21:13:32 +00:00
|
|
|
return Response(serializer.data)
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
@list_route(methods=["post"])
|
2018-01-07 21:13:32 +00:00
|
|
|
def validate(self, request, *args, **kwargs):
|
|
|
|
try:
|
2018-06-09 13:36:16 +00:00
|
|
|
f_list = request.data["filters"]
|
2018-01-07 21:13:32 +00:00
|
|
|
except KeyError:
|
2018-06-09 13:36:16 +00:00
|
|
|
return Response({"error": "You must provide a filters list"}, status=400)
|
|
|
|
data = {"filters": []}
|
2018-01-07 21:13:32 +00:00
|
|
|
for f in f_list:
|
|
|
|
results = filters.test(f)
|
2018-06-09 13:36:16 +00:00
|
|
|
if results["candidates"]["sample"]:
|
|
|
|
qs = results["candidates"]["sample"].for_nested_serialization()
|
|
|
|
results["candidates"]["sample"] = TrackSerializer(qs, many=True).data
|
|
|
|
data["filters"].append(results)
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
return Response(data)
|
|
|
|
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
class RadioSessionViewSet(
|
|
|
|
mixins.CreateModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet
|
|
|
|
):
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
serializer_class = serializers.RadioSessionSerializer
|
|
|
|
queryset = models.RadioSession.objects.all()
|
2018-05-06 09:18:28 +00:00
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
2018-05-06 09:18:28 +00:00
|
|
|
return queryset.filter(user=self.request.user)
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
def get_serializer_context(self):
|
|
|
|
context = super().get_serializer_context()
|
2018-06-09 13:36:16 +00:00
|
|
|
context["user"] = self.request.user
|
2017-06-23 21:00:42 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
class RadioSessionTrackViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet):
|
2017-06-23 21:00:42 +00:00
|
|
|
serializer_class = serializers.RadioSessionTrackSerializer
|
|
|
|
queryset = models.RadioSessionTrack.objects.all()
|
2018-05-06 09:18:28 +00:00
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
2018-06-09 13:36:16 +00:00
|
|
|
session = serializer.validated_data["session"]
|
2017-06-23 21:00:42 +00:00
|
|
|
try:
|
2018-05-06 09:18:28 +00:00
|
|
|
assert request.user == session.user
|
2017-06-23 21:00:42 +00:00
|
|
|
except AssertionError:
|
|
|
|
return Response(status=status.HTTP_403_FORBIDDEN)
|
2018-06-09 15:41:59 +00:00
|
|
|
session.radio.pick()
|
2018-06-09 13:36:16 +00:00
|
|
|
session_track = session.session_tracks.all().latest("id")
|
2017-06-23 21:00:42 +00:00
|
|
|
# self.perform_create(serializer)
|
|
|
|
# dirty override here, since we use a different serializer for creation and detail
|
2018-06-09 13:36:16 +00:00
|
|
|
serializer = self.serializer_class(
|
|
|
|
instance=session_track, context=self.get_serializer_context()
|
|
|
|
)
|
2017-06-23 21:00:42 +00:00
|
|
|
headers = self.get_success_headers(serializer.data)
|
2018-06-09 13:36:16 +00:00
|
|
|
return Response(
|
|
|
|
serializer.data, status=status.HTTP_201_CREATED, headers=headers
|
|
|
|
)
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
def get_serializer_class(self, *args, **kwargs):
|
2018-06-09 13:36:16 +00:00
|
|
|
if self.action == "create":
|
2017-06-23 21:00:42 +00:00
|
|
|
return serializers.RadioSessionTrackSerializerCreate
|
|
|
|
return super().get_serializer_class(*args, **kwargs)
|