funkwhale/api/funkwhale_api/radios/views.py

124 wiersze
4.4 KiB
Python
Czysty Zwykły widok Historia

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
from funkwhale_api.common import permissions as common_permissions
from funkwhale_api.music.serializers import TrackSerializer
2018-06-10 08:55:16 +00:00
from . import filters, filtersets, models, serializers
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
permission_classes = [
permissions.IsAuthenticated,
common_permissions.OwnerPermission,
]
2018-01-07 21:13:32 +00:00
filter_class = filtersets.RadioFilter
owner_field = "user"
owner_checks = ["write"]
2018-01-07 21:13:32 +00:00
def get_queryset(self):
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)
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:
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
):
serializer_class = serializers.RadioSessionSerializer
queryset = models.RadioSession.objects.all()
permission_classes = [permissions.IsAuthenticated]
def get_queryset(self):
queryset = super().get_queryset()
return queryset.filter(user=self.request.user)
def get_serializer_context(self):
context = super().get_serializer_context()
2018-06-09 13:36:16 +00:00
context["user"] = self.request.user
return context
2018-06-09 13:36:16 +00:00
class RadioSessionTrackViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet):
serializer_class = serializers.RadioSessionTrackSerializer
queryset = models.RadioSessionTrack.objects.all()
permission_classes = [permissions.IsAuthenticated]
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"]
try:
assert request.user == session.user
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")
# 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()
)
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
)
def get_serializer_class(self, *args, **kwargs):
2018-06-09 13:36:16 +00:00
if self.action == "create":
return serializers.RadioSessionTrackSerializerCreate
return super().get_serializer_class(*args, **kwargs)